DatabaseImplement - ShopStorage

This commit is contained in:
ShabOl 2024-04-17 01:36:29 +04:00
parent fe3e9171e8
commit 3d750ef0b2

View File

@ -0,0 +1,220 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace AutoWorkshopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var Context = new AutoWorkshopDatabase();
return Context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ShopName))
return new();
using var Context = new AutoWorkshopDatabase();
return Context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.Where(x => x.ShopName.Contains(Model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ShopName) && !Model.Id.HasValue)
return new();
using var Context = new AutoWorkshopDatabase();
return Context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.FirstOrDefault(x => (!string.IsNullOrEmpty(Model.ShopName) && x.ShopName == Model.ShopName) ||
(Model.Id.HasValue && x.Id == Model.Id))?
.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel Model)
{
using var Context = new AutoWorkshopDatabase();
var NewShop = Shop.Create(Context, Model);
if (NewShop == null)
return null;
Context.Shops.Add(NewShop);
Context.SaveChanges();
return NewShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel Model)
{
using var Context = new AutoWorkshopDatabase();
using var Transaction = Context.Database.BeginTransaction();
try
{
var Shop = Context.Shops.FirstOrDefault(x => x.Id == Model.Id);
if (Shop == null)
return null;
Shop.Update(Model);
Context.SaveChanges();
Shop.UpdateRepairs(Context, Model);
Transaction.Commit();
return Shop.GetViewModel;
}
catch
{
Transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel Model)
{
using var Context = new AutoWorkshopDatabase();
var Shop = Context.Shops
.Include(x => x.Repairs)
.FirstOrDefault(x => x.Id == Model.Id);
if (Shop == null)
return null;
Context.Shops.Remove(Shop);
Context.SaveChanges();
return Shop.GetViewModel;
}
public bool RestockingShops(SupplyBindingModel Model)
{
using var Context = new AutoWorkshopDatabase();
var Transaction = Context.Database.BeginTransaction();
var Shops = Context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.ToList()
.Where(x => x.RepairsMaxCount > x.ShopRepairs
.Select(x => x.Value.Item2).Sum())
.ToList();
try
{
foreach (Shop Shop in Shops)
{
int Difference = Shop.RepairsMaxCount - Shop.ShopRepairs.Select(x => x.Value.Item2).Sum();
int Refill = Math.Min(Difference, Model.Count);
Model.Count -= Refill;
if (Shop.ShopRepairs.ContainsKey(Model.RepairId))
{
var DatePair = Shop.ShopRepairs[Model.RepairId];
DatePair.Item2 += Refill;
Shop.ShopRepairs[Model.RepairId] = DatePair;
}
else
{
var Repair = Context.Repairs.First(x => x.Id == Model.RepairId);
Shop.ShopRepairs.Add(Model.RepairId, (Repair, Refill));
}
Shop.RepairsDictionatyUpdate(Context);
if (Model.Count == 0)
{
Transaction.Commit();
return true;
}
}
Transaction.Rollback();
return false;
}
catch
{
Transaction.Rollback();
throw;
}
}
public bool Sell(SupplySearchModel Model)
{
using var Context = new AutoWorkshopDatabase();
var Transaction = Context.Database.BeginTransaction();
try
{
var Shops = Context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.ToList()
.Where(x => x.ShopRepairs.ContainsKey(Model.RepairId.Value))
.OrderByDescending(x => x.ShopRepairs[Model.RepairId.Value].Item2)
.ToList();
foreach (var Shop in Shops)
{
int Residue = Model.Count.Value - Shop.ShopRepairs[Model.RepairId.Value].Item2;
if (Residue > 0)
{
Shop.ShopRepairs.Remove(Model.RepairId.Value);
Shop.RepairsDictionatyUpdate(Context);
Context.SaveChanges();
Model.Count = Residue;
}
else
{
if (Residue == 0)
Shop.ShopRepairs.Remove(Model.RepairId.Value);
else
{
var DataPair = Shop.ShopRepairs[Model.RepairId.Value];
DataPair.Item2 = -Residue;
Shop.ShopRepairs[Model.RepairId.Value] = DataPair;
}
Shop.RepairsDictionatyUpdate(Context);
Transaction.Commit();
return true;
}
}
Transaction.Rollback();
return false;
}
catch
{
Transaction.Rollback();
throw;
}
}
}
}