PIbd-21_Bakalskaya_E.D._Sus.../SushiBarDatabaseImplement/Implements/ShopStorage.cs

261 lines
8.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using SushiBarDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
/// <summary>
/// Добавление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateSushis(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Shops
.Include(x => x.Sushis)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
/// <summary>
/// Продажа изделий
/// </summary>
/// <param name="model"></param>
/// <param name="count"></param>
/// <returns></returns>
public bool SellSushis(ISushiModel model, int count)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shops = context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.ToList()
.Where(x => x.ShopSushis.ContainsKey(model.Id));
foreach (var shop in shops)
{
int countInCurrentShop = shop.ShopSushis[model.Id].Item2;
if (countInCurrentShop <= count)
{
var elem = context.ShopSushis
.Where(x => x.SushiId == model.Id)
.FirstOrDefault(x => x.ShopId == shop.Id);
context.ShopSushis.Remove(elem);
shop.ShopSushis.Remove(model.Id);
count -= countInCurrentShop;
}
else
{
shop.ShopSushis[model.Id] = (shop.ShopSushis[model.Id].Item1, countInCurrentShop - count);
count = 0;
shop.UpdateSushis(context, new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
ShopSushis = shop.ShopSushis,
MaxCountSushis = shop.MaxCountSushis
});
}
if (count <= 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Проверка наличия в нужном количестве
/// </summary>
/// <param name="model"></param>
/// <param name="count"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CheckCountSushi(ISushiModel model, int count)
{
throw new NotImplementedException();
}
/*public bool CheckCountSushi(ISushiModel model, int count)
{
int store = _source.Shops.Select(x => x.ShopSushis.Select(y =>
(y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum();
return store >= count;
}
public bool SellSushis(ISushiModel model, int count)
{
var sushi = _source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (sushi == null || !CheckCountSushi(model, count))
{
throw new ArgumentNullException("Такого количества суш нет, продать столько низя ");
}
foreach (var shop in _source.Shops)
{
var sushis = shop.ShopSushis;
foreach (var elem in sushis.Where(x => x.Value.Item1.Id == sushi.Id))
{
var selling = Math.Min(elem.Value.Item2, count);
sushis[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling);
count -= selling;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = model.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
ShopSushis = sushis,
MaxCountSushis = shop.MaxCountSushis
});
}
_source.SaveShops();
return true;
}*/
}
}