141 lines
4.6 KiB
C#
141 lines
4.6 KiB
C#
using ConfectioneryContracts.BindingModels;
|
||
using ConfectioneryContracts.SearchModels;
|
||
using ConfectioneryContracts.StoragesContract;
|
||
using ConfectioneryContracts.ViewModels;
|
||
using ConfectioneryDatabaseImplement.Models;
|
||
using ConfectioneryDataModels;
|
||
using ConfectioneryDataModels.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
namespace ConfectioneryDatabaseImplement
|
||
{
|
||
public class ShopStorage : IShopStorage
|
||
{
|
||
|
||
public ShopViewModel? Delete(ShopBindingModel model)
|
||
{
|
||
using var context = new ConfectioneryDatabase();
|
||
var element = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||
if (element != null)
|
||
{
|
||
context.Shops.Remove(element);
|
||
context.SaveChanges();
|
||
return element.GetViewModel;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||
{
|
||
if (!model.Id.HasValue)
|
||
{
|
||
return null;
|
||
}
|
||
using var context = new ConfectioneryDatabase();
|
||
return context.Shops
|
||
.Include(x => x.ShopPastries)
|
||
.ThenInclude(x => x.Pastry)
|
||
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
||
?.GetViewModel;
|
||
}
|
||
|
||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||
{
|
||
if (string.IsNullOrEmpty(model.Name))
|
||
{
|
||
return new();
|
||
}
|
||
using var context = new ConfectioneryDatabase();
|
||
return context.Shops
|
||
.Include(x => x.ShopPastries)
|
||
.ThenInclude(x => x.Pastry)
|
||
.Select(x => x.GetViewModel)
|
||
.Where(x => x.Name.Contains(model.Name ?? string.Empty))
|
||
.ToList();
|
||
}
|
||
|
||
public List<ShopViewModel> GetFullList()
|
||
{
|
||
using var context = new ConfectioneryDatabase();
|
||
return context.Shops
|
||
.Include(x => x.ShopPastries)
|
||
.ThenInclude(x => x.Pastry)
|
||
.Select(shop => shop.GetViewModel)
|
||
.ToList();
|
||
}
|
||
|
||
public ShopViewModel? Insert(ShopBindingModel model)
|
||
{
|
||
using var context = new ConfectioneryDatabase();
|
||
using var transaction = context.Database.BeginTransaction();
|
||
try
|
||
{
|
||
var newShop = Shop.Create(context, model);
|
||
if (newShop == null)
|
||
{
|
||
return null;
|
||
}
|
||
if (context.Shops.Any(x => x.Name == newShop.Name))
|
||
{
|
||
throw new Exception("Не должно быть два магазина с одним названием");
|
||
}
|
||
context.Shops.Add(newShop);
|
||
context.SaveChanges();
|
||
transaction.Commit();
|
||
return newShop.GetViewModel;
|
||
}
|
||
catch
|
||
{
|
||
transaction.Rollback();
|
||
throw;
|
||
}
|
||
}
|
||
|
||
public ShopViewModel? Update(ShopBindingModel model)
|
||
{
|
||
using var context = new ConfectioneryDatabase();
|
||
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||
if (shop == null)
|
||
{
|
||
return null;
|
||
}
|
||
shop.Update(model);
|
||
shop.UpdatePastries(context, model);
|
||
context.SaveChanges();
|
||
return shop.GetViewModel;
|
||
}
|
||
|
||
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
||
{
|
||
using var context = new ConfectioneryDatabase();
|
||
var resultCount = context.Shops
|
||
.Select(shop => shop.Pastries
|
||
.FirstOrDefault(x => x.Key == pastry.Id).Value.Item2)
|
||
.Sum();
|
||
return resultCount >= needCount;
|
||
}
|
||
|
||
public bool SellPastries(IPastryModel pastry, int needCount)
|
||
{
|
||
if (!HasNeedPastries(pastry, needCount))
|
||
{
|
||
return false;
|
||
}
|
||
using var context = new ConfectioneryDatabase();
|
||
foreach (var shop in context.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id)))
|
||
{
|
||
var tuple = shop.Pastries[pastry.Id];
|
||
var diff = Math.Min(tuple.Item2, needCount);
|
||
shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff);
|
||
|
||
needCount -= diff;
|
||
if (needCount <= 0)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
}
|