diff --git a/GarmentFactory/GarmentFactoryDatabaseImplement/GarmentFactoryDatabase.cs b/GarmentFactory/GarmentFactoryDatabaseImplement/GarmentFactoryDatabase.cs index e0e142d..e8cfb05 100644 --- a/GarmentFactory/GarmentFactoryDatabaseImplement/GarmentFactoryDatabase.cs +++ b/GarmentFactory/GarmentFactoryDatabaseImplement/GarmentFactoryDatabase.cs @@ -22,5 +22,8 @@ namespace GarmentFactoryDatabaseImplement public virtual DbSet Textiles { set; get; } public virtual DbSet TextileComponents { set; get; } public virtual DbSet Orders { set; get; } - } + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopTextiles { set; get; } + } } diff --git a/GarmentFactory/GarmentFactoryDatabaseImplement/Implements/ShopStorage.cs b/GarmentFactory/GarmentFactoryDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..3e3fb8e --- /dev/null +++ b/GarmentFactory/GarmentFactoryDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,146 @@ +using GarmentFactoryContracts.BindingModels; +using GarmentFactoryContracts.SearchModels; +using GarmentFactoryContracts.StoragesContracts; +using GarmentFactoryContracts.ViewModels; +using GarmentFactoryDatabaseImplement.Models; +using GarmentFactoryDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace GarmentFactoryDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new GarmentFactoryDatabase(); + return context.Shops + .Include(x => x.Textiles) + .ThenInclude(x => x.Textile) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new GarmentFactoryDatabase(); + return context.Shops + .Include(x => x.Textiles) + .ThenInclude(x => x.Textile) + .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 GarmentFactoryDatabase(); + return context.Shops + .Include(x => x.Textiles) + .ThenInclude(x => x.Textile) + .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 GarmentFactoryDatabase(); + var newShop = Shop.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 GarmentFactoryDatabase(); + 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.UpdateTextiles(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new GarmentFactoryDatabase(); + var element = context.Shops + .Include(x => x.Textiles) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool MakeSale(ITextileModel model, int count) + { + using var context = new GarmentFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops.Include(x => x.Textiles).ThenInclude(x => x.Textile) + .Where(x => x.Textiles.Any(x => x.TextileId == model.Id)) + .ToList()) + { + var textile = shop.ShopTextiles[model.Id]; + int min = Math.Min(textile.Item2, count); + if (min == textile.Item2) + { + shop.ShopTextiles.Remove(model.Id); + } + else + { + shop.ShopTextiles[model.Id] = (textile.Item1, textile.Item2 - min); + } + shop.UpdateTextiles(context, new() { Id = shop.Id, ShopTextiles = shop.ShopTextiles }); + count -= min; + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Shop.cs b/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..1b679a5 --- /dev/null +++ b/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using GarmentFactoryContracts.BindingModels; +using GarmentFactoryContracts.ViewModels; +using GarmentFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GarmentFactoryDatabaseImplement.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 DateOpening { get; set; } + + [Required] + public int TextilesMaximum { get; set; } + + private Dictionary? _shopTextiles = null; + + [NotMapped] + public Dictionary ShopTextiles + { + get + { + if (_shopTextiles == null) + { + _shopTextiles = Textiles + .ToDictionary(x => x.TextileId, x => (x.Textile as ITextileModel, x.Count)); + } + return _shopTextiles; + } + } + + [ForeignKey("ShopId")] + public virtual List Textiles { get; set; } = new(); + + public static Shop Create(GarmentFactoryDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + TextilesMaximum = model.TextilesMaximum, + Textiles = model.ShopTextiles.Select(x => new ShopTextile + { + Textile = context.Textiles.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; + TextilesMaximum = model.TextilesMaximum; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + TextilesMaximum = TextilesMaximum, + ShopTextiles = ShopTextiles + }; + + public void UpdateTextiles(GarmentFactoryDatabase context, ShopBindingModel model) + { + var shopTextiles = context.ShopTextiles.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopTextiles != null && shopTextiles.Count > 0) + { + context.ShopTextiles.RemoveRange(shopTextiles.Where(rec => !model.ShopTextiles.ContainsKey(rec.TextileId))); + context.SaveChanges(); + foreach (var updateTextile in shopTextiles) + { + if (model.ShopTextiles.ContainsKey(updateTextile.TextileId)) + { + updateTextile.Count = model.ShopTextiles[updateTextile.TextileId].Item2; + model.ShopTextiles.Remove(updateTextile.TextileId); + } + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ic in model.ShopTextiles) + { + context.ShopTextiles.Add(new ShopTextile + { + Shop = shop, + Textile = context.Textiles.First(x => x.Id == ic.Key), + Count = ic.Value.Item2 + }); + context.SaveChanges(); + } + _shopTextiles = null; + } + } +} diff --git a/GarmentFactory/GarmentFactoryDatabaseImplement/Models/ShopTextile.cs b/GarmentFactory/GarmentFactoryDatabaseImplement/Models/ShopTextile.cs new file mode 100644 index 0000000..6429cd2 --- /dev/null +++ b/GarmentFactory/GarmentFactoryDatabaseImplement/Models/ShopTextile.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 GarmentFactoryDatabaseImplement.Models +{ + public class ShopTextile + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int TextileId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Textile Textile { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +} diff --git a/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Textile.cs b/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Textile.cs index 76aa3ce..5fc28fd 100644 --- a/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Textile.cs +++ b/GarmentFactory/GarmentFactoryDatabaseImplement/Models/Textile.cs @@ -34,7 +34,9 @@ namespace GarmentFactoryDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("TextileId")] public virtual List Orders { get; set; } = new(); - public static Textile Create(GarmentFactoryDatabase context, + [ForeignKey("TextileId")] + public virtual List ShopTextiles { get; set; } = new(); + public static Textile Create(GarmentFactoryDatabase context, TextileBindingModel model) { return new Textile()