diff --git a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs index fe07f33..b62d5e5 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs @@ -30,6 +30,8 @@ namespace HardwareShopDatabaseImplement public virtual DbSet Goods { set; get; } + public virtual DbSet GoodComponents { set; get; } + public virtual DbSet Orders { set; get; } public virtual DbSet Purchases { set; get; } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs new file mode 100644 index 0000000..bbd3224 --- /dev/null +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs @@ -0,0 +1,26 @@ +using HardwareShopDatabaseImplement.Models.Storekeeper; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace HardwareShopDatabaseImplement.Models.ManyToMany +{ + public class GoodComponent + { + [Key] + [Column(Order = 1)] + [Required] + public int GoodId { get; set; } + + [Key] + [Column(Order = 2)] + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Good Good { get; set; } = new(); + } +} diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs index e930ffc..f1b4e59 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs @@ -1,9 +1,9 @@ -using HardwareShopDataModels.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.ManyToMany; +using HardwareShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HardwareShopDatabaseImplement.Models.Storekeeper { @@ -11,6 +11,52 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper { public int Id { get; set; } - public virtual User User { get; set; } + [Required] + public string ComponentName { get; set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [Required] + public int UserId { get; set; } + + [ForeignKey("ComponentId")] + public virtual List SushiComponents { get; set; } = new(); + + public virtual User User { get; set; } = null!; + + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost, + UserId = model.UserId + }; + } + + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost, + UserId = UserId, + UserLogin = User.Login + }; } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs index 2f95404..e4679d0 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs @@ -1,10 +1,9 @@ -using HardwareShopDataModels.Enums; +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.ManyToMany; using HardwareShopDataModels.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HardwareShopDatabaseImplement.Models.Storekeeper { @@ -12,6 +11,101 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper { public int Id { get; set; } - public virtual User User { get; set; } + [Required] + public string GoodName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + [Required] + public int UserId { get; set; } + + public virtual User User { get; set; } = null!; + + private Dictionary? _goodComponents = null; + + [ForeignKey("GoodId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("GoodId")] + public virtual List Orders { get; set; } = new(); + + [NotMapped] + public Dictionary GoodComponents + { + get + { + if (_goodComponents == null) + { + _goodComponents = Components + .ToDictionary(recGC => recGC.ComponentId, recGC => (recGC.Component as IComponentModel, recGC.Count)); + } + return _goodComponents; + } + } + + public static Good Create(HardwareShopDatabase context, GoodBindingModel model) + { + return new Good() + { + Id = model.Id, + GoodName = model.GoodName, + Price = model.Price, + UserId = model.UserId, + Components = model.GoodComponents.Select(x => new GoodComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(GoodBindingModel model) + { + GoodName = model.GoodName; + Price = model.Price; + } + + public GoodViewModel GetViewModel => new() + { + Id = Id, + GoodName = GoodName, + Price = Price, + UserId = UserId, + UserLogin = User.Login, + GoodComponents = GoodComponents + }; + + public void UpdateComponents(HardwareShopDatabase context, GoodBindingModel model) + { + var goodComponents = context.GoodComponents + .Where(rec => rec.GoodId == model.Id).ToList(); + if (goodComponents != null && goodComponents.Count > 0) + { // удалили те, которых нет в модели + context.GoodComponents + .RemoveRange(goodComponents + .Where(rec => !model.GoodComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in goodComponents) + { + updateComponent.Count = model.GoodComponents[updateComponent.ComponentId].Item2; + model.GoodComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var good = context.Goods.First(x => x.Id == Id); + foreach (var gc in model.GoodComponents) + { + context.GoodComponents.Add(new GoodComponent + { + Good = good, + Component = context.Components.First(x => x.Id == gc.Key), + Count = gc.Value.Item2 + }); + context.SaveChanges(); + } + _goodComponents = null; + } } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Order.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Order.cs index 6adf3bb..a91c69c 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Order.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Order.cs @@ -1,12 +1,80 @@ -using HardwareShopDataModels.Enums; +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Enums; using HardwareShopDataModels.Models; -using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + namespace HardwareShopDatabaseImplement.Models.Storekeeper { public class Order : IOrderModel { public int Id { get; set; } - public virtual User User { get; set; } + [Required] + public int GoodId { get; set; } + + [Required] + public int UserId { get; set; } + + [Required] + public int Count { get; set; } + + [Required] + public double Sum { get; set; } + + [Required] + public OrderStatus Status { get; set; } + + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public virtual User User { get; set; } = null!; + + public virtual Good Good { get; set; } = null!; + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + GoodId = model.GoodId, + UserId = model.UserId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + GoodId = GoodId, + UserId = UserId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + GoodName = Good.GoodName, + UserLogin = User.Login + }; } }