Промежуточное фиксирование. Добавление классов, связанных с магазином

This commit is contained in:
dasha 2023-03-07 17:20:16 +04:00
parent 84525a17da
commit 522bff8245
6 changed files with 289 additions and 9 deletions

View File

@ -0,0 +1,138 @@
using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
namespace SushiBarDatabaseImplement.Implements
{
internal class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.ListSushi)
.ThenInclude(x => x.Sushi)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.ListSushi)
.ThenInclude(x => x.Sushi)
.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 null;
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.ListSushi)
.ThenInclude(x => x.Sushi)
.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 SushiBarDatabase();
var newShop = Sushi.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 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.UpdateSushi(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Shops
.Include(x => x.ListSushiFk)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool IsEnoughSushi(ISushiModel model, int count)
{
throw new NotImplementedException();
}
public bool SellSushi(ISushiModel model, int count)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
foreach (var shop in context.Shops.Where(x => x.ListSushi.ContainsKey(model.Id)))
{
int countInCurrentShop = shop.ListSushi[model.Id].Item2;
if (countInCurrentShop <= count)
{
shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, 0);
count -= countInCurrentShop;
}
else
{
shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, countInCurrentShop - count);
count = 0;
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -4,6 +4,7 @@ using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using SushiBarDataModels.Models;
namespace SushiBarDatabaseImplement.Implements
{
@ -12,7 +13,7 @@ namespace SushiBarDatabaseImplement.Implements
public List<SushiViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.SushiList
return context.ListSushi
.Include(x => x.Ingredients)
.ThenInclude(x => x.Ingredient)
.ToList()
@ -27,7 +28,7 @@ namespace SushiBarDatabaseImplement.Implements
return new();
}
using var context = new SushiBarDatabase();
return context.SushiList
return context.ListSushi
.Include(x => x.Ingredients)
.ThenInclude(x => x.Ingredient)
.Where(x => x.SushiName.Contains(model.SushiName))
@ -43,7 +44,7 @@ namespace SushiBarDatabaseImplement.Implements
return null;
}
using var context = new SushiBarDatabase();
return context.SushiList
return context.ListSushi
.Include(x => x.Ingredients)
.ThenInclude(x => x.Ingredient)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) ||
@ -59,7 +60,7 @@ namespace SushiBarDatabaseImplement.Implements
{
return null;
}
context.SushiList.Add(newSushi);
context.ListSushi.Add(newSushi);
context.SaveChanges();
return newSushi.GetViewModel;
}
@ -70,7 +71,7 @@ namespace SushiBarDatabaseImplement.Implements
using var transaction = context.Database.BeginTransaction();
try
{
var sushi = context.SushiList.FirstOrDefault(rec => rec.Id == model.Id);
var sushi = context.ListSushi.FirstOrDefault(rec => rec.Id == model.Id);
if (sushi == null)
{
return null;
@ -91,17 +92,27 @@ namespace SushiBarDatabaseImplement.Implements
public SushiViewModel? Delete(SushiBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.SushiList
var element = context.ListSushi
.Include(x => x.Ingredients)
.Include(x => x.Orders)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.SushiList.Remove(element);
context.ListSushi.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool HasSushi(ISushiModel model, int needCount)
{
throw new NotImplementedException();
}
public bool SellSushi(ISushiModel model, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,105 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;
namespace SushiBarDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
[Required]
public string ShopName { get; private set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
[Required]
public DateTime DateOpening { get; private set; }
[Required]
public int MaxCountSushi { get; private set; }
private Dictionary<int, (ISushiModel, int)>? _shopSushi = null;
[NotMapped]
public Dictionary<int, (ISushiModel, int)> ListSushi
{
get
{
if (_shopSushi == null)
{
_shopSushi = ListSushiFk
.ToDictionary(recPC => recPC.SushiId, recPC => (recPC.Sushi as ISushiModel, recPC.Count));
}
return _shopSushi;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopSushi> ListSushiFk { get; set; } = new();
public static Shop Create(SushiBarDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
MaxCountSushi = model.MaxCountSushi,
ListSushiFk = model.ListSushi.Select(x => new ShopSushi
{
Sushi = context.ListSushi.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
MaxCountSushi = model.MaxCountSushi;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
MaxCountSushi = MaxCountSushi,
ListSushi = ListSushi
};
public void UpdateSushi(SushiBarDatabase context, ShopBindingModel model)
{
var shopSushi = context.ShopSushi.Where(rec => rec.SushiId == model.Id).ToList();
if (shopSushi != null && shopSushi.Count > 0)
{ // удалили те, которых нет в модели
context.ShopSushi.RemoveRange(shopSushi.Where(rec => !model.ListSushi.ContainsKey(rec.SushiId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateSushi in shopSushi)
{
updateSushi.Count = model.ListSushi[updateSushi.SushiId].Item2;
model.ListSushi.Remove(updateSushi.SushiId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ss in model.ListSushi)
{
context.ShopSushi.Add(new ShopSushi
{
Shop = shop,
Sushi = context.ListSushi.First(x => x.Id == ss.Key),
Count = ss.Value.Item2
});
context.SaveChanges();
}
_shopSushi = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace SushiBarDatabaseImplement.Models
{
public class ShopSushi
{
public int Id { get; set; }
[Required]
public int SushiId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Sushi Sushi { get; set; } = new();
}
}

View File

@ -81,7 +81,7 @@ namespace SushiBarDatabaseImplement.Models
}
context.SaveChanges();
}
var sushi = context.SushiList.First(x => x.Id == Id);
var sushi = context.ListSushi.First(x => x.Id == Id);
foreach (var si in model.SushiIngredients)
{
context.SushiIngredients.Add(new SushiIngredient

View File

@ -16,10 +16,14 @@ namespace SushiBarDatabaseImplement
public virtual DbSet<Ingredient> Ingredients { set; get; }
public virtual DbSet<Sushi> SushiList { set; get; }
public virtual DbSet<Sushi> ListSushi { set; get; }
public virtual DbSet<SushiIngredient> SushiIngredients { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopSushi> ShopSushi { set; get; }
}
}