2023-03-01 00:04:15 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.StoragesContract;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
using ConfectioneryDatabaseImplement.Models;
|
|
|
|
|
using ConfectioneryDataModels;
|
|
|
|
|
using ConfectioneryDataModels.Models;
|
2023-03-01 01:51:24 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-03-01 00:04:15 +04:00
|
|
|
|
|
|
|
|
|
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();
|
2023-03-01 01:51:24 +04:00
|
|
|
|
return context.Shops
|
|
|
|
|
.Include(x => x.ShopPastries)
|
|
|
|
|
.ThenInclude(x => x.Pastry)
|
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
2023-03-01 00:04:15 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new ConfectioneryDatabase();
|
|
|
|
|
return context.Shops
|
2023-03-01 01:51:24 +04:00
|
|
|
|
.Include(x => x.ShopPastries)
|
|
|
|
|
.ThenInclude(x => x.Pastry)
|
2023-03-01 00:04:15 +04:00
|
|
|
|
.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
|
2023-03-01 01:51:24 +04:00
|
|
|
|
.Include(x => x.ShopPastries)
|
|
|
|
|
.ThenInclude(x => x.Pastry)
|
2023-03-01 00:04:15 +04:00
|
|
|
|
.Select(shop => shop.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShopViewModel? Insert(ShopBindingModel model)
|
|
|
|
|
{
|
2023-03-01 01:51:24 +04:00
|
|
|
|
using var context = new ConfectioneryDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2023-03-01 00:04:15 +04:00
|
|
|
|
{
|
2023-03-01 01:51:24 +04:00
|
|
|
|
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;
|
2023-03-01 00:04:15 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2023-03-01 01:51:24 +04:00
|
|
|
|
shop.UpdatePastries(context, model);
|
2023-03-01 00:04:15 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return shop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
|
|
|
|
{
|
2023-03-01 02:38:30 +04:00
|
|
|
|
throw new NotImplementedException();
|
2023-03-01 00:04:15 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SellPastries(IPastryModel pastry, int needCount)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ConfectioneryDatabase();
|
2023-03-01 02:38:30 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
foreach (var sp in context.ShopPastries.Where(x => x.PastryId == pastry.Id))
|
2023-03-01 00:04:15 +04:00
|
|
|
|
{
|
2023-03-01 02:38:30 +04:00
|
|
|
|
var res = Math.Min(needCount, sp.Count);
|
|
|
|
|
sp.Count -= res;
|
|
|
|
|
needCount -= res;
|
|
|
|
|
if (sp.Count == 0) // Изделия больше нет в магазине, значит удаляем его
|
2023-03-01 00:04:15 +04:00
|
|
|
|
{
|
2023-03-01 02:38:30 +04:00
|
|
|
|
context.ShopPastries.Remove(sp);
|
2023-03-01 00:04:15 +04:00
|
|
|
|
}
|
2023-03-01 02:38:30 +04:00
|
|
|
|
if (needCount == 0) // Нельзя коммитить изменения в цикле, что использует контекст, поэтому выходим
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (needCount == 0)
|
|
|
|
|
{
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
2023-03-01 00:04:15 +04:00
|
|
|
|
}
|
2023-03-01 02:38:30 +04:00
|
|
|
|
return needCount == 0;
|
2023-03-01 00:04:15 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|