2023-04-07 16:10:59 +04:00
|
|
|
|
using ComputerShopContracts.BindingModels;
|
|
|
|
|
using ComputerShopContracts.SearchModels;
|
|
|
|
|
using ComputerShopContracts.StorageContracts;
|
|
|
|
|
using ComputerShopContracts.ViewModels;
|
|
|
|
|
using ComputerShopDatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ComputerShopDatabaseImplement.Implements
|
|
|
|
|
{
|
2023-05-19 19:34:21 +04:00
|
|
|
|
public class SupplyStorage : ISupplyStorage
|
2023-04-07 16:10:59 +04:00
|
|
|
|
{
|
|
|
|
|
public SupplyViewModel? Delete(SupplyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
|
|
|
|
var element = context.Supplies
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Supplies.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SupplyViewModel? GetElement(SupplySearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
|
|
|
|
return context.Supplies
|
|
|
|
|
.Include(x => x.Orders)
|
2023-05-20 00:08:47 +04:00
|
|
|
|
.ThenInclude(x => x.Order).Include(x => x.Receiving)
|
2023-04-07 16:10:59 +04:00
|
|
|
|
.FirstOrDefault(x =>
|
|
|
|
|
(model.Id.HasValue && x.Id ==
|
|
|
|
|
model.Id))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
|
|
|
|
|
{
|
2023-05-19 18:21:13 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.ComponentId.HasValue)
|
2023-04-07 16:10:59 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
2023-05-24 18:51:13 +04:00
|
|
|
|
if(!model.ComponentId.HasValue)
|
2023-05-19 18:21:13 +04:00
|
|
|
|
return context.Supplies
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.ThenInclude(x => x.Order)
|
2023-05-20 00:08:47 +04:00
|
|
|
|
.Include(x => x.Receiving)
|
2023-05-19 18:21:13 +04:00
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
else
|
|
|
|
|
return context.Supplies
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.ThenInclude(x => x.Order)
|
2023-05-20 00:08:47 +04:00
|
|
|
|
.Include(x => x.Receiving)
|
2023-05-24 18:51:13 +04:00
|
|
|
|
.Include(x => x.Components)
|
2023-05-19 18:21:13 +04:00
|
|
|
|
.ThenInclude(x => x.Component)
|
2023-05-24 18:51:13 +04:00
|
|
|
|
.Where(x => x.Components.Any(x => x.ComponentId == model.ComponentId))
|
2023-05-19 18:21:13 +04:00
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-07 16:10:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SupplyViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
|
|
|
|
return context.Supplies
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.ThenInclude(x => x.Order)
|
2023-05-24 18:51:13 +04:00
|
|
|
|
.Include(x => x.Components)
|
|
|
|
|
.ThenInclude(x => x.Component)
|
2023-05-20 00:08:47 +04:00
|
|
|
|
.Include(x => x.Receiving)
|
2023-04-07 16:10:59 +04:00
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SupplyViewModel? Insert(SupplyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
|
|
|
|
var newProduct = Supply.Create(context, model);
|
|
|
|
|
if (newProduct == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Supplies.Add(newProduct);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newProduct.GetViewModel;
|
2023-05-20 00:08:47 +04:00
|
|
|
|
|
2023-04-07 16:10:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SupplyViewModel? Update(SupplyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ComputerShopDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var product = context.Supplies.FirstOrDefault(rec =>
|
|
|
|
|
rec.Id == model.Id);
|
|
|
|
|
if (product == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
product.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
product.UpdateOrders(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return product.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|