118 lines
4.2 KiB
C#
118 lines
4.2 KiB
C#
|
using ComputerHardwareStoreContracts.BindingModels;
|
|||
|
using ComputerHardwareStoreContracts.SearchModels;
|
|||
|
using ComputerHardwareStoreContracts.StorageContracts;
|
|||
|
using ComputerHardwareStoreContracts.ViewModels;
|
|||
|
using ComputerHardwareStoreDatabaseImplement.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
|||
|
namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class PurchaseStorage : IPurchaseStorage
|
|||
|
{
|
|||
|
public List<PurchaseViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
return context.Purchases
|
|||
|
.Include(p => p.Builds)
|
|||
|
.ThenInclude(p => p.Build)
|
|||
|
.Include(p => p.Products)
|
|||
|
.ThenInclude(p => p.Product)
|
|||
|
.Select(p => p.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
return context.Purchases
|
|||
|
.Include(p => p.Builds)
|
|||
|
.ThenInclude(p => p.Build)
|
|||
|
.Include(p => p.Products)
|
|||
|
.ThenInclude(p => p.Product)
|
|||
|
.Where(p =>
|
|||
|
(model.Id.HasValue && p.Id == model.Id) ||
|
|||
|
((model.DateFrom.HasValue && model.DateTo.HasValue) &&
|
|||
|
((model.DateFrom <= p.DateCreate && p.DateCreate <= model.DateTo) ||
|
|||
|
(p.DateImplement.HasValue && p.DateCreate < model.DateFrom && model.DateFrom < p.DateImplement)))
|
|||
|
)
|
|||
|
.Select(p => p.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
return context.Purchases
|
|||
|
.Include(p => p.Builds)
|
|||
|
.ThenInclude(p => p.Build)
|
|||
|
.Include(p => p.Products)
|
|||
|
.ThenInclude(p => p.Product)
|
|||
|
.FirstOrDefault(p => p.Id == model.Id)?
|
|||
|
.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public PurchaseViewModel? Insert(PurchaseBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
var newPurchase = Purchase.Create(context, model);
|
|||
|
if (newPurchase == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Purchases.Add(newPurchase);
|
|||
|
context.SaveChanges();
|
|||
|
return newPurchase.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public PurchaseViewModel? Update(PurchaseBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var purchase = context.Purchases
|
|||
|
.Include(p => p.Builds)
|
|||
|
.ThenInclude(p => p.Build)
|
|||
|
.Include(p => p.Products)
|
|||
|
.ThenInclude(p => p.Product)
|
|||
|
.FirstOrDefault(p => p.Id == model.Id);
|
|||
|
if (purchase == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
purchase.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
transaction.Commit();
|
|||
|
return purchase.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public PurchaseViewModel? Delete(PurchaseBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
var element = context.Purchases
|
|||
|
.Include(p => p.Builds)
|
|||
|
.ThenInclude(p => p.Build)
|
|||
|
.Include(p => p.Products)
|
|||
|
.ThenInclude(p => p.Product)
|
|||
|
.FirstOrDefault(o => o.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Purchases.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|