diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..1a15d7e --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -0,0 +1,50 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List ProductComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost + }; + } + public void Update (ComponentBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Cost = model.Cost; + } + + public ComponentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost + }; + + public int StoreKeeperId => throw new NotImplementedException(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..eebf5a7 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -0,0 +1,62 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Enums; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + [Required] + public int ProductId { get; private set; } + public virtual Product? Product { get; set; } + public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + ProductId = model.CannedId, + Product = context.Products.First(x => x.Id == model.ProductId) + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ProductId = ProductId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + ProductName = Product.ProductName + }; + } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..0d67d07 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -0,0 +1,96 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _productComponents = null; + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components + .ToDictionary(c => c.ComponentId, c => + (c.Component as IComponentModel, c.Count)); + } + return _productComponents; + } + } + [ForeignKey("ProductId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ProductId")] + public virtual List Orders { get; set; } = new(); + public static Product Create(ComputerHardwareStoreDBContext context, ProductBindingModel model) + { + return new Product() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Components = model.ProductComponents.Select(x => + new ProductComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ProductBindingModel model) + { + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Price = model.Price; + + } + public ProductViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Price = Price, + ProductComponents = ProductComponents + }; + + public static void UpdateComponents(ComputerHardwareStoreDBContext context, ProductBindingModel model) + { + var productComponents = context.ProductComponents + .Where(pc => pc.ProductId == model.Id) + .ToList(); + if (productComponents.Count != 0 && productComponents.Count > 0) + { + // удалили те, которых нет в модели + context.ProductComponents + .Where(pc => !model.ProductComponents.ContainsKey(pc.ComponentId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productComponents + .ForEach(updateComponent => + { + updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2; + model.ProductComponents.Remove(updateComponent.ComponentId); + }); + } + // добавили новые + context.ProductComponents + .AddRange(model.ProductComponents.Values + .Select(val => new ProductComponent() + { + ProductId = model.Id, + ComponentId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..27d43d4 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +}