diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index 194ce4c..eafd283 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -17,5 +17,7 @@ namespace CarpentryWorkshopDatabaseImplement public virtual DbSet Woods { set; get; } public virtual DbSet WoodComponents { set; get; } public virtual DbSet Orders { set; get; } - } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopWoods { get; set; } + } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..24831e0 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,148 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; +using CarpentryWorkshopDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + 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 (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.Shops + .Include(x => x.Woods) + .ThenInclude(x => x.Wood) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + catch + { + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new CarpentryWorkshopDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + try + { + if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id))) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + shop.Update(model); + shop.UpdateWoods(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + catch + { + throw; + } + } + public bool SellWoods(IWoodModel model, int count) + { + if (model == null) + return false; + using var context = new CarpentryWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + List lst = new List(); + foreach (var el in context.ShopWoods.Where(x => x.WoodId == model.Id)) + { + int dif = count; + if (el.Count < dif) + dif = el.Count; + el.Count -= dif; + count -= dif; + if (el.Count == 0) + { + lst.Add(el); + } + if (count == 0) + break; + } + if (count > 0) + { + transaction.Rollback(); + return false; + } + foreach (var el in lst) + { + context.ShopWoods.Remove(el); + } + context.SaveChanges(); + transaction.Commit(); + return true; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..b8ca036 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,118 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpen { get; set; } + + [Required] + public int WoodMaxCount { get; set; } + + [ForeignKey("ShopId")] + public List Woods { get; set; } = new(); + + private Dictionary? _shopWoods = null; + + [NotMapped] + public Dictionary ShopWoods + { + get + { + if (_shopWoods == null) + { + _shopWoods = Woods + .ToDictionary(x => x.WoodId, x => + (x.Wood as IWoodModel, x.Count)); + } + return _shopWoods; + } + } + + public static Shop? Create(CarpentryWorkshopDatabase context, ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + WoodMaxCount = model.WoodMaxCount, + Woods = model.ShopWoods.Select(x => new ShopWood + { + Wood = context.Woods.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + WoodMaxCount = model.WoodMaxCount; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopWoods = ShopWoods, + WoodMaxCount = WoodMaxCount + }; + + public void UpdateWoods(CarpentryWorkshopDatabase context, + ShopBindingModel model) + { + var shopWoods = context.ShopWoods.Where(rec => + rec.ShopId == model.Id).ToList(); + if (shopWoods != null && shopWoods.Count > 0) + { + context.ShopWoods.RemoveRange(shopWoods.Where(rec => !model.ShopWoods.ContainsKey(rec.WoodId))); + + context.SaveChanges(); + foreach (var updateWood in shopWoods) + { + updateWood.Count = + model.ShopWoods[updateWood.WoodId].Item2; + model.ShopWoods.Remove(updateWood.WoodId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopWoods) + { + context.ShopWoods.Add(new ShopWood + { + Shop = shop, + Wood = context.Woods.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopWoods = null; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs new file mode 100644 index 0000000..d780ec3 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/ShopWood.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class ShopWood + { + public int Id { get; set; } + + [Required] + public int WoodId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Wood Wood { get; set; } = new(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs index b6e21ec..2a9484c 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs @@ -33,8 +33,10 @@ namespace CarpentryWorkshopDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("WoodId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("WoodId")] + public virtual List ShopWoods { get; set; } = new(); - public static Wood? Create(CarpentryWorkshopDatabase context, WoodBindingModel model) + public static Wood? Create(CarpentryWorkshopDatabase context, WoodBindingModel model) { var components = context.Components; return new Wood()