From ce72b6d6b0cb9111f6f45e87ee0a12e07713ec4d Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 20:59:09 +0400 Subject: [PATCH 01/49] + --- .../ComputerHardwareStoreBusinessLogic.csproj | 6 ++ .../ComputerHardwareStoreDBContext.cs | 35 +++++++ ...puterHardwareStoreDatabaseImplement.csproj | 19 ++++ .../Models/Component.cs | 50 ++++++++++ .../Models/Order.cs | 62 ++++++++++++ .../Models/Product.cs | 96 +++++++++++++++++++ .../Models/ProductComponent.cs | 17 ++++ 7 files changed, 285 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj index 47a15a0..982ae40 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs new file mode 100644 index 0000000..9226f42 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -0,0 +1,35 @@ +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement +{ + public class ComputerHardwareStoreDBContext : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + //optionsBuilder.UseNpgsql(@"Host=localhost;Database=ProductBar_db;Username=postgres;Password=postgres"); // не надо >: + /* + * в program добавить: + * // получаем строку подключения из файла конфигурации + * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); + * + * // добавляем контекст ApplicationContext в качестве сервиса в приложение + * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); + * + * в appsettings: + * "ConnectionStrings": { + * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" + * }, + */ + } + base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductComponents { set; get; } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj new file mode 100644 index 0000000..a7da7e9 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + 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(); + } +} From 210d90bb69d929830c66fee2a163e4a8c8a0b817 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 21:02:20 +0400 Subject: [PATCH 02/49] + --- .../Models/Component.cs | 50 ++++++++++ .../Models/Order.cs | 62 ++++++++++++ .../Models/Product.cs | 96 +++++++++++++++++++ .../Models/ProductComponent.cs | 17 ++++ 4 files changed, 225 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs 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(); + } +} From 493886c45a93a1d49d31fcc19d67e23da0f888a3 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 21:02:41 +0400 Subject: [PATCH 03/49] =?UTF-8?q?=D1=84=D0=B8=D0=B3=20=D0=B7=D0=BD=D0=B0?= =?UTF-8?q?=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/OrderLogic.cs | 6 ++-- .../BusinessLogic/PurchaseLogic.cs | 2 +- .../ComputerHardwareStoreBusinessLogic.csproj | 6 ++++ .../BindingModels/OrderBindingModel.cs | 6 ++-- .../{IBuidStorage.cs => IBuildStorage.cs} | 2 +- .../ViewModels/OrderViewModel.cs | 12 +++++-- .../Models/IOrderModel.cs | 8 +++-- .../ComputerHardwareStoreDBContext.cs | 35 +++++++++++++++++++ ...puterHardwareStoreDatabaseImplement.csproj | 19 ++++++++++ 9 files changed, 82 insertions(+), 14 deletions(-) rename ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/{IBuidStorage.cs => IBuildStorage.cs} (94%) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/OrderLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/OrderLogic.cs index 9b4e906..865608c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/OrderLogic.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.BusinessLogicsContracts; using ComputerHardwareStoreContracts.SearchModels; using ComputerHardwareStoreContracts.StorageContracts; using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Enums; using Microsoft.Extensions.Logging; namespace ComputerHardwareStoreBusinessLogic.BusinessLogic @@ -78,7 +79,6 @@ namespace ComputerHardwareStoreBusinessLogic.BusinessLogic model.OrderProduct = element.OrderProduct; model.DateCreate = element.DateCreate; model.Status = element.Status; - model.Cost = element.Cost; if (model.Status != orderStatus - 1) { @@ -106,9 +106,9 @@ namespace ComputerHardwareStoreBusinessLogic.BusinessLogic { return; } - if (model.Cost <= 0) + if (model.Sum <= 0) { - throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Cost)); + throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs index ce693a0..81b183f 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -92,7 +92,7 @@ namespace ComputerHardwareStoreBusinessLogic.BusinessLogic { return; } - if (string.IsNullOrEmpty(model.DateCreate)) + if (model.DateCreate == null) // TODO чего блин, всмысле нет { throw new ArgumentNullException("Нет даты создания покупки", nameof(model.Date)); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj index 47a15a0..982ae40 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs index 720bc7b..9cf80bd 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs @@ -6,12 +6,10 @@ namespace ComputerHardwareStoreContracts.BindingModels public class OrderBindingModel : IOrderModel { public int Id { get; set; } - public int CannedId { get; set; } - public int Count { get; set; } - public double Cost { get; set; } + public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplement { get; set; } + public DateTime? DateImplement { get; set; } = null; public Dictionary OrderProduct { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/IBuidStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/IBuildStorage.cs similarity index 94% rename from ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/IBuidStorage.cs rename to ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/IBuildStorage.cs index fba13ee..a389993 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/IBuidStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/IBuildStorage.cs @@ -4,7 +4,7 @@ using ComputerHardwareStoreContracts.ViewModels; namespace ComputerHardwareStoreContracts.StorageContracts { - public interface IBuidStorage + public interface IBuildStorage { List GetFullList(); List GetFilteredList(BuildSearchModel model); diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs index 02394a4..d2f0ff1 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs @@ -1,15 +1,21 @@ -using ComputerHardwareStoreDataModels.Models; +using ComputerHardwareStoreDataModels.Enums; +using ComputerHardwareStoreDataModels.Models; using System.ComponentModel; namespace ComputerHardwareStoreContracts.ViewModels { public class OrderViewModel : IOrderModel { + [DisplayName("Номер")] public int Id { get; set; } - [DisplayName("Стоимость")] - public double Cost { get; set; } + [DisplayName("Сумма")] + public double Sum { get; set; } + [DisplayName("Статус")] + public OrderStatus Status { get; set; } [DisplayName("Дата создания")] public DateTime DateCreate { get; set; } + [DisplayName("Дата выполнения")] + public DateTime? DateImplement { get; set; } public Dictionary OrderProduct { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs index bddb1f6..06f933b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs @@ -1,9 +1,13 @@ -namespace ComputerHardwareStoreDataModels.Models +using ComputerHardwareStoreDataModels.Enums; + +namespace ComputerHardwareStoreDataModels.Models { public interface IOrderModel : IId { - double Cost { get; } + double Sum { get; } + OrderStatus Status { get; } DateTime DateCreate { get; } + DateTime? DateImplement { get; } public Dictionary OrderProduct { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs new file mode 100644 index 0000000..9226f42 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -0,0 +1,35 @@ +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement +{ + public class ComputerHardwareStoreDBContext : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + //optionsBuilder.UseNpgsql(@"Host=localhost;Database=ProductBar_db;Username=postgres;Password=postgres"); // не надо >: + /* + * в program добавить: + * // получаем строку подключения из файла конфигурации + * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); + * + * // добавляем контекст ApplicationContext в качестве сервиса в приложение + * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); + * + * в appsettings: + * "ConnectionStrings": { + * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" + * }, + */ + } + base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductComponents { set; get; } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj new file mode 100644 index 0000000..a7da7e9 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + From 41fa66267d57286f097ce88e10ab06ac0ff01e73 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 15:57:30 +0400 Subject: [PATCH 04/49] =?UTF-8?q?vendor=20=D1=8F=20=D0=BD=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=88=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerHardwareStore.sln | 6 +++ .../ViewModels/ProductViewModel.cs | 4 +- .../ComputerHardwareStoreDBContext.cs | 3 ++ .../Models/Build.cs | 22 ++++++++ .../Models/Component.cs | 11 ++-- .../Models/Order.cs | 7 +-- .../Models/Product.cs | 3 +- .../Models/StoreKeeper.cs | 52 +++++++++++++++++++ 8 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs diff --git a/ComputerHardwareStore/ComputerHardwareStore.sln b/ComputerHardwareStore/ComputerHardwareStore.sln index 1edd1ac..cb44a3d 100644 --- a/ComputerHardwareStore/ComputerHardwareStore.sln +++ b/ComputerHardwareStore/ComputerHardwareStore.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreContra EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDatabaseImplement", "ComputerHardwareStoreDatabaseImplement\ComputerHardwareStoreDatabaseImplement.csproj", "{09A57BE9-A653-4AAD-9FB2-1F8974F294CD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.Build.0 = Release|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs index 00cc4c2..c1ba122 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs @@ -10,8 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Price { get; set; } - public Dictionary CannedComponents { get; set; } = new(); - - public Dictionary ProductComponents => throw new NotImplementedException(); + public Dictionary ProductComponents { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 9226f42..dac47e6 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -31,5 +31,8 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Components { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } + + public virtual DbSet StoreKeepers { set; get; } + public virtual DbSet Builds { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs new file mode 100644 index 0000000..4d650c3 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -0,0 +1,22 @@ + +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Build : IBuildModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public int VendorId { get; private set; } + public virtual Vendor Vendor { get; private set; } = new(); + + [ForeignKey("BuildId")] + public Dictionary BuildComponents => throw new NotImplementedException(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index 1a15d7e..3089400 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -13,9 +13,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; private set; } = string.Empty; [Required] public double Cost { get; set; } + [Required] + public int StoreKeeperId { get; private set; } + public virtual StoreKeeper? StoreKeeper { get; set; } + [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); - public static Component? Create(ComponentBindingModel model) + public static Component? Create(ComputerHardwareStoreDBContext context, ComponentBindingModel model) { if (model == null) { @@ -25,7 +29,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Models { Id = model.Id, Name = model.Name, - Cost = model.Cost + Cost = model.Cost, + StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeperId) }; } public void Update (ComponentBindingModel model) @@ -44,7 +49,5 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Name = Name, Cost = Cost }; - - public int StoreKeeperId => throw new NotImplementedException(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index eebf5a7..5c08691 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -30,12 +30,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Models 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) }; } @@ -51,12 +49,11 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, - ProductId = ProductId, - Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - ProductName = Product.ProductName }; + + public Dictionary OrderProduct => throw new NotImplementedException(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs index 0d67d07..a7af49c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -23,8 +23,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models if (_productComponents == null) { _productComponents = Components - .ToDictionary(c => c.ComponentId, c => - (c.Component as IComponentModel, c.Count)); + .ToDictionary(c => c.ComponentId, c => (c.Component as IComponentModel, c.Count)); } return _productComponents; } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs new file mode 100644 index 0000000..1c2028d --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -0,0 +1,52 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class StoreKeeper : IStoreKeeperModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + + public string Login { get; private set; } = string.Empty; + [Required] + + public string Password { get; private set; } = string.Empty; + + public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) + { + if (model == null) + { + return null; + } + return new StoreKeeper() + { + Id = model.Id, + Name = model.Name, + Login = model.Login, + Password = model.Password, + }; + } + public void Update(StoreKeeperBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + + public StoreKeeperViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Login = Login, + Password = Password + }; + } +} From 55eade53997510df1a2e6a57e71a6fc995b867bf Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 17:05:42 +0400 Subject: [PATCH 05/49] + --- .../ComputerHardwareStore.sln | 8 +++- .../BindingModels/OrderBindingModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../Models/IOrderModel.cs | 2 +- .../ComputerHardwareStoreDBContext.cs | 1 + .../Models/Order.cs | 38 ++++++++++++++----- .../Models/OrderProduct.cs | 17 +++++++++ 7 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs diff --git a/ComputerHardwareStore/ComputerHardwareStore.sln b/ComputerHardwareStore/ComputerHardwareStore.sln index 1edd1ac..1f6cda1 100644 --- a/ComputerHardwareStore/ComputerHardwareStore.sln +++ b/ComputerHardwareStore/ComputerHardwareStore.sln @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDataMo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreContracts", "ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj", "{5F394E21-2597-432B-AE73-BBAFD8D9F50E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDatabaseImplement", "ComputerHardwareStoreDatabaseImplement\ComputerHardwareStoreDatabaseImplement.csproj", "{93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.Build.0 = Release|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs index 9cf80bd..e1017a0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } = null; - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs index d2f0ff1..a0b6602 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs @@ -16,6 +16,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public DateTime DateCreate { get; set; } [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs index 06f933b..3e7f94e 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs @@ -8,6 +8,6 @@ namespace ComputerHardwareStoreDataModels.Models OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } - public Dictionary OrderProduct { get; } + public Dictionary OrderProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 9226f42..850515c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -31,5 +31,6 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Components { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } + public virtual DbSet OrderProducts { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index eebf5a7..1754b65 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Enums; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -18,9 +19,25 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [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; } + + private Dictionary? _orderProducts = null; + [NotMapped] + public Dictionary OrderProducts + { + get + { + if (_orderProducts == null) + { + _orderProducts = Products + .ToDictionary( + op => op.ProductId, + op => (op.Product as IProductModel, op.Count)); + } + return _orderProducts; + } + } + [ForeignKey("OrderId")] + public virtual List Products { get; set; } = new(); public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model) { if (model == null) @@ -30,13 +47,18 @@ namespace ComputerHardwareStoreDatabaseImplement.Models 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) + Products = model.OrderProducts + .Select(op => new OrderProduct() + { + OrderId = model.Id, + ProductId = op.Key, + Count = op.Value.Item2 + }) + .ToList() }; } public void Update(OrderBindingModel model) @@ -51,12 +73,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, - ProductId = ProductId, - Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - ProductName = Product.ProductName }; } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs new file mode 100644 index 0000000..9930fe7 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class OrderProduct + { + public int Id { get; set; } + [Required] + public int OrderId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Product Product { get; set; } = new(); + public virtual Order Order { get; set; } = new(); + } +} From 54afeb29066ae557cfde2cf9365acf0d09168440 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 17:12:13 +0400 Subject: [PATCH 06/49] + --- ComputerHardwareStore/ComputerHardwareStore.sln | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ComputerHardwareStore/ComputerHardwareStore.sln b/ComputerHardwareStore/ComputerHardwareStore.sln index eb002a7..282ca90 100644 --- a/ComputerHardwareStore/ComputerHardwareStore.sln +++ b/ComputerHardwareStore/ComputerHardwareStore.sln @@ -35,6 +35,10 @@ Global {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.Build.0 = Release|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {09A57BE9-A653-4AAD-9FB2-1F8974F294CD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 2643f53e0b1483f967e90999bb9467602b55b417 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 17:16:45 +0400 Subject: [PATCH 07/49] Vendor --- .../Models/Vendor.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs new file mode 100644 index 0000000..bc9567d --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs @@ -0,0 +1,52 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Vendor : IVendorModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + + public string Login { get; private set; } = string.Empty; + [Required] + + public string Password { get; private set; } = string.Empty; + + public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model) + { + if (model == null) + { + return null; + } + return new Vendor() + { + Id = model.Id, + Name = model.Name, + Login = model.Login, + Password = model.Password, + }; + } + public void Update(VendorBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + + public VendorViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Login = Login, + Password = Password + }; + } +} From 4391a40a78a5f193310b25e08bd8cb34dfb439be Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 18:16:30 +0400 Subject: [PATCH 08/49] =?UTF-8?q?=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=83=20=D0=BA=D0=BB=D0=B0=D0=B4=D0=BE=D0=B2=D1=89=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BD=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/StoreKeeper.cs | 6 ++++++ .../ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs index 1c2028d..2143e5b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -2,6 +2,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -16,6 +17,11 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public string Password { get; private set; } = string.Empty; + [ForeignKey("StoreKeeperId")] + public virtual List Products { get; set; } = new(); + [ForeignKey("StoreKeeperId")] + public virtual List Components { get; set; } = new(); + public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) { diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs index bc9567d..4465a20 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs @@ -2,6 +2,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -16,6 +17,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public string Password { get; private set; } = string.Empty; + [ForeignKey("VendorId")] + public virtual List Purchases { get; set; } = new(); + [ForeignKey("VendorId")] + public virtual List Builds { get; set; } = new(); public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model) { From 733c40a85a6a30577a49b03e238b0ac393b7c6f8 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 18:59:45 +0400 Subject: [PATCH 09/49] =?UTF-8?q?=D0=A1=D0=B1=D0=BE=D1=80=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/BuildBindingModel.cs | 1 + .../ViewModels/BuildViewModel.cs | 9 +-- .../ViewModels/CommentViewModel.cs | 1 - .../Models/IBuildModel.cs | 1 + .../Models/ICommentModel.cs | 1 - .../ComputerHardwareStoreDBContext.cs | 3 +- .../Models/Build.cs | 56 +++++++++++++++++-- .../Models/BuildComponent.cs | 22 ++++++++ .../Models/Comment.cs | 48 ++++++++++++++++ 9 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs index e437bec..60ae196 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs @@ -9,5 +9,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public double Price { get; set; } public int VendorId { get; set; } public Dictionary BuildComponents { get; set; } = new(); + public List Comments { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs index 763cf75..0ddafa9 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs @@ -7,10 +7,11 @@ namespace ComputerHardwareStoreContracts.ViewModels { public int Id { get; set; } [DisplayName("Название сборки")] - public string Name { get; } = string.Empty; + public string Name { get; set; } = string.Empty; [DisplayName("Стоимость")] - public double Price { get; } - public int VendorId { get; } - public Dictionary BuildComponents { get; } = new(); + public double Price { get; set; } + public int VendorId { get; set; } + public Dictionary BuildComponents { get; set; } = new(); + public List Comments { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs index b62b98c..0978a2f 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs @@ -7,6 +7,5 @@ namespace ComputerHardwareStoreContracts.ViewModels public int Id { get; set; } public DateTime Date { get; set; } public string Text { get; set; } = string.Empty; - public int BuildId { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs index 6485ba0..6ae15ed 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs @@ -6,5 +6,6 @@ double Price { get; } int VendorId { get; } public Dictionary BuildComponents { get; } + public List Comments { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs index 76d1cdd..da32990 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs @@ -4,6 +4,5 @@ { DateTime Date { get; } string Text { get; } - int BuildId { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 2b99985..84fa66a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -31,9 +31,10 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Components { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } - public virtual DbSet StoreKeepers { set; get; } public virtual DbSet Builds { set; get; } public virtual DbSet OrderProducts { set; get; } + public virtual DbSet BuildComponents { set; get; } + public virtual DbSet Comments { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 4d650c3..38276ab 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -1,22 +1,68 @@  +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Runtime.InteropServices; namespace ComputerHardwareStoreDatabaseImplement.Models { public class Build : IBuildModel { - public int Id { get; private set; } + public int Id { get; set; } [Required] - public string Name { get; private set; } = string.Empty; + public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } [Required] - public int VendorId { get; private set; } + public int VendorId { get; set; } public virtual Vendor Vendor { get; private set; } = new(); - + private Dictionary? _buildComponents = null; + [NotMapped] + public Dictionary BuildComponents + { + get + { + if (_buildComponents == null) + { + _buildComponents = Components + .ToDictionary(bc => bc.ComponentId, bc => + (bc.Component as IComponentModel, bc.Count)); + } + return _buildComponents; + } + } [ForeignKey("BuildId")] - public Dictionary BuildComponents => throw new NotImplementedException(); + public virtual List Components { get; set; } = new(); + [ForeignKey("BuildId")] + public virtual List Comments { get; set; } = new(); + [NotMapped] + List IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList(); + + public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + VendorId = model.VendorId, + }; + } + + public BuildViewModel GetViewModel() => new() + { + Id = Id, + Name = Name, + Price = Price, + VendorId = VendorId, + Comments = Comments.Select(c => c as ICommentModel).ToList(), + BuildComponents = BuildComponents + }; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs new file mode 100644 index 0000000..4f98220 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class BuildComponent + { + public int Id { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Build Build { get; set; } = new(); + public virtual Component Component { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs new file mode 100644 index 0000000..dc10685 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs @@ -0,0 +1,48 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Comment : ICommentModel + { + public int Id { get; set; } + [Required] + public DateTime Date { get; set; } + [Required] + public string Text { get; set; } = string.Empty; + [Required] + public int BuildId { get; set; } + public static Comment? Create(CommentBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Date = model.Date, + Text = model.Text, + BuildId = model.BuildId, + }; + } + + public void Update(CommentBindingModel model) + { + if (model == null) + { + return; + } + Text = model.Text; + } + + public CommentViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + Text = Text, + }; + } +} From c01e3c916d8f002ab062a6f9df0206925352229e Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 19:12:40 +0400 Subject: [PATCH 10/49] =?UTF-8?q?Purchase=20=D0=B8=20=D1=81=D0=B2=D1=8F?= =?UTF-8?q?=D0=B7=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/PurchaseBindingModel.cs | 4 +- .../ViewModels/PurchaseViewModel.cs | 4 +- .../Models/IPurchaseModel.cs | 4 +- .../ComputerHardwareStoreDBContext.cs | 3 + .../Models/Purchase.cs | 151 ++++++++++++++++++ .../Models/PurchaseBuild.cs | 18 +++ .../Models/PurchaseProduct.cs | 18 +++ 7 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index af08205..df458d5 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -8,7 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs index d0de94d..7e159b7 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs @@ -13,8 +13,8 @@ namespace ComputerHardwareStoreContracts.ViewModels public int VendorId { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 9afa7b6..39dd9e9 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -5,7 +5,7 @@ int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } - public Dictionary PurchaseBuild { get; } - public Dictionary PurchaseProduct { get; } + public Dictionary PurchaseBuilds { get; } + public Dictionary PurchaseProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 84fa66a..6d081d6 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -36,5 +36,8 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } + public virtual DbSet Purchases { set; get; } + public virtual DbSet PurchaseBuilds { set; get; } + public virtual DbSet PurchaseProducts { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs new file mode 100644 index 0000000..3c5c9f0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -0,0 +1,151 @@ + +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 Purchase : IPurchaseModel + { + public int Id { get; private set; } + [Required] + public int VendorId { get; set; } + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + private Dictionary? _purchaseBuilds = null; + [NotMapped] + public Dictionary PurchaseBuilds + { + get + { + if (_purchaseBuilds == null) + { + _purchaseBuilds = Builds + .ToDictionary(op => op.BuildId, op => (op.Build as IBuildModel, op.Count)); + } + return _purchaseBuilds; + } + } + + [ForeignKey("PurchaseId")] + public virtual List Builds { get; set; } = new(); + private Dictionary? _purchaseProducts = null; + [NotMapped] + public Dictionary PurchaseProducts + { + get + { + if (_purchaseProducts == null) + { + _purchaseProducts = Products + .ToDictionary(op => op.ProductId, op => (op.Product as IProductModel, op.Count)); + } + return _purchaseProducts; + } + } + [ForeignKey("PurchaseId")] + public virtual List Products { get; set; } = new(); + + public static Purchase Create(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + return new Purchase() + { + Id = model.Id, + Cost = model.Cost, + DateCreate = model.DateCreate, + Builds = model.PurchaseBuilds.Select(x => + new PurchaseBuild + { + Build = context.Builds.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + Products = model.PurchaseProducts.Select(x => + new PurchaseProduct + { + Product = context.Products.First(z => z.Id == z.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PurchaseBindingModel model) + { + Cost = model.Cost; + + } + public PurchaseViewModel GetViewModel => new() + { + Id = Id, + Cost = Cost, + DateCreate = DateCreate, + PurchaseBuilds = PurchaseBuilds, + PurchaseProducts = PurchaseProducts + }; + + public static void UpdateBuilds(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productBuilds = context.PurchaseBuilds + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productBuilds.Count != 0 && productBuilds.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseBuilds + .Where(pc => !model.PurchaseBuilds.ContainsKey(pc.BuildId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productBuilds + .ForEach(updateBuild => + { + updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2; + model.PurchaseBuilds.Remove(updateBuild.BuildId); + }); + } + // добавили новые + context.PurchaseBuilds + .AddRange(model.PurchaseBuilds.Values + .Select(val => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + + public static void UpdateProducts(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productProducts = context.PurchaseProducts + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productProducts.Count != 0 && productProducts.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseProducts + .Where(pc => !model.PurchaseProducts.ContainsKey(pc.ProductId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productProducts + .ForEach(updateProduct => + { + updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2; + model.PurchaseProducts.Remove(updateProduct.ProductId); + }); + } + // добавили новые + context.PurchaseProducts + .AddRange(model.PurchaseProducts.Values + .Select(val => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs new file mode 100644 index 0000000..2f5c1af --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseBuild + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Build Build { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs new file mode 100644 index 0000000..2bc9116 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseProduct + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} From cc389d7922f638251c532e55d44c82a2787a9526 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 19:43:16 +0400 Subject: [PATCH 11/49] cyjdf c,jhrf --- .../BindingModels/BuildBindingModel.cs | 2 +- .../ViewModels/BuildViewModel.cs | 2 +- .../Models/IBuildModel.cs | 2 +- .../ComputerHardwareStoreDBContext.cs | 1 + .../Models/Build.cs | 22 ++++++++++++++----- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs index 60ae196..e52972b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs @@ -7,7 +7,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Price { get; set; } - public int VendorId { get; set; } + public IVendorModel Vendor { get; set; } public Dictionary BuildComponents { get; set; } = new(); public List Comments { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs index 0ddafa9..8274f6d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs @@ -10,7 +10,7 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Стоимость")] public double Price { get; set; } - public int VendorId { get; set; } + public IVendorModel Vendor { get; set; } public Dictionary BuildComponents { get; set; } = new(); public List Comments { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs index 6ae15ed..dee1777 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs @@ -4,7 +4,7 @@ { string Name { get; } double Price { get; } - int VendorId { get; } + IVendorModel Vendor { get; } public Dictionary BuildComponents { get; } public List Comments { get; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 84fa66a..8dac2da 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -36,5 +36,6 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } + public virtual DbSet Vendors { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 38276ab..db68236 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -2,6 +2,7 @@ using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Runtime.InteropServices; @@ -15,9 +16,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } - [Required] - public int VendorId { get; set; } - public virtual Vendor Vendor { get; private set; } = new(); + private Dictionary? _buildComponents = null; [NotMapped] public Dictionary BuildComponents @@ -36,10 +35,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [ForeignKey("BuildId")] public virtual List Components { get; set; } = new(); [ForeignKey("BuildId")] + public virtual List Comments { get; set; } = new(); [NotMapped] List IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList(); + public virtual Vendor Vendor { get; private set; } = new(); + IVendorModel IBuildModel.Vendor => Vendor as IVendorModel; + public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model) { if (model == null) @@ -51,7 +54,16 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Price = model.Price, - VendorId = model.VendorId, + Vendor = context.Vendors.First(v => v.Id == model.Vendor.Id), + Components = context.Components + .Where(c => model.BuildComponents.ContainsKey(c.Id)) + .Select(c => new BuildComponent() + { + BuildId = model.Id, + ComponentId = c.Id, + Component = c, + Count = model.BuildComponents[c.Id].Item2 + }).ToList() }; } @@ -60,7 +72,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = Id, Name = Name, Price = Price, - VendorId = VendorId, + Vendor = Vendor, Comments = Comments.Select(c => c as ICommentModel).ToList(), BuildComponents = BuildComponents }; From a30de13f4e1dc18d42df768744a12141fe407667 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 19:49:13 +0400 Subject: [PATCH 12/49] product mtm --- .../Models/Product.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs index a7af49c..023758a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -39,12 +39,15 @@ namespace ComputerHardwareStoreDatabaseImplement.Models 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() + Components = context.Components + .Where(c => model.ProductComponents.ContainsKey(c.Id)) + .Select(c => new ProductComponent() + { + ProductId = model.Id, + ComponentId = c.Id, + Component = c, + Count = model.ProductComponents[c.Id].Item2 + }).ToList(), }; } public void Update(ProductBindingModel model) From 2a30f036825cd107424334bd34e1585ffa5ecc8a Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 19:52:50 +0400 Subject: [PATCH 13/49] =?UTF-8?q?cleaning=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B1=D0=B5=D0=B7=20=D1=83=D1=81=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerHardwareStoreDBContext.cs | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 8671a3d..506d15d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -7,23 +7,20 @@ namespace ComputerHardwareStoreDatabaseImplement { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - if (optionsBuilder.IsConfigured == false) - { - //optionsBuilder.UseNpgsql(@"Host=localhost;Database=ProductBar_db;Username=postgres;Password=postgres"); // не надо >: - /* - * в program добавить: - * // получаем строку подключения из файла конфигурации - * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); - * - * // добавляем контекст ApplicationContext в качестве сервиса в приложение - * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); - * - * в appsettings: - * "ConnectionStrings": { - * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" - * }, - */ - } + /* + * в program добавить: + * // получаем строку подключения из файла конфигурации + * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); + * + * // добавляем контекст ApplicationContext в качестве сервиса в приложение + * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); + * + * в appsettings: + * "ConnectionStrings": { + * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" + * }, + */ + base.OnConfiguring(optionsBuilder); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); From af397abdac0975d12edd8d393aa5b5e3fc4108a5 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 19:53:37 +0400 Subject: [PATCH 14/49] + order --- .../ComputerHardwareStoreDBContext.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 9c53efb..8671a3d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -33,6 +33,7 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet ProductComponents { set; get; } public virtual DbSet StoreKeepers { set; get; } public virtual DbSet Builds { set; get; } + public virtual DbSet Orders { set; get; } public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } From 4fe042017b1de4ce1ceeca24d064865559af1350 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:06:35 +0400 Subject: [PATCH 15/49] =?UTF-8?q?=D0=BF=D1=83=D1=80=D1=87=D0=B0=D0=B9?= =?UTF-8?q?=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ComponentBindingModel.cs | 2 +- .../ViewModels/ComponentViewModel.cs | 2 +- .../Models/IComponentModel.cs | 2 +- .../Models/Component.cs | 5 +-- .../Models/Purchase.cs | 31 ++++++++++++------- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs index af0759b..e14244d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs @@ -7,6 +7,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs index 6aadb04..eb9fd9a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs index 6eea153..84f259d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs @@ -4,6 +4,6 @@ { string Name { get; } double Cost { get; } - int StoreKeeperId { get; } + IStoreKeeperModel StoreKeeper { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index 3089400..e26f654 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -14,8 +14,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public double Cost { get; set; } [Required] - public int StoreKeeperId { get; private set; } public virtual StoreKeeper? StoreKeeper { get; set; } + [NotMapped] + IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); @@ -30,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeperId) + StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) }; } public void Update (ComponentBindingModel model) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs index 3c5c9f0..3a60436 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -58,18 +58,25 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Cost = model.Cost, DateCreate = model.DateCreate, - Builds = model.PurchaseBuilds.Select(x => - new PurchaseBuild - { - Build = context.Builds.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList(), - Products = model.PurchaseProducts.Select(x => - new PurchaseProduct - { - Product = context.Products.First(z => z.Id == z.Key), - Count = x.Value.Item2 - }).ToList() + Builds = context.Builds + .Where(b => model.PurchaseBuilds.ContainsKey(b.Id)) + .Select(b => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = b.Id, + Build = b, + Count = model.PurchaseProducts[b.Id].Item2 + }).ToList(), + Products = context.Products + .Where(p => model.PurchaseProducts.ContainsKey(p.Id)) + .Select (p => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = p.Id, + Product = p, + Count = model.PurchaseProducts[p.Id].Item2 + }) + .ToList() }; } public void Update(PurchaseBindingModel model) From fb7db4b90bdfdd5cb6b8cb1774733f74b86b2c2d Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:11:52 +0400 Subject: [PATCH 16/49] op --- .../Models/Order.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index fcf83e4..bf50b41 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -51,12 +51,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, - Products = model.OrderProducts - .Select(op => new OrderProduct() + Products = context.Products + .Where(p => model.OrderProducts.ContainsKey(p.Id)) + .Select(p => new OrderProduct() { OrderId = model.Id, - ProductId = op.Key, - Count = op.Value.Item2 + ProductId = p.Id, + Product = p, + Count = model.OrderProducts[p.Id].Item2 }) .ToList() }; @@ -78,7 +80,5 @@ namespace ComputerHardwareStoreDatabaseImplement.Models DateCreate = DateCreate, DateImplement = DateImplement, }; - - public Dictionary OrderProduct => throw new NotImplementedException(); } } \ No newline at end of file From 19d31583425c4b143161fc9ae38a9f663919467c Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:27:11 +0400 Subject: [PATCH 17/49] componentStorage --- .../Implements/ComponentStorage.cs | 85 +++++++++++++++++++ .../Models/Build.cs | 10 +++ .../Models/StoreKeeper.cs | 2 - 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..5041064 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new (); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) + && x.Name == model.Name) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComponent = Component.Create(context, model); + if (newComponent == null) + { + return null; + } + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index db68236..1675e6a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -67,6 +67,16 @@ namespace ComputerHardwareStoreDatabaseImplement.Models }; } + public void Update(BuildBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Price = model.Price; + } + public BuildViewModel GetViewModel() => new() { Id = Id, diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs index 2143e5b..1fd691b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -15,14 +15,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Login { get; private set; } = string.Empty; [Required] - public string Password { get; private set; } = string.Empty; [ForeignKey("StoreKeeperId")] public virtual List Products { get; set; } = new(); [ForeignKey("StoreKeeperId")] public virtual List Components { get; set; } = new(); - public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) { if (model == null) From db1fdee078bea3f89672a1350c14f3aae572521b Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:42:42 +0400 Subject: [PATCH 18/49] productStorage --- .../Implements/ComponentStorage.cs | 29 +++---- .../Implements/ProductStorage.cs | 85 +++++++++++++++++++ 2 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs index 5041064..5bb8224 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -12,7 +12,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { using var context = new ComputerHardwareStoreDBContext(); return context.Components - .Select(x => x.GetViewModel) + .Select(c => c.GetViewModel) .ToList(); } @@ -20,12 +20,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { if (string.IsNullOrEmpty(model.Name)) { - return new (); + return new(); } using var context = new ComputerHardwareStoreDBContext(); return context.Components - .Where(x => x.Name.Contains(model.Name)) - .Select(x => x.GetViewModel) + .Where(c => c.Name.Contains(model.Name)) + .Select(c => c.GetViewModel) .ToList(); } @@ -37,10 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Components - .FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Name) - && x.Name == model.Name) - || (model.Id.HasValue && x.Id == model.Id)) + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c .Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) ?.GetViewModel; } @@ -59,7 +58,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Update(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + var component = context.Components.FirstOrDefault(c => c.Id == model.Id); if (component == null) { return null; @@ -72,14 +71,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Delete(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + var element = context.Components.FirstOrDefault(c => c.Id == model.Id); + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..a5face0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,85 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Where(p => p.Name.Contains(model.Name)) + .Select(p => p.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) + ?.GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return product.GetViewModel; + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Products.Remove(product); + context.SaveChanges(); + return product.GetViewModel; + } + } +} From 5337beeb659e0d7e02937542640634b4242cf909 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 21:00:26 +0400 Subject: [PATCH 19/49] productStorage --- .../Implements/ProductStorage.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index a5face0..9c74297 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.SearchModels; using ComputerHardwareStoreContracts.StorageContracts; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace ComputerHardwareStoreDatabaseImplement.Implements { @@ -12,6 +13,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { using var context = new ComputerHardwareStoreDBContext(); return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) .ToList(); } @@ -25,6 +28,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements using var context = new ComputerHardwareStoreDBContext(); return context.Products .Where(p => p.Name.Contains(model.Name)) + .Include(p => p.Components) + .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) .ToList(); } @@ -37,10 +42,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) .FirstOrDefault(c => (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || - (model.Id.HasValue && c.Id == model.Id)) - ?.GetViewModel; + (model.Id.HasValue && c.Id == model.Id))? + .GetViewModel; } public ProductViewModel? Insert(ProductBindingModel model) @@ -53,13 +60,20 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } context.Products.Add(newProduct); context.SaveChanges(); - return newProduct.GetViewModel; + return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == newProduct.Id)? + .GetViewModel; } public ProductViewModel? Update(ProductBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); if (product == null) { return null; @@ -72,7 +86,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ProductViewModel? Delete(ProductBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); if (product == null) { return null; From 1ae74a6fdb7f32db08b2315778c67a71e7b1cdb0 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:04:41 +0400 Subject: [PATCH 20/49] OrderStorage --- .../Implements/OrderStorage.cs | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..2378d44 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,108 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .Select(o => o.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .Where(o => + (model.Id.HasValue && o.Id == model.Id) || + ((model.DateFrom.HasValue && model.DateTo.HasValue) && + ((model.DateFrom <= o.DateCreate && o.DateCreate <= model.DateTo) || + (o.DateImplement.HasValue && o.DateCreate < model.DateFrom && model.DateFrom < o.DateImplement))) + ) + .Select(o => o.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id)? + .GetViewModel; + + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + using var transaction = context.Database.BeginTransaction(); + try + { + var order = context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + transaction.Commit(); + return order.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} From e249402043a29262afb35db9bc593beaae5a9d21 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Wed, 1 May 2024 11:09:52 +0400 Subject: [PATCH 21/49] =?UTF-8?q?comment,=20storekeeper,=20vendor=20+=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2?= =?UTF-8?q?=20=D0=B8=D1=85=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/CommentSearchModel.cs | 1 - .../SearchModels/ProductSearchModel.cs | 2 + .../SearchModels/StoreKeeperSearchModel.cs | 1 - .../Implements/CommentStorage.cs | 77 +++++++++++++++++ .../Implements/ComponentStorage.cs | 8 +- .../Implements/StoreKeeperStorage.cs | 86 +++++++++++++++++++ .../Implements/Vendor.cs | 84 ++++++++++++++++++ .../Models/Component.cs | 8 +- 8 files changed, 257 insertions(+), 10 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs index 1378964..3dcc2c0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs @@ -3,6 +3,5 @@ public class CommentSearchModel { public int? Id { get; set; } - public DateTime? Date { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 7944de9..7c72d29 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,5 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } + public int? VendorId { get; set; } + public int? Build { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs index ed748fe..21c4265 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs @@ -4,6 +4,5 @@ { public int? Id { get; set; } public string? Login { get; set; } - public string? Password { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs new file mode 100644 index 0000000..a357455 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs @@ -0,0 +1,77 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class CommentStorage : ICommentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CommentSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public CommentViewModel? GetElement(CommentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public CommentViewModel? Insert(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComment = Comment.Create(model); + if (newComment == null) + { + return null; + } + context.Comments.Add(newComment); + context.SaveChanges(); + return newComment.GetViewModel; + } + public CommentViewModel? Update(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + + public CommentViewModel? Delete(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Comments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs index 5bb8224..9fdc559 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -58,14 +58,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Update(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var component = context.Components.FirstOrDefault(c => c.Id == model.Id); - if (component == null) + var element = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (element == null) { return null; } - component.Update(model); + element.Update(model); context.SaveChanges(); - return component.GetViewModel; + return element.GetViewModel; } public ComponentViewModel? Delete(ComponentBindingModel model) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs new file mode 100644 index 0000000..bd4aff8 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs @@ -0,0 +1,86 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class StoreKeeperStorage : IStoreKeeperStorage + { + public StoreKeeperViewModel? GetElement(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Include(x => x.Components) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StoreKeeperViewModel? Insert(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newStoreKeeper = StoreKeeper.Create(context, model); + if (newStoreKeeper == null) + { + return null; + } + context.StoreKeepers.Add(newStoreKeeper); + context.SaveChanges(); + return newStoreKeeper.GetViewModel; + } + + public StoreKeeperViewModel? Update(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public StoreKeeperViewModel? Delete(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.StoreKeepers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs new file mode 100644 index 0000000..1e74a2a --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs @@ -0,0 +1,84 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class VendorStorage : IVendorStorage + { + public VendorViewModel? GetElement(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Select(x => x.GetViewModel) + .ToList(); + } + + public VendorViewModel? Insert(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newVendor = Vendor.Create(context, model); + if (newVendor == null) + { + return null; + } + context.Vendors.Add(newVendor); + context.SaveChanges(); + return newVendor.GetViewModel; + } + + public VendorViewModel? Update(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public VendorViewModel? Delete(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Vendors.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index e26f654..4acbece 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -14,13 +14,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public double Cost { get; set; } [Required] - public virtual StoreKeeper? StoreKeeper { get; set; } + public virtual StoreKeeper? StoreKeeperЗЛ { get; set; } [NotMapped] - IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); + public IStoreKeeperModel? StoreKeeper { get; set; } [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); - public static Component? Create(ComputerHardwareStoreDBContext context, ComponentBindingModel model) + public static Component? Create(ComponentBindingModel model) { if (model == null) { @@ -31,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) + StoreKeeper = model.StoreKeeper }; } public void Update (ComponentBindingModel model) From 09dcc021c8f907e005ffa5ab7eae129032e7585e Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:35:35 +0400 Subject: [PATCH 22/49] build storage --- .../Implements/BuildStorage.cs | 107 ++++++++++++++++++ .../Implements/ProductStorage.cs | 6 +- .../Models/Build.cs | 8 +- 3 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs new file mode 100644 index 0000000..72c2a4b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -0,0 +1,107 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class BuildStorage : IBuildStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Select(b => b.GetViewModel) + .ToList(); + } + + public List GetFilteredList(BuildSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Where(b => b.Name.Contains(model.Name)) + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Select(b => b.GetViewModel) + .ToList(); + } + + public BuildViewModel? GetElement(BuildSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(b => + (!string.IsNullOrEmpty(model.Name) && b.Name == model.Name) || + (model.Id.HasValue && b.Id == model.Id))? + .GetViewModel; + } + + public BuildViewModel? Insert(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newBuild = Build.Create(context, model); + if (newBuild == null) + { + return null; + } + context.Builds.Add(newBuild); + context.SaveChanges(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == newBuild.Id)? + .GetViewModel; + } + + public BuildViewModel? Update(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var build = context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == model.Id); + if (build == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return build.GetViewModel; + } + public BuildViewModel? Delete(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var build = context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == model.Id); + if (build == null) + { + return null; + } + context.Builds.Remove(build); + context.SaveChanges(); + return build.GetViewModel; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index 9c74297..ae83b6b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -44,9 +44,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements return context.Products .Include(p => p.Components) .ThenInclude(p => p.Component) - .FirstOrDefault(c => - (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || - (model.Id.HasValue && c.Id == model.Id))? + .FirstOrDefault(p => + (!string.IsNullOrEmpty(model.Name) && p.Name == model.Name) || + (model.Id.HasValue && p.Id == model.Id))? .GetViewModel; } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 1675e6a..c4c8f29 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -1,11 +1,9 @@ - -using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Runtime.InteropServices; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -16,7 +14,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } - + [NotMapped] private Dictionary? _buildComponents = null; [NotMapped] public Dictionary BuildComponents @@ -77,7 +75,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Price = model.Price; } - public BuildViewModel GetViewModel() => new() + public BuildViewModel GetViewModel => new() { Id = Id, Name = Name, From 96c186a5906be43eab312fb08c7d13adeb907890 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:55:41 +0400 Subject: [PATCH 23/49] PurchaseStorage --- .../BindingModels/PurchaseBindingModel.cs | 1 + .../ViewModels/PurchaseViewModel.cs | 3 +- .../Models/IPurchaseModel.cs | 1 + .../Implements/PurchaseStorage.cs | 117 ++++++++++++++++++ .../Models/Purchase.cs | 1 + 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index df458d5..e28c326 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -8,6 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } public Dictionary PurchaseBuilds { get; set; } = new(); public Dictionary PurchaseProducts { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs index 7e159b7..4911e1d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs @@ -10,11 +10,12 @@ namespace ComputerHardwareStoreContracts.ViewModels public double Cost { get; set; } [DisplayName("Дата создания")] public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } public int VendorId { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } public Dictionary PurchaseBuilds { get; set; } = new(); - public Dictionary PurchaseProducts { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 39dd9e9..efc0095 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -5,6 +5,7 @@ int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } + DateTime? DateImplement { get; } public Dictionary PurchaseBuilds { get; } public Dictionary PurchaseProducts { get; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs new file mode 100644 index 0000000..bbce924 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs @@ -0,0 +1,117 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class PurchaseStorage : IPurchaseStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(PurchaseSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .Where(p => + (model.Id.HasValue && p.Id == model.Id) || + ((model.DateFrom.HasValue && model.DateTo.HasValue) && + ((model.DateFrom <= p.DateCreate && p.DateCreate <= model.DateTo) || + (p.DateImplement.HasValue && p.DateCreate < model.DateFrom && model.DateFrom < p.DateImplement))) + ) + .Select(p => p.GetViewModel) + .ToList(); + } + + public PurchaseViewModel? GetElement(PurchaseSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(p => p.Id == model.Id)? + .GetViewModel; + } + + public PurchaseViewModel? Insert(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newPurchase = Purchase.Create(context, model); + if (newPurchase == null) + { + return null; + } + context.Purchases.Add(newPurchase); + context.SaveChanges(); + return newPurchase.GetViewModel; + } + + public PurchaseViewModel? Update(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + using var transaction = context.Database.BeginTransaction(); + try + { + var purchase = context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(p => p.Id == model.Id); + if (purchase == null) + { + return null; + } + purchase.Update(model); + context.SaveChanges(); + transaction.Commit(); + return purchase.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PurchaseViewModel? Delete(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (element != null) + { + context.Purchases.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs index 3a60436..470cea3 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -17,6 +17,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public double Cost { get; set; } [Required] public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } = null; private Dictionary? _purchaseBuilds = null; [NotMapped] public Dictionary PurchaseBuilds From 56b6290c71725c0a09837d14161b593423cb6ef6 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 12:25:11 +0400 Subject: [PATCH 24/49] =?UTF-8?q?=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=81=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B2=D1=86=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/BuildSearchModel.cs | 1 + .../SearchModels/ProductSearchModel.cs | 4 ++-- .../Implements/BuildStorage.cs | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs index 49022a5..5b6b533 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs @@ -3,6 +3,7 @@ public class BuildSearchModel { public int? Id { get; set; } + public int VendorId { get; set; } public string? Name { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 7c72d29..1271144 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,7 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } - public int? VendorId { get; set; } - public int? Build { get; set; } + public int VendorId { get; set; } + public List? Builds { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs index 72c2a4b..d546f43 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -16,6 +16,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) + .Include(b => b.Vendor) .Select(b => b.GetViewModel) .ToList(); } @@ -28,6 +29,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Builds + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.VendorId ) .Where(b => b.Name.Contains(model.Name)) .Include(b => b.Comments) .Include(b => b.Components) @@ -44,6 +47,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Builds + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.VendorId) .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) @@ -64,6 +69,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements context.Builds.Add(newBuild); context.SaveChanges(); return context.Builds + .Include(b => b.Vendor) .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) @@ -78,6 +84,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.Vendor.Id) .FirstOrDefault(p => p.Id == model.Id); if (build == null) { @@ -94,6 +102,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.Vendor.Id) .FirstOrDefault(p => p.Id == model.Id); if (build == null) { From affb89bb59c45f7ec422f0cb2c4f547ae207524d Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 21:02:20 +0400 Subject: [PATCH 25/49] + --- .../Models/Component.cs | 50 ++++++++++ .../Models/Order.cs | 62 ++++++++++++ .../Models/Product.cs | 96 +++++++++++++++++++ .../Models/ProductComponent.cs | 17 ++++ 4 files changed, 225 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs 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(); + } +} From 070473b0d6c1abda5387f3d857ccba81f5920c7c Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 21:02:41 +0400 Subject: [PATCH 26/49] =?UTF-8?q?=D1=84=D0=B8=D0=B3=20=D0=B7=D0=BD=D0=B0?= =?UTF-8?q?=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerHardwareStoreBusinessLogic.csproj | 6 ++++ .../ComputerHardwareStoreDBContext.cs | 35 +++++++++++++++++++ ...puterHardwareStoreDatabaseImplement.csproj | 19 ++++++++++ 3 files changed, 60 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj index 47a15a0..982ae40 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs new file mode 100644 index 0000000..9226f42 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -0,0 +1,35 @@ +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement +{ + public class ComputerHardwareStoreDBContext : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + //optionsBuilder.UseNpgsql(@"Host=localhost;Database=ProductBar_db;Username=postgres;Password=postgres"); // не надо >: + /* + * в program добавить: + * // получаем строку подключения из файла конфигурации + * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); + * + * // добавляем контекст ApplicationContext в качестве сервиса в приложение + * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); + * + * в appsettings: + * "ConnectionStrings": { + * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" + * }, + */ + } + base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductComponents { set; get; } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj new file mode 100644 index 0000000..a7da7e9 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + From 95b2759a12647d0393d231566da0d3bd316c676c Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 17:05:42 +0400 Subject: [PATCH 27/49] + --- .../ComputerHardwareStore.sln | 8 +++- .../BindingModels/OrderBindingModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../Models/IOrderModel.cs | 2 +- .../ComputerHardwareStoreDBContext.cs | 1 + .../Models/Order.cs | 38 ++++++++++++++----- .../Models/OrderProduct.cs | 17 +++++++++ 7 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs diff --git a/ComputerHardwareStore/ComputerHardwareStore.sln b/ComputerHardwareStore/ComputerHardwareStore.sln index 1edd1ac..1f6cda1 100644 --- a/ComputerHardwareStore/ComputerHardwareStore.sln +++ b/ComputerHardwareStore/ComputerHardwareStore.sln @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDataMo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreContracts", "ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj", "{5F394E21-2597-432B-AE73-BBAFD8D9F50E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDatabaseImplement", "ComputerHardwareStoreDatabaseImplement\ComputerHardwareStoreDatabaseImplement.csproj", "{93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.ActiveCfg = Release|Any CPU {D32DEB60-AF40-46AF-8914-DC6A19BD66CD}.Release|Any CPU.Build.0 = Release|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs index 9cf80bd..e1017a0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } = null; - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs index d2f0ff1..a0b6602 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs @@ -16,6 +16,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public DateTime DateCreate { get; set; } [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs index 06f933b..3e7f94e 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs @@ -8,6 +8,6 @@ namespace ComputerHardwareStoreDataModels.Models OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } - public Dictionary OrderProduct { get; } + public Dictionary OrderProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 9226f42..850515c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -31,5 +31,6 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Components { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } + public virtual DbSet OrderProducts { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index eebf5a7..1754b65 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Enums; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -18,9 +19,25 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [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; } + + private Dictionary? _orderProducts = null; + [NotMapped] + public Dictionary OrderProducts + { + get + { + if (_orderProducts == null) + { + _orderProducts = Products + .ToDictionary( + op => op.ProductId, + op => (op.Product as IProductModel, op.Count)); + } + return _orderProducts; + } + } + [ForeignKey("OrderId")] + public virtual List Products { get; set; } = new(); public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model) { if (model == null) @@ -30,13 +47,18 @@ namespace ComputerHardwareStoreDatabaseImplement.Models 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) + Products = model.OrderProducts + .Select(op => new OrderProduct() + { + OrderId = model.Id, + ProductId = op.Key, + Count = op.Value.Item2 + }) + .ToList() }; } public void Update(OrderBindingModel model) @@ -51,12 +73,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, - ProductId = ProductId, - Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - ProductName = Product.ProductName }; } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs new file mode 100644 index 0000000..9930fe7 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class OrderProduct + { + public int Id { get; set; } + [Required] + public int OrderId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Product Product { get; set; } = new(); + public virtual Order Order { get; set; } = new(); + } +} From 34ada3ab9829015e4d63ed09ae42866e229da2b0 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 15:57:30 +0400 Subject: [PATCH 28/49] =?UTF-8?q?vendor=20=D1=8F=20=D0=BD=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=88=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/ProductViewModel.cs | 4 +- .../ComputerHardwareStoreDBContext.cs | 3 ++ .../Models/Build.cs | 22 ++++++++ .../Models/Component.cs | 11 ++-- .../Models/Order.cs | 11 ++-- .../Models/Product.cs | 3 +- .../Models/StoreKeeper.cs | 52 +++++++++++++++++++ 7 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs index 00cc4c2..c1ba122 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs @@ -10,8 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Price { get; set; } - public Dictionary CannedComponents { get; set; } = new(); - - public Dictionary ProductComponents => throw new NotImplementedException(); + public Dictionary ProductComponents { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 850515c..958078f 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -32,5 +32,8 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } public virtual DbSet OrderProducts { set; get; } + + public virtual DbSet StoreKeepers { set; get; } + public virtual DbSet Builds { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs new file mode 100644 index 0000000..4d650c3 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -0,0 +1,22 @@ + +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Build : IBuildModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public int VendorId { get; private set; } + public virtual Vendor Vendor { get; private set; } = new(); + + [ForeignKey("BuildId")] + public Dictionary BuildComponents => throw new NotImplementedException(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index 1a15d7e..3089400 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -13,9 +13,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; private set; } = string.Empty; [Required] public double Cost { get; set; } + [Required] + public int StoreKeeperId { get; private set; } + public virtual StoreKeeper? StoreKeeper { get; set; } + [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); - public static Component? Create(ComponentBindingModel model) + public static Component? Create(ComputerHardwareStoreDBContext context, ComponentBindingModel model) { if (model == null) { @@ -25,7 +29,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Models { Id = model.Id, Name = model.Name, - Cost = model.Cost + Cost = model.Cost, + StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeperId) }; } public void Update (ComponentBindingModel model) @@ -44,7 +49,5 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Name = Name, Cost = Cost }; - - public int StoreKeeperId => throw new NotImplementedException(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index 1754b65..ffb8b5e 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -51,14 +51,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, - Products = model.OrderProducts - .Select(op => new OrderProduct() - { - OrderId = model.Id, - ProductId = op.Key, - Count = op.Value.Item2 - }) - .ToList() + Product = context.Products.First(x => x.Id == model.ProductId) }; } public void Update(OrderBindingModel model) @@ -78,5 +71,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models DateCreate = DateCreate, DateImplement = DateImplement, }; + + public Dictionary OrderProduct => throw new NotImplementedException(); } } \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs index 0d67d07..a7af49c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -23,8 +23,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models if (_productComponents == null) { _productComponents = Components - .ToDictionary(c => c.ComponentId, c => - (c.Component as IComponentModel, c.Count)); + .ToDictionary(c => c.ComponentId, c => (c.Component as IComponentModel, c.Count)); } return _productComponents; } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs new file mode 100644 index 0000000..1c2028d --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -0,0 +1,52 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class StoreKeeper : IStoreKeeperModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + + public string Login { get; private set; } = string.Empty; + [Required] + + public string Password { get; private set; } = string.Empty; + + public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) + { + if (model == null) + { + return null; + } + return new StoreKeeper() + { + Id = model.Id, + Name = model.Name, + Login = model.Login, + Password = model.Password, + }; + } + public void Update(StoreKeeperBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + + public StoreKeeperViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Login = Login, + Password = Password + }; + } +} From 9688e5eb1c682f7fc2cb5f33f2d7fc99bac2344f Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 17:16:45 +0400 Subject: [PATCH 29/49] Vendor --- .../Models/Vendor.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs new file mode 100644 index 0000000..bc9567d --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs @@ -0,0 +1,52 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Vendor : IVendorModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + + public string Login { get; private set; } = string.Empty; + [Required] + + public string Password { get; private set; } = string.Empty; + + public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model) + { + if (model == null) + { + return null; + } + return new Vendor() + { + Id = model.Id, + Name = model.Name, + Login = model.Login, + Password = model.Password, + }; + } + public void Update(VendorBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + + public VendorViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Login = Login, + Password = Password + }; + } +} From 4733811487f88a55ef79b633526568cee99b2ff0 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 18:16:30 +0400 Subject: [PATCH 30/49] =?UTF-8?q?=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=83=20=D0=BA=D0=BB=D0=B0=D0=B4=D0=BE=D0=B2=D1=89=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BD=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/StoreKeeper.cs | 6 ++++++ .../ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs index 1c2028d..2143e5b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -2,6 +2,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -16,6 +17,11 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public string Password { get; private set; } = string.Empty; + [ForeignKey("StoreKeeperId")] + public virtual List Products { get; set; } = new(); + [ForeignKey("StoreKeeperId")] + public virtual List Components { get; set; } = new(); + public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) { diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs index bc9567d..4465a20 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs @@ -2,6 +2,7 @@ using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -16,6 +17,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public string Password { get; private set; } = string.Empty; + [ForeignKey("VendorId")] + public virtual List Purchases { get; set; } = new(); + [ForeignKey("VendorId")] + public virtual List Builds { get; set; } = new(); public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model) { From 7f429638b5ae146971e2680df26c4e014ce8aef6 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 18:59:45 +0400 Subject: [PATCH 31/49] =?UTF-8?q?=D0=A1=D0=B1=D0=BE=D1=80=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/BuildBindingModel.cs | 1 + .../ViewModels/BuildViewModel.cs | 9 +-- .../ViewModels/CommentViewModel.cs | 1 - .../Models/IBuildModel.cs | 1 + .../Models/ICommentModel.cs | 1 - .../ComputerHardwareStoreDBContext.cs | 4 +- .../Models/Build.cs | 56 +++++++++++++++++-- .../Models/BuildComponent.cs | 22 ++++++++ .../Models/Comment.cs | 48 ++++++++++++++++ 9 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs index e437bec..60ae196 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs @@ -9,5 +9,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public double Price { get; set; } public int VendorId { get; set; } public Dictionary BuildComponents { get; set; } = new(); + public List Comments { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs index 763cf75..0ddafa9 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs @@ -7,10 +7,11 @@ namespace ComputerHardwareStoreContracts.ViewModels { public int Id { get; set; } [DisplayName("Название сборки")] - public string Name { get; } = string.Empty; + public string Name { get; set; } = string.Empty; [DisplayName("Стоимость")] - public double Price { get; } - public int VendorId { get; } - public Dictionary BuildComponents { get; } = new(); + public double Price { get; set; } + public int VendorId { get; set; } + public Dictionary BuildComponents { get; set; } = new(); + public List Comments { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs index b62b98c..0978a2f 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs @@ -7,6 +7,5 @@ namespace ComputerHardwareStoreContracts.ViewModels public int Id { get; set; } public DateTime Date { get; set; } public string Text { get; set; } = string.Empty; - public int BuildId { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs index 6485ba0..6ae15ed 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs @@ -6,5 +6,6 @@ double Price { get; } int VendorId { get; } public Dictionary BuildComponents { get; } + public List Comments { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs index 76d1cdd..da32990 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs @@ -4,6 +4,5 @@ { DateTime Date { get; } string Text { get; } - int BuildId { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 958078f..cee42d3 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -31,9 +31,11 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Components { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductComponents { set; get; } - public virtual DbSet OrderProducts { set; get; } public virtual DbSet StoreKeepers { set; get; } public virtual DbSet Builds { set; get; } + public virtual DbSet OrderProducts { set; get; } + public virtual DbSet BuildComponents { set; get; } + public virtual DbSet Comments { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 4d650c3..38276ab 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -1,22 +1,68 @@  +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Runtime.InteropServices; namespace ComputerHardwareStoreDatabaseImplement.Models { public class Build : IBuildModel { - public int Id { get; private set; } + public int Id { get; set; } [Required] - public string Name { get; private set; } = string.Empty; + public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } [Required] - public int VendorId { get; private set; } + public int VendorId { get; set; } public virtual Vendor Vendor { get; private set; } = new(); - + private Dictionary? _buildComponents = null; + [NotMapped] + public Dictionary BuildComponents + { + get + { + if (_buildComponents == null) + { + _buildComponents = Components + .ToDictionary(bc => bc.ComponentId, bc => + (bc.Component as IComponentModel, bc.Count)); + } + return _buildComponents; + } + } [ForeignKey("BuildId")] - public Dictionary BuildComponents => throw new NotImplementedException(); + public virtual List Components { get; set; } = new(); + [ForeignKey("BuildId")] + public virtual List Comments { get; set; } = new(); + [NotMapped] + List IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList(); + + public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + VendorId = model.VendorId, + }; + } + + public BuildViewModel GetViewModel() => new() + { + Id = Id, + Name = Name, + Price = Price, + VendorId = VendorId, + Comments = Comments.Select(c => c as ICommentModel).ToList(), + BuildComponents = BuildComponents + }; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs new file mode 100644 index 0000000..4f98220 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class BuildComponent + { + public int Id { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Build Build { get; set; } = new(); + public virtual Component Component { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs new file mode 100644 index 0000000..dc10685 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs @@ -0,0 +1,48 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Comment : ICommentModel + { + public int Id { get; set; } + [Required] + public DateTime Date { get; set; } + [Required] + public string Text { get; set; } = string.Empty; + [Required] + public int BuildId { get; set; } + public static Comment? Create(CommentBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Date = model.Date, + Text = model.Text, + BuildId = model.BuildId, + }; + } + + public void Update(CommentBindingModel model) + { + if (model == null) + { + return; + } + Text = model.Text; + } + + public CommentViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + Text = Text, + }; + } +} From ed846686ce4457cb28167032c2059aa740dcd339 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 19:43:16 +0400 Subject: [PATCH 32/49] cyjdf c,jhrf --- .../BindingModels/BuildBindingModel.cs | 2 +- .../ViewModels/BuildViewModel.cs | 2 +- .../Models/IBuildModel.cs | 2 +- .../ComputerHardwareStoreDBContext.cs | 1 + .../Models/Build.cs | 22 ++++++++++++++----- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs index 60ae196..e52972b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs @@ -7,7 +7,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Price { get; set; } - public int VendorId { get; set; } + public IVendorModel Vendor { get; set; } public Dictionary BuildComponents { get; set; } = new(); public List Comments { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs index 0ddafa9..8274f6d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs @@ -10,7 +10,7 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Стоимость")] public double Price { get; set; } - public int VendorId { get; set; } + public IVendorModel Vendor { get; set; } public Dictionary BuildComponents { get; set; } = new(); public List Comments { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs index 6ae15ed..dee1777 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs @@ -4,7 +4,7 @@ { string Name { get; } double Price { get; } - int VendorId { get; } + IVendorModel Vendor { get; } public Dictionary BuildComponents { get; } public List Comments { get; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index cee42d3..e566837 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -37,5 +37,6 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } + public virtual DbSet Vendors { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 38276ab..db68236 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -2,6 +2,7 @@ using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Runtime.InteropServices; @@ -15,9 +16,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } - [Required] - public int VendorId { get; set; } - public virtual Vendor Vendor { get; private set; } = new(); + private Dictionary? _buildComponents = null; [NotMapped] public Dictionary BuildComponents @@ -36,10 +35,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [ForeignKey("BuildId")] public virtual List Components { get; set; } = new(); [ForeignKey("BuildId")] + public virtual List Comments { get; set; } = new(); [NotMapped] List IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList(); + public virtual Vendor Vendor { get; private set; } = new(); + IVendorModel IBuildModel.Vendor => Vendor as IVendorModel; + public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model) { if (model == null) @@ -51,7 +54,16 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Price = model.Price, - VendorId = model.VendorId, + Vendor = context.Vendors.First(v => v.Id == model.Vendor.Id), + Components = context.Components + .Where(c => model.BuildComponents.ContainsKey(c.Id)) + .Select(c => new BuildComponent() + { + BuildId = model.Id, + ComponentId = c.Id, + Component = c, + Count = model.BuildComponents[c.Id].Item2 + }).ToList() }; } @@ -60,7 +72,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = Id, Name = Name, Price = Price, - VendorId = VendorId, + Vendor = Vendor, Comments = Comments.Select(c => c as ICommentModel).ToList(), BuildComponents = BuildComponents }; From ca6035d7a1e2619bb0ed82f8e5239533e98a0d55 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 19:12:40 +0400 Subject: [PATCH 33/49] =?UTF-8?q?Purchase=20=D0=B8=20=D1=81=D0=B2=D1=8F?= =?UTF-8?q?=D0=B7=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/PurchaseBindingModel.cs | 4 +- .../ViewModels/PurchaseViewModel.cs | 4 +- .../Models/IPurchaseModel.cs | 4 +- .../ComputerHardwareStoreDBContext.cs | 3 + .../Models/Purchase.cs | 151 ++++++++++++++++++ .../Models/PurchaseBuild.cs | 18 +++ .../Models/PurchaseProduct.cs | 18 +++ 7 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index af08205..df458d5 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -8,7 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs index d0de94d..7e159b7 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs @@ -13,8 +13,8 @@ namespace ComputerHardwareStoreContracts.ViewModels public int VendorId { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 9afa7b6..39dd9e9 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -5,7 +5,7 @@ int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } - public Dictionary PurchaseBuild { get; } - public Dictionary PurchaseProduct { get; } + public Dictionary PurchaseBuilds { get; } + public Dictionary PurchaseProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index e566837..e83ce07 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -37,6 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } + public virtual DbSet Purchases { set; get; } + public virtual DbSet PurchaseBuilds { set; get; } + public virtual DbSet PurchaseProducts { set; get; } public virtual DbSet Vendors { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs new file mode 100644 index 0000000..3c5c9f0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -0,0 +1,151 @@ + +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 Purchase : IPurchaseModel + { + public int Id { get; private set; } + [Required] + public int VendorId { get; set; } + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + private Dictionary? _purchaseBuilds = null; + [NotMapped] + public Dictionary PurchaseBuilds + { + get + { + if (_purchaseBuilds == null) + { + _purchaseBuilds = Builds + .ToDictionary(op => op.BuildId, op => (op.Build as IBuildModel, op.Count)); + } + return _purchaseBuilds; + } + } + + [ForeignKey("PurchaseId")] + public virtual List Builds { get; set; } = new(); + private Dictionary? _purchaseProducts = null; + [NotMapped] + public Dictionary PurchaseProducts + { + get + { + if (_purchaseProducts == null) + { + _purchaseProducts = Products + .ToDictionary(op => op.ProductId, op => (op.Product as IProductModel, op.Count)); + } + return _purchaseProducts; + } + } + [ForeignKey("PurchaseId")] + public virtual List Products { get; set; } = new(); + + public static Purchase Create(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + return new Purchase() + { + Id = model.Id, + Cost = model.Cost, + DateCreate = model.DateCreate, + Builds = model.PurchaseBuilds.Select(x => + new PurchaseBuild + { + Build = context.Builds.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + Products = model.PurchaseProducts.Select(x => + new PurchaseProduct + { + Product = context.Products.First(z => z.Id == z.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PurchaseBindingModel model) + { + Cost = model.Cost; + + } + public PurchaseViewModel GetViewModel => new() + { + Id = Id, + Cost = Cost, + DateCreate = DateCreate, + PurchaseBuilds = PurchaseBuilds, + PurchaseProducts = PurchaseProducts + }; + + public static void UpdateBuilds(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productBuilds = context.PurchaseBuilds + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productBuilds.Count != 0 && productBuilds.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseBuilds + .Where(pc => !model.PurchaseBuilds.ContainsKey(pc.BuildId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productBuilds + .ForEach(updateBuild => + { + updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2; + model.PurchaseBuilds.Remove(updateBuild.BuildId); + }); + } + // добавили новые + context.PurchaseBuilds + .AddRange(model.PurchaseBuilds.Values + .Select(val => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + + public static void UpdateProducts(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productProducts = context.PurchaseProducts + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productProducts.Count != 0 && productProducts.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseProducts + .Where(pc => !model.PurchaseProducts.ContainsKey(pc.ProductId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productProducts + .ForEach(updateProduct => + { + updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2; + model.PurchaseProducts.Remove(updateProduct.ProductId); + }); + } + // добавили новые + context.PurchaseProducts + .AddRange(model.PurchaseProducts.Values + .Select(val => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs new file mode 100644 index 0000000..2f5c1af --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseBuild + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Build Build { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs new file mode 100644 index 0000000..2bc9116 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseProduct + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} From 0fd8a7fd8f905cf5caf08e0fbd76ba144e2cfde4 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 19:49:13 +0400 Subject: [PATCH 34/49] product mtm --- .../Models/Product.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs index a7af49c..023758a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -39,12 +39,15 @@ namespace ComputerHardwareStoreDatabaseImplement.Models 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() + Components = context.Components + .Where(c => model.ProductComponents.ContainsKey(c.Id)) + .Select(c => new ProductComponent() + { + ProductId = model.Id, + ComponentId = c.Id, + Component = c, + Count = model.ProductComponents[c.Id].Item2 + }).ToList(), }; } public void Update(ProductBindingModel model) From 9df5c5d35875364c5e335afa49399858164eed2f Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 19:53:37 +0400 Subject: [PATCH 35/49] + order --- .../ComputerHardwareStoreDBContext.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index e83ce07..6b2bf5d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -34,6 +34,7 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet StoreKeepers { set; get; } public virtual DbSet Builds { set; get; } + public virtual DbSet Orders { set; get; } public virtual DbSet OrderProducts { set; get; } public virtual DbSet BuildComponents { set; get; } public virtual DbSet Comments { set; get; } From 3e68f069ce987f3234113d5a1ddd5beffb7729a4 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:06:35 +0400 Subject: [PATCH 36/49] =?UTF-8?q?=D0=BF=D1=83=D1=80=D1=87=D0=B0=D0=B9?= =?UTF-8?q?=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ComponentBindingModel.cs | 2 +- .../ViewModels/ComponentViewModel.cs | 2 +- .../Models/IComponentModel.cs | 2 +- .../Models/Component.cs | 5 +-- .../Models/Purchase.cs | 31 ++++++++++++------- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs index af0759b..e14244d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs @@ -7,6 +7,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs index 6aadb04..eb9fd9a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs index 6eea153..84f259d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs @@ -4,6 +4,6 @@ { string Name { get; } double Cost { get; } - int StoreKeeperId { get; } + IStoreKeeperModel StoreKeeper { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index 3089400..e26f654 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -14,8 +14,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public double Cost { get; set; } [Required] - public int StoreKeeperId { get; private set; } public virtual StoreKeeper? StoreKeeper { get; set; } + [NotMapped] + IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); @@ -30,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeperId) + StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) }; } public void Update (ComponentBindingModel model) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs index 3c5c9f0..3a60436 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -58,18 +58,25 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Cost = model.Cost, DateCreate = model.DateCreate, - Builds = model.PurchaseBuilds.Select(x => - new PurchaseBuild - { - Build = context.Builds.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList(), - Products = model.PurchaseProducts.Select(x => - new PurchaseProduct - { - Product = context.Products.First(z => z.Id == z.Key), - Count = x.Value.Item2 - }).ToList() + Builds = context.Builds + .Where(b => model.PurchaseBuilds.ContainsKey(b.Id)) + .Select(b => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = b.Id, + Build = b, + Count = model.PurchaseProducts[b.Id].Item2 + }).ToList(), + Products = context.Products + .Where(p => model.PurchaseProducts.ContainsKey(p.Id)) + .Select (p => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = p.Id, + Product = p, + Count = model.PurchaseProducts[p.Id].Item2 + }) + .ToList() }; } public void Update(PurchaseBindingModel model) From 060c5dc37f2d077848ac66dda40f247daf30fae7 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 30 Apr 2024 19:52:50 +0400 Subject: [PATCH 37/49] =?UTF-8?q?cleaning=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B1=D0=B5=D0=B7=20=D1=83=D1=81=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerHardwareStoreDBContext.cs | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 6b2bf5d..1c09ba6 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -7,23 +7,20 @@ namespace ComputerHardwareStoreDatabaseImplement { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - if (optionsBuilder.IsConfigured == false) - { - //optionsBuilder.UseNpgsql(@"Host=localhost;Database=ProductBar_db;Username=postgres;Password=postgres"); // не надо >: - /* - * в program добавить: - * // получаем строку подключения из файла конфигурации - * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); - * - * // добавляем контекст ApplicationContext в качестве сервиса в приложение - * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); - * - * в appsettings: - * "ConnectionStrings": { - * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" - * }, - */ - } + /* + * в program добавить: + * // получаем строку подключения из файла конфигурации + * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); + * + * // добавляем контекст ApplicationContext в качестве сервиса в приложение + * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); + * + * в appsettings: + * "ConnectionStrings": { + * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" + * }, + */ + base.OnConfiguring(optionsBuilder); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); From 7bcfa7e0e0c9fcf36feefe8f90fdecb53346a579 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:11:52 +0400 Subject: [PATCH 38/49] op --- .../Models/Order.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs index ffb8b5e..bf50b41 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -51,7 +51,16 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, - Product = context.Products.First(x => x.Id == model.ProductId) + Products = context.Products + .Where(p => model.OrderProducts.ContainsKey(p.Id)) + .Select(p => new OrderProduct() + { + OrderId = model.Id, + ProductId = p.Id, + Product = p, + Count = model.OrderProducts[p.Id].Item2 + }) + .ToList() }; } public void Update(OrderBindingModel model) @@ -71,7 +80,5 @@ namespace ComputerHardwareStoreDatabaseImplement.Models DateCreate = DateCreate, DateImplement = DateImplement, }; - - public Dictionary OrderProduct => throw new NotImplementedException(); } } \ No newline at end of file From 0a6730a72227f4d6afbec88b6c894207f1d5c1ff Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:27:11 +0400 Subject: [PATCH 39/49] componentStorage --- .../Implements/ComponentStorage.cs | 85 +++++++++++++++++++ .../Models/Build.cs | 10 +++ .../Models/StoreKeeper.cs | 2 - 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..5041064 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new (); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) + && x.Name == model.Name) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComponent = Component.Create(context, model); + if (newComponent == null) + { + return null; + } + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index db68236..1675e6a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -67,6 +67,16 @@ namespace ComputerHardwareStoreDatabaseImplement.Models }; } + public void Update(BuildBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Price = model.Price; + } + public BuildViewModel GetViewModel() => new() { Id = Id, diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs index 2143e5b..1fd691b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -15,14 +15,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Login { get; private set; } = string.Empty; [Required] - public string Password { get; private set; } = string.Empty; [ForeignKey("StoreKeeperId")] public virtual List Products { get; set; } = new(); [ForeignKey("StoreKeeperId")] public virtual List Components { get; set; } = new(); - public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) { if (model == null) From 2f7003d3b7d3f6eb7a93d394bb0a34ac7bc43605 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 20:42:42 +0400 Subject: [PATCH 40/49] productStorage --- .../Implements/ComponentStorage.cs | 29 +++---- .../Implements/ProductStorage.cs | 85 +++++++++++++++++++ 2 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs index 5041064..5bb8224 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -12,7 +12,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { using var context = new ComputerHardwareStoreDBContext(); return context.Components - .Select(x => x.GetViewModel) + .Select(c => c.GetViewModel) .ToList(); } @@ -20,12 +20,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { if (string.IsNullOrEmpty(model.Name)) { - return new (); + return new(); } using var context = new ComputerHardwareStoreDBContext(); return context.Components - .Where(x => x.Name.Contains(model.Name)) - .Select(x => x.GetViewModel) + .Where(c => c.Name.Contains(model.Name)) + .Select(c => c.GetViewModel) .ToList(); } @@ -37,10 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Components - .FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Name) - && x.Name == model.Name) - || (model.Id.HasValue && x.Id == model.Id)) + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c .Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) ?.GetViewModel; } @@ -59,7 +58,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Update(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + var component = context.Components.FirstOrDefault(c => c.Id == model.Id); if (component == null) { return null; @@ -72,14 +71,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Delete(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + var element = context.Components.FirstOrDefault(c => c.Id == model.Id); + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..a5face0 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,85 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Where(p => p.Name.Contains(model.Name)) + .Select(p => p.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) + ?.GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return product.GetViewModel; + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Products.Remove(product); + context.SaveChanges(); + return product.GetViewModel; + } + } +} From 765b68f754c0693d745a9c20af5aea1d8cbbce79 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 30 Apr 2024 21:00:26 +0400 Subject: [PATCH 41/49] productStorage --- .../Implements/ProductStorage.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index a5face0..9c74297 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -3,6 +3,7 @@ using ComputerHardwareStoreContracts.SearchModels; using ComputerHardwareStoreContracts.StorageContracts; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace ComputerHardwareStoreDatabaseImplement.Implements { @@ -12,6 +13,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements { using var context = new ComputerHardwareStoreDBContext(); return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) .ToList(); } @@ -25,6 +28,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements using var context = new ComputerHardwareStoreDBContext(); return context.Products .Where(p => p.Name.Contains(model.Name)) + .Include(p => p.Components) + .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) .ToList(); } @@ -37,10 +42,12 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) .FirstOrDefault(c => (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || - (model.Id.HasValue && c.Id == model.Id)) - ?.GetViewModel; + (model.Id.HasValue && c.Id == model.Id))? + .GetViewModel; } public ProductViewModel? Insert(ProductBindingModel model) @@ -53,13 +60,20 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } context.Products.Add(newProduct); context.SaveChanges(); - return newProduct.GetViewModel; + return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == newProduct.Id)? + .GetViewModel; } public ProductViewModel? Update(ProductBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); if (product == null) { return null; @@ -72,7 +86,10 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ProductViewModel? Delete(ProductBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var product = context.Products.FirstOrDefault(p => p.Id == model.Id); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); if (product == null) { return null; From 2ff2aeb812e19c23156f5cd85bd00b76c3ed1926 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:04:41 +0400 Subject: [PATCH 42/49] OrderStorage --- .../Implements/OrderStorage.cs | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..2378d44 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,108 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .Select(o => o.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .Where(o => + (model.Id.HasValue && o.Id == model.Id) || + ((model.DateFrom.HasValue && model.DateTo.HasValue) && + ((model.DateFrom <= o.DateCreate && o.DateCreate <= model.DateTo) || + (o.DateImplement.HasValue && o.DateCreate < model.DateFrom && model.DateFrom < o.DateImplement))) + ) + .Select(o => o.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id)? + .GetViewModel; + + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + using var transaction = context.Database.BeginTransaction(); + try + { + var order = context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + transaction.Commit(); + return order.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} From fa4bf3e8d4bd37771340b8c3cfe4dfec8531ca35 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:35:35 +0400 Subject: [PATCH 43/49] build storage --- .../Implements/BuildStorage.cs | 107 ++++++++++++++++++ .../Implements/ProductStorage.cs | 6 +- .../Models/Build.cs | 8 +- 3 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs new file mode 100644 index 0000000..72c2a4b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -0,0 +1,107 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class BuildStorage : IBuildStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Select(b => b.GetViewModel) + .ToList(); + } + + public List GetFilteredList(BuildSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Where(b => b.Name.Contains(model.Name)) + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Select(b => b.GetViewModel) + .ToList(); + } + + public BuildViewModel? GetElement(BuildSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(b => + (!string.IsNullOrEmpty(model.Name) && b.Name == model.Name) || + (model.Id.HasValue && b.Id == model.Id))? + .GetViewModel; + } + + public BuildViewModel? Insert(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newBuild = Build.Create(context, model); + if (newBuild == null) + { + return null; + } + context.Builds.Add(newBuild); + context.SaveChanges(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == newBuild.Id)? + .GetViewModel; + } + + public BuildViewModel? Update(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var build = context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == model.Id); + if (build == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return build.GetViewModel; + } + public BuildViewModel? Delete(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var build = context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == model.Id); + if (build == null) + { + return null; + } + context.Builds.Remove(build); + context.SaveChanges(); + return build.GetViewModel; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index 9c74297..ae83b6b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -44,9 +44,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements return context.Products .Include(p => p.Components) .ThenInclude(p => p.Component) - .FirstOrDefault(c => - (!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) || - (model.Id.HasValue && c.Id == model.Id))? + .FirstOrDefault(p => + (!string.IsNullOrEmpty(model.Name) && p.Name == model.Name) || + (model.Id.HasValue && p.Id == model.Id))? .GetViewModel; } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 1675e6a..c4c8f29 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -1,11 +1,9 @@ - -using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Runtime.InteropServices; namespace ComputerHardwareStoreDatabaseImplement.Models { @@ -16,7 +14,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } - + [NotMapped] private Dictionary? _buildComponents = null; [NotMapped] public Dictionary BuildComponents @@ -77,7 +75,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Price = model.Price; } - public BuildViewModel GetViewModel() => new() + public BuildViewModel GetViewModel => new() { Id = Id, Name = Name, From fa4c708e1c325f0fbcfe1a723e8ea98eeaee5518 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Wed, 1 May 2024 11:09:52 +0400 Subject: [PATCH 44/49] =?UTF-8?q?comment,=20storekeeper,=20vendor=20+=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2?= =?UTF-8?q?=20=D0=B8=D1=85=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/CommentSearchModel.cs | 1 - .../SearchModels/ProductSearchModel.cs | 3 +- .../SearchModels/StoreKeeperSearchModel.cs | 1 - .../Implements/CommentStorage.cs | 77 +++++++++++++++++ .../Implements/ComponentStorage.cs | 8 +- .../Implements/StoreKeeperStorage.cs | 86 +++++++++++++++++++ .../Implements/Vendor.cs | 84 ++++++++++++++++++ .../Models/Component.cs | 8 +- 8 files changed, 257 insertions(+), 11 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs index 1378964..3dcc2c0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs @@ -3,6 +3,5 @@ public class CommentSearchModel { public int? Id { get; set; } - public DateTime? Date { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 046e6d7..7c72d29 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,6 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } - public int? StoreKeeperId { get; set; } + public int? VendorId { get; set; } + public int? Build { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs index ed748fe..21c4265 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs @@ -4,6 +4,5 @@ { public int? Id { get; set; } public string? Login { get; set; } - public string? Password { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs new file mode 100644 index 0000000..a357455 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs @@ -0,0 +1,77 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class CommentStorage : ICommentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CommentSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public CommentViewModel? GetElement(CommentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public CommentViewModel? Insert(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComment = Comment.Create(model); + if (newComment == null) + { + return null; + } + context.Comments.Add(newComment); + context.SaveChanges(); + return newComment.GetViewModel; + } + public CommentViewModel? Update(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + + public CommentViewModel? Delete(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Comments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs index 5bb8224..9fdc559 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -58,14 +58,14 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements public ComponentViewModel? Update(ComponentBindingModel model) { using var context = new ComputerHardwareStoreDBContext(); - var component = context.Components.FirstOrDefault(c => c.Id == model.Id); - if (component == null) + var element = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (element == null) { return null; } - component.Update(model); + element.Update(model); context.SaveChanges(); - return component.GetViewModel; + return element.GetViewModel; } public ComponentViewModel? Delete(ComponentBindingModel model) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs new file mode 100644 index 0000000..bd4aff8 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs @@ -0,0 +1,86 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class StoreKeeperStorage : IStoreKeeperStorage + { + public StoreKeeperViewModel? GetElement(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Include(x => x.Components) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StoreKeeperViewModel? Insert(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newStoreKeeper = StoreKeeper.Create(context, model); + if (newStoreKeeper == null) + { + return null; + } + context.StoreKeepers.Add(newStoreKeeper); + context.SaveChanges(); + return newStoreKeeper.GetViewModel; + } + + public StoreKeeperViewModel? Update(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public StoreKeeperViewModel? Delete(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.StoreKeepers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs new file mode 100644 index 0000000..1e74a2a --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs @@ -0,0 +1,84 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class VendorStorage : IVendorStorage + { + public VendorViewModel? GetElement(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Select(x => x.GetViewModel) + .ToList(); + } + + public VendorViewModel? Insert(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newVendor = Vendor.Create(context, model); + if (newVendor == null) + { + return null; + } + context.Vendors.Add(newVendor); + context.SaveChanges(); + return newVendor.GetViewModel; + } + + public VendorViewModel? Update(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public VendorViewModel? Delete(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Vendors.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index e26f654..4acbece 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -14,13 +14,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [Required] public double Cost { get; set; } [Required] - public virtual StoreKeeper? StoreKeeper { get; set; } + public virtual StoreKeeper? StoreKeeperЗЛ { get; set; } [NotMapped] - IStoreKeeperModel IComponentModel.StoreKeeper => throw new NotImplementedException(); + public IStoreKeeperModel? StoreKeeper { get; set; } [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); - public static Component? Create(ComputerHardwareStoreDBContext context, ComponentBindingModel model) + public static Component? Create(ComponentBindingModel model) { if (model == null) { @@ -31,7 +31,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Id = model.Id, Name = model.Name, Cost = model.Cost, - StoreKeeper = context.StoreKeepers.First(x => x.Id == model.StoreKeeper.Id) + StoreKeeper = model.StoreKeeper }; } public void Update (ComponentBindingModel model) From 7f6c134c575beaf8efa1805aed341021781680b4 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:55:41 +0400 Subject: [PATCH 45/49] PurchaseStorage --- .../BindingModels/PurchaseBindingModel.cs | 1 + .../ViewModels/PurchaseViewModel.cs | 3 +- .../Models/IPurchaseModel.cs | 1 + .../Implements/PurchaseStorage.cs | 117 ++++++++++++++++++ .../Models/Purchase.cs | 1 + 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index df458d5..e28c326 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -8,6 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } public Dictionary PurchaseBuilds { get; set; } = new(); public Dictionary PurchaseProducts { get; set; } = new(); } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs index 7e159b7..4911e1d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs @@ -10,11 +10,12 @@ namespace ComputerHardwareStoreContracts.ViewModels public double Cost { get; set; } [DisplayName("Дата создания")] public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } public int VendorId { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } public Dictionary PurchaseBuilds { get; set; } = new(); - public Dictionary PurchaseProducts { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 39dd9e9..efc0095 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -5,6 +5,7 @@ int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } + DateTime? DateImplement { get; } public Dictionary PurchaseBuilds { get; } public Dictionary PurchaseProducts { get; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs new file mode 100644 index 0000000..bbce924 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs @@ -0,0 +1,117 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class PurchaseStorage : IPurchaseStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(PurchaseSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .Where(p => + (model.Id.HasValue && p.Id == model.Id) || + ((model.DateFrom.HasValue && model.DateTo.HasValue) && + ((model.DateFrom <= p.DateCreate && p.DateCreate <= model.DateTo) || + (p.DateImplement.HasValue && p.DateCreate < model.DateFrom && model.DateFrom < p.DateImplement))) + ) + .Select(p => p.GetViewModel) + .ToList(); + } + + public PurchaseViewModel? GetElement(PurchaseSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(p => p.Id == model.Id)? + .GetViewModel; + } + + public PurchaseViewModel? Insert(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newPurchase = Purchase.Create(context, model); + if (newPurchase == null) + { + return null; + } + context.Purchases.Add(newPurchase); + context.SaveChanges(); + return newPurchase.GetViewModel; + } + + public PurchaseViewModel? Update(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + using var transaction = context.Database.BeginTransaction(); + try + { + var purchase = context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(p => p.Id == model.Id); + if (purchase == null) + { + return null; + } + purchase.Update(model); + context.SaveChanges(); + transaction.Commit(); + return purchase.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PurchaseViewModel? Delete(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (element != null) + { + context.Purchases.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs index 3a60436..470cea3 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -17,6 +17,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public double Cost { get; set; } [Required] public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } = null; private Dictionary? _purchaseBuilds = null; [NotMapped] public Dictionary PurchaseBuilds From c2c4759c68e485bc1ed297c9b13843d7ffa6c247 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 12:25:11 +0400 Subject: [PATCH 46/49] =?UTF-8?q?=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=81=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B2=D1=86=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/BuildSearchModel.cs | 1 + .../SearchModels/ProductSearchModel.cs | 4 ++-- .../Implements/BuildStorage.cs | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs index a6183f7..6792217 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs @@ -3,6 +3,7 @@ public class BuildSearchModel { public int? Id { get; set; } + public int VendorId { get; set; } public string? Name { get; set; } public int? VendorId { get; set; } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 7c72d29..1271144 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,7 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } - public int? VendorId { get; set; } - public int? Build { get; set; } + public int VendorId { get; set; } + public List? Builds { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs index 72c2a4b..d546f43 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -16,6 +16,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) + .Include(b => b.Vendor) .Select(b => b.GetViewModel) .ToList(); } @@ -28,6 +29,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Builds + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.VendorId ) .Where(b => b.Name.Contains(model.Name)) .Include(b => b.Comments) .Include(b => b.Components) @@ -44,6 +47,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Builds + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.VendorId) .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) @@ -64,6 +69,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements context.Builds.Add(newBuild); context.SaveChanges(); return context.Builds + .Include(b => b.Vendor) .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) @@ -78,6 +84,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.Vendor.Id) .FirstOrDefault(p => p.Id == model.Id); if (build == null) { @@ -94,6 +102,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements .Include(b => b.Comments) .Include(b => b.Components) .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.Vendor.Id) .FirstOrDefault(p => p.Id == model.Id); if (build == null) { From de8a64c5096eef4b8346aab4d2926ca4850d74d7 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 12:37:40 +0400 Subject: [PATCH 47/49] + --- .../SearchModels/BuildSearchModel.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs index 6792217..5b6b533 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs @@ -5,6 +5,5 @@ public int? Id { get; set; } public int VendorId { get; set; } public string? Name { get; set; } - public int? VendorId { get; set; } } } From c229b58d9ddb35d0375ab156c4ec5b6114cc9806 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Wed, 1 May 2024 17:42:38 +0400 Subject: [PATCH 48/49] =?UTF-8?q?=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D1=81?= =?UTF-8?q?=D0=B1=D0=BE=D1=80=D0=BE=D0=BA=20=D0=BF=D0=BE=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BA=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/ProductSearchModel.cs | 4 ++-- .../Models/IProductModel.cs | 1 + .../Implements/ProductStorage.cs | 8 +++++++- .../Models/Component.cs | 2 ++ .../Models/Product.cs | 1 + 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 1271144..80dacff 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,7 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } - public int VendorId { get; set; } - public List? Builds { get; set; } + public int StoreKeeperId { get; set; } + public int? BuildId { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IProductModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IProductModel.cs index a38ae89..a0dbef4 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IProductModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IProductModel.cs @@ -4,6 +4,7 @@ { string Name { get; } double Price { get; } + int StoreKeeperId { get; } public Dictionary ProductComponents { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index ae83b6b..d27a131 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -27,7 +27,13 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Products - .Where(p => p.Name.Contains(model.Name)) + .Where(p => (p.StoreKeeperId == model.StoreKeeperId) + // есть хотя бы 1 общий со сборкой компонент + && (!model.BuildId.HasValue || p.Components.FirstOrDefault + (c => c.Component.BuldComponents.FirstOrDefault( + b => b.BuildId == model.BuildId) + != null) + != null)) .Include(p => p.Components) .ThenInclude(p => p.Component) .Select(p => p.GetViewModel) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs index 4acbece..80f2389 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -20,6 +20,8 @@ namespace ComputerHardwareStoreDatabaseImplement.Models [ForeignKey("ComponentId")] public virtual List ProductComponents { get; set; } = new(); + [ForeignKey("ComponentId")] + public virtual List BuldComponents { get; set; } = new(); public static Component? Create(ComponentBindingModel model) { if (model == null) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs index 023758a..f86ed01 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -14,6 +14,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } + public int StoreKeeperId { get; private set; } private Dictionary? _productComponents = null; [NotMapped] public Dictionary ProductComponents From d04752b2badefdf1c93b2613de4f3c8d3f59aa73 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Wed, 1 May 2024 17:57:14 +0400 Subject: [PATCH 49/49] =?UTF-8?q?=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B4=D1=83=D0=BA=D1=82=D0=BE=D0=B2=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D1=81=D0=B1=D0=BE=D1=80=D0=BA=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SearchModels/BuildSearchModel.cs | 1 + .../ComputerHardwareStoreDBContext.cs | 1 - .../Implements/BuildStorage.cs | 12 ++++++++++-- .../Implements/{Vendor.cs => VendorStorage.cs} | 0 4 files changed, 11 insertions(+), 3 deletions(-) rename ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/{Vendor.cs => VendorStorage.cs} (100%) diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs index 5b6b533..1af793c 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs @@ -4,6 +4,7 @@ { public int? Id { get; set; } public int VendorId { get; set; } + public int? ProductId { get; set; } public string? Name { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs index 0be2db5..c2d1673 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -39,6 +39,5 @@ namespace ComputerHardwareStoreDatabaseImplement public virtual DbSet Purchases { set; get; } public virtual DbSet PurchaseBuilds { set; get; } public virtual DbSet PurchaseProducts { set; get; } - public virtual DbSet Vendors { set; get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs index d546f43..f192b82 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -29,10 +29,18 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements } using var context = new ComputerHardwareStoreDBContext(); return context.Builds + // только те, что созданы пользователем .Include(b => b.Vendor) - .Where(b => b.Vendor.Id == model.VendorId ) - .Where(b => b.Name.Contains(model.Name)) + .Where(b => b.Vendor.Id == model.VendorId + // если есть хотя бы 1 общий компонент с товарами + && !model.ProductId.HasValue || b.Components.FirstOrDefault( + c => c.Component.ProductComponents.FirstOrDefault( + p => p.ProductId == model.ProductId) + != null) + != null) + // вместе с комментами .Include(b => b.Comments) + // вместе с компонентами .Include(b => b.Components) .ThenInclude(b => b.Component) .Select(b => b.GetViewModel) diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/VendorStorage.cs similarity index 100% rename from ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs rename to ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/VendorStorage.cs