From 4b1d26dfb5c0dbfb4650a5b41fb38cf0e02fb7a1 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 20:51:55 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=87=D1=82=D0=BE=20=D1=82=D0=B0=D0=BA?= =?UTF-8?q?=D0=BE=D0=B5=20=D0=B1=D0=B5=D0=B7=D1=83=D0=BC=D0=B8=D0=B5=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/OrderLogic.cs | 6 +++--- .../BusinessLogic/PurchaseLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 6 ++---- .../{IBuidStorage.cs => IBuildStorage.cs} | 2 +- .../ViewModels/OrderViewModel.cs | 12 +++++++++--- .../Models/IOrderModel.cs | 8 ++++++-- 6 files changed, 22 insertions(+), 14 deletions(-) rename ComputerHardwareStore/ComputerHardwareStoreContracts/StorageContracts/{IBuidStorage.cs => IBuildStorage.cs} (94%) 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/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; } } } From ce72b6d6b0cb9111f6f45e87ee0a12e07713ec4d Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 29 Apr 2024 20:59:09 +0400 Subject: [PATCH 2/2] + --- .../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(); + } +}