From a9b6277b5568a1f38e9464b46de47ce40a9bf0dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D0=B1=D0=B5=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Sun, 21 Apr 2024 19:17:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=D0=BB=D0=BE=D1=8F=20=D1=81=20=D0=B1=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B8=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D1=8B=D0=B4=D1=83=D1=89=D0=B8=D1=85=20=D1=81?= =?UTF-8?q?=D0=BB=D0=BE=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AccountingWarehouseProducts.sln | 6 + .../BindingModels/StandBindingModel.cs | 2 + .../SearchModels/StandSearchModel.cs | 2 + .../ViewModels/StandViewModel.cs | 4 + .../Models/IStandModel.cs | 3 +- .../AccountingWarehouseProductsDatabase.cs | 31 ++ ...gWarehouseProductsDatabaseImplement.csproj | 23 ++ .../Implements/OrderStorage.cs | 87 ++++++ .../Implements/ProductStorage.cs | 77 +++++ .../Implements/ShipmentStorage.cs | 77 +++++ .../Implements/StandStorage.cs | 77 +++++ .../Implements/SupplierStorage.cs | 77 +++++ .../Implements/WarehouseStorage.cs | 86 ++++++ .../20240421151136_InitMigration.Designer.cs | 281 ++++++++++++++++++ .../20240421151136_InitMigration.cs | 210 +++++++++++++ ...gWarehouseProductsDatabaseModelSnapshot.cs | 278 +++++++++++++++++ .../Models/Order.cs | 97 ++++++ .../Models/OrderProduct.cs | 21 ++ .../Models/Product.cs | 60 ++++ .../Models/Shipment.cs | 60 ++++ .../Models/Stand.cs | 60 ++++ .../Models/Supplier.cs | 56 ++++ .../Models/Warehouse.cs | 99 ++++++ .../Models/WarehouseProduct.cs | 21 ++ .../AccountingWarehouseProductsView.csproj | 11 + 25 files changed, 1805 insertions(+), 1 deletion(-) create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabase.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabaseImplement.csproj create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/OrderStorage.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ProductStorage.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ShipmentStorage.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/StandStorage.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/SupplierStorage.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/WarehouseStorage.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.Designer.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/AccountingWarehouseProductsDatabaseModelSnapshot.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Order.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/OrderProduct.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Product.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Shipment.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Stand.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Supplier.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Warehouse.cs create mode 100644 AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/WarehouseProduct.cs diff --git a/AccountingWarehouseProducts/AccountingWarehouseProducts.sln b/AccountingWarehouseProducts/AccountingWarehouseProducts.sln index cfb45cd..14d0f3f 100644 --- a/AccountingWarehouseProducts/AccountingWarehouseProducts.sln +++ b/AccountingWarehouseProducts/AccountingWarehouseProducts.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountingWarehouseProducts EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountingWarehouseProductsBusinessLogic", "AccountingWarehouseProductsBusinessLogic\AccountingWarehouseProductsBusinessLogic.csproj", "{7F90D2FA-8F89-4200-92FC-CFFF489AD6C9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountingWarehouseProductsDatabaseImplement", "AccountingWarehouseProductsDatabaseImplement\AccountingWarehouseProductsDatabaseImplement.csproj", "{1063D739-0279-4F6A-BD7B-13E51EAA3410}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {7F90D2FA-8F89-4200-92FC-CFFF489AD6C9}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F90D2FA-8F89-4200-92FC-CFFF489AD6C9}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F90D2FA-8F89-4200-92FC-CFFF489AD6C9}.Release|Any CPU.Build.0 = Release|Any CPU + {1063D739-0279-4F6A-BD7B-13E51EAA3410}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1063D739-0279-4F6A-BD7B-13E51EAA3410}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1063D739-0279-4F6A-BD7B-13E51EAA3410}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1063D739-0279-4F6A-BD7B-13E51EAA3410}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/BindingModels/StandBindingModel.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/BindingModels/StandBindingModel.cs index 17c3ccb..b91cc4f 100644 --- a/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/BindingModels/StandBindingModel.cs +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/BindingModels/StandBindingModel.cs @@ -11,6 +11,8 @@ namespace AccountingWarehouseProductsContracts.BindingModels { public int Id { get; set; } + public string StandName { get; set; } + public DateTime? DeliveryDate { get; set; } public int Count { get; set; } diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/SearchModels/StandSearchModel.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/SearchModels/StandSearchModel.cs index 2497b91..30322e2 100644 --- a/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/SearchModels/StandSearchModel.cs +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/SearchModels/StandSearchModel.cs @@ -9,5 +9,7 @@ namespace AccountingWarehouseProductsContracts.SearchModels public class StandSearchModel { public int? Id { get; set; } + + public string StandName { get; set; } = string.Empty; } } diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/ViewModels/StandViewModel.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/ViewModels/StandViewModel.cs index 01d2508..577856e 100644 --- a/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/ViewModels/StandViewModel.cs +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsContracts/ViewModels/StandViewModel.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,6 +14,9 @@ namespace AccountingWarehouseProductsContracts.ViewModels [DisplayName("Номер")] public int Id { get; set; } + [DisplayName("Название поставки")] + public string StandName { get; set; } = string.Empty; + [DisplayName("Дата поставки")] public DateTime? DeliveryDate { get; set; } diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDataModels/Models/IStandModel.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDataModels/Models/IStandModel.cs index b4080c4..03af383 100644 --- a/AccountingWarehouseProducts/AccountingWarehouseProductsDataModels/Models/IStandModel.cs +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDataModels/Models/IStandModel.cs @@ -8,12 +8,13 @@ namespace AccountingWarehouseProductsDataModels.Models { public interface IStandModel : IId { + string StandName { get; } DateTime? DeliveryDate { get; } int Count { get; } int ProductId { get; } - int SupplierId { get; } + int SupplierId { get; } } } diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabase.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabase.cs new file mode 100644 index 0000000..2a4fe41 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabase.cs @@ -0,0 +1,31 @@ +using AccountingWarehouseProductsDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement +{ + public class AccountingWarehouseProductsDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=AccountingWarehouseProductsDatabase;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Suppliers { get; set; } + public virtual DbSet Warehouses { get; set; } + public virtual DbSet Products { get; set; } + public virtual DbSet Shipments { get; set; } + public virtual DbSet Stands { get; set; } + public virtual DbSet Orders { get; set; } + public virtual DbSet OrderProducts { get; set; } + public virtual DbSet WarehouseProducts { get; set; } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabaseImplement.csproj b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabaseImplement.csproj new file mode 100644 index 0000000..7ec9e6e --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/AccountingWarehouseProductsDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/OrderStorage.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1ed1faa --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,87 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.SearchModels; +using AccountingWarehouseProductsContracts.StoragesContracts; +using AccountingWarehouseProductsContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var element = context.Orders.Include(x => x.Products).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Orders.Include(x => x.Products).ThenInclude(x => x.Product).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new AccountingWarehouseProductsDatabase(); + return context.Orders.Include(x => x.Products).ThenInclude(x => x.Product).Where(x => x.Id == model.Id).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Orders.Include(x => x.Products).ThenInclude(x => x.Product).ToList().Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var newOrder = Models.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 AccountingWarehouseProductsDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + transaction.Commit(); + return order.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ProductStorage.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..26a2710 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,77 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.SearchModels; +using AccountingWarehouseProductsContracts.StoragesContracts; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName)) + { + return new(); + } + using var context = new AccountingWarehouseProductsDatabase(); + return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Products.Select(x => x.GetViewModel).ToList(); + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var element = context.Products.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Products.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName)) && x.ProductName == model.ProductName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + var newProduct = Product.Create(model); + if (newProduct == null) + { + return null; + } + using var context = new AccountingWarehouseProductsDatabase(); + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var component = context.Products.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ShipmentStorage.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ShipmentStorage.cs new file mode 100644 index 0000000..7291345 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/ShipmentStorage.cs @@ -0,0 +1,77 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.SearchModels; +using AccountingWarehouseProductsContracts.StoragesContracts; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Implements +{ + public class ShipmentStorage : IShipmentStorage + { + public List GetFilteredList(ShipmentSearchModel model) + { + if (string.IsNullOrEmpty(model.Recipient)) + { + return new(); + } + using var context = new AccountingWarehouseProductsDatabase(); + return context.Shipments.Where(x => x.Recipient.Contains(model.Recipient)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Shipments.Select(x => x.GetViewModel).ToList(); + } + + public ShipmentViewModel? Delete(ShipmentBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var element = context.Shipments.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shipments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShipmentViewModel? GetElement(ShipmentSearchModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Shipments.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Recipient)) && x.Recipient == model.Recipient || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public ShipmentViewModel? Insert(ShipmentBindingModel model) + { + var newShipment = Shipment.Create(model); + if (newShipment == null) + { + return null; + } + using var context = new AccountingWarehouseProductsDatabase(); + context.Shipments.Add(newShipment); + context.SaveChanges(); + return newShipment.GetViewModel; + } + + public ShipmentViewModel? Update(ShipmentBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var component = context.Shipments.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/StandStorage.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/StandStorage.cs new file mode 100644 index 0000000..3c5bb54 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/StandStorage.cs @@ -0,0 +1,77 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.SearchModels; +using AccountingWarehouseProductsContracts.StoragesContracts; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Implements +{ + public class StandStorage : IStandStorage + { + public List GetFilteredList(StandSearchModel model) + { + if (string.IsNullOrEmpty(model.StandName)) + { + return new(); + } + using var context = new AccountingWarehouseProductsDatabase(); + return context.Stands.Where(x => x.StandName.Contains(model.StandName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Stands.Select(x => x.GetViewModel).ToList(); + } + + public StandViewModel? Delete(StandBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var element = context.Stands.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Stands.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public StandViewModel? GetElement(StandSearchModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Stands.FirstOrDefault(x => (!string.IsNullOrEmpty(model.StandName)) && x.StandName == model.StandName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public StandViewModel? Insert(StandBindingModel model) + { + var newStand = Stand.Create(model); + if (newStand == null) + { + return null; + } + using var context = new AccountingWarehouseProductsDatabase(); + context.Stands.Add(newStand); + context.SaveChanges(); + return newStand.GetViewModel; + } + + public StandViewModel? Update(StandBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var component = context.Stands.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/SupplierStorage.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/SupplierStorage.cs new file mode 100644 index 0000000..7f85239 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/SupplierStorage.cs @@ -0,0 +1,77 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.SearchModels; +using AccountingWarehouseProductsContracts.StoragesContracts; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Implements +{ + public class SupplierStorage : ISupplierStorage + { + public List GetFilteredList(SupplierSearchModel model) + { + if (string.IsNullOrEmpty(model.SupplierName)) + { + return new(); + } + using var context = new AccountingWarehouseProductsDatabase(); + return context.Suppliers.Where(x => x.SupplierName.Contains(model.SupplierName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Suppliers.Select(x => x.GetViewModel).ToList(); + } + + public SupplierViewModel? Delete(SupplierBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var element = context.Suppliers.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Suppliers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public SupplierViewModel? GetElement(SupplierSearchModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Suppliers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SupplierName)) && x.SupplierName == model.SupplierName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public SupplierViewModel? Insert(SupplierBindingModel model) + { + var newSupplier = Supplier.Create(model); + if (newSupplier == null) + { + return null; + } + using var context = new AccountingWarehouseProductsDatabase(); + context.Suppliers.Add(newSupplier); + context.SaveChanges(); + return newSupplier.GetViewModel; + } + + public SupplierViewModel? Update(SupplierBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var component = context.Suppliers.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/WarehouseStorage.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/WarehouseStorage.cs new file mode 100644 index 0000000..321e4eb --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Implements/WarehouseStorage.cs @@ -0,0 +1,86 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.SearchModels; +using AccountingWarehouseProductsContracts.StoragesContracts; +using AccountingWarehouseProductsContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Implements +{ + public class WarehouseStorage : IWarehouseStorage + { + public WarehouseViewModel? Delete(WarehouseBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var element = context.Warehouses.Include(x => x.Products).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Warehouses.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public WarehouseViewModel? GetElement(WarehouseSearchModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Warehouses.Include(x => x.Products).ThenInclude(x => x.Product).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(WarehouseSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new AccountingWarehouseProductsDatabase(); + return context.Warehouses.Include(x => x.Products).ThenInclude(x => x.Product).Where(x => x.Id == model.Id).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AccountingWarehouseProductsDatabase(); + return context.Warehouses.Include(x => x.Products).ThenInclude(x => x.Product).ToList().Select(x => x.GetViewModel).ToList(); + } + + public WarehouseViewModel? Insert(WarehouseBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + var newWarehouse = Models.Warehouse.Create(context, model); + if (newWarehouse == null) + { + return null; + } + context.Warehouses.Add(newWarehouse); + context.SaveChanges(); + return newWarehouse.GetViewModel; + } + + public WarehouseViewModel? Update(WarehouseBindingModel model) + { + using var context = new AccountingWarehouseProductsDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var warehouse = context.Warehouses.FirstOrDefault(x => x.Id == model.Id); + if (warehouse == null) + { + return null; + } + context.SaveChanges(); + transaction.Commit(); + return warehouse.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.Designer.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.Designer.cs new file mode 100644 index 0000000..6dcfae3 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.Designer.cs @@ -0,0 +1,281 @@ +// +using System; +using AccountingWarehouseProductsDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AccountingWarehouseProductsDatabaseImplement.Migrations +{ + [DbContext(typeof(AccountingWarehouseProductsDatabase))] + [Migration("20240421151136_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DateofOrder") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.OrderProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("OrderId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ProductId"); + + b.ToTable("OrderProducts"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Category") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("ExpirationDate") + .IsRequired() + .HasColumnType("datetime2"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Shipment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("OrderId") + .HasColumnType("int"); + + b.Property("Recipient") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShipmentDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shipments"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Stand", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DeliveryDate") + .HasColumnType("datetime2"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("StandName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SupplierId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Stands"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Supplier", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ContactPerson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Phone") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SupplierName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Suppliers"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Warehouse", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Capacity") + .HasColumnType("int"); + + b.Property("WarehouseName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Warehouses"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.WarehouseProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("WarehouseId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("WarehouseId"); + + b.ToTable("WarehouseProducts"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.OrderProduct", b => + { + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Order", "Order") + .WithMany("Products") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.WarehouseProduct", b => + { + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Warehouse", "Warehouse") + .WithMany("Products") + .HasForeignKey("WarehouseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + + b.Navigation("Warehouse"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Order", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Warehouse", b => + { + b.Navigation("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.cs new file mode 100644 index 0000000..9abaaf1 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/20240421151136_InitMigration.cs @@ -0,0 +1,210 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AccountingWarehouseProductsDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DateofOrder = table.Column(type: "datetime2", nullable: true), + Status = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false), + ExpirationDate = table.Column(type: "datetime2", nullable: false), + Category = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Shipments", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShipmentDate = table.Column(type: "datetime2", nullable: false), + Count = table.Column(type: "int", nullable: false), + Recipient = table.Column(type: "nvarchar(max)", nullable: false), + OrderId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shipments", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Stands", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + StandName = table.Column(type: "nvarchar(max)", nullable: false), + DeliveryDate = table.Column(type: "datetime2", nullable: true), + Count = table.Column(type: "int", nullable: false), + ProductId = table.Column(type: "int", nullable: false), + SupplierId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Stands", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Suppliers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SupplierName = table.Column(type: "nvarchar(max)", nullable: false), + ContactPerson = table.Column(type: "nvarchar(max)", nullable: false), + Phone = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Suppliers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Warehouses", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WarehouseName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + Capacity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Warehouses", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "OrderProducts", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + OrderId = table.Column(type: "int", nullable: false), + ProductId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_OrderProducts", x => x.Id); + table.ForeignKey( + name: "FK_OrderProducts_Orders_OrderId", + column: x => x.OrderId, + principalTable: "Orders", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_OrderProducts_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "WarehouseProducts", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + WarehouseId = table.Column(type: "int", nullable: false), + ProductId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_WarehouseProducts", x => x.Id); + table.ForeignKey( + name: "FK_WarehouseProducts_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_WarehouseProducts_Warehouses_WarehouseId", + column: x => x.WarehouseId, + principalTable: "Warehouses", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_OrderProducts_OrderId", + table: "OrderProducts", + column: "OrderId"); + + migrationBuilder.CreateIndex( + name: "IX_OrderProducts_ProductId", + table: "OrderProducts", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_WarehouseProducts_ProductId", + table: "WarehouseProducts", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_WarehouseProducts_WarehouseId", + table: "WarehouseProducts", + column: "WarehouseId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "OrderProducts"); + + migrationBuilder.DropTable( + name: "Shipments"); + + migrationBuilder.DropTable( + name: "Stands"); + + migrationBuilder.DropTable( + name: "Suppliers"); + + migrationBuilder.DropTable( + name: "WarehouseProducts"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Products"); + + migrationBuilder.DropTable( + name: "Warehouses"); + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/AccountingWarehouseProductsDatabaseModelSnapshot.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/AccountingWarehouseProductsDatabaseModelSnapshot.cs new file mode 100644 index 0000000..7fff081 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Migrations/AccountingWarehouseProductsDatabaseModelSnapshot.cs @@ -0,0 +1,278 @@ +// +using System; +using AccountingWarehouseProductsDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AccountingWarehouseProductsDatabaseImplement.Migrations +{ + [DbContext(typeof(AccountingWarehouseProductsDatabase))] + partial class AccountingWarehouseProductsDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DateofOrder") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.OrderProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("OrderId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("OrderId"); + + b.HasIndex("ProductId"); + + b.ToTable("OrderProducts"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Category") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("ExpirationDate") + .IsRequired() + .HasColumnType("datetime2"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Shipment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("OrderId") + .HasColumnType("int"); + + b.Property("Recipient") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShipmentDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shipments"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Stand", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DeliveryDate") + .HasColumnType("datetime2"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("StandName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SupplierId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Stands"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Supplier", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ContactPerson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Phone") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SupplierName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Suppliers"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Warehouse", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Capacity") + .HasColumnType("int"); + + b.Property("WarehouseName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Warehouses"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.WarehouseProduct", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("WarehouseId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.HasIndex("WarehouseId"); + + b.ToTable("WarehouseProducts"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.OrderProduct", b => + { + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Order", "Order") + .WithMany("Products") + .HasForeignKey("OrderId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Order"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.WarehouseProduct", b => + { + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Product", "Product") + .WithMany() + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AccountingWarehouseProductsDatabaseImplement.Models.Warehouse", "Warehouse") + .WithMany("Products") + .HasForeignKey("WarehouseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Product"); + + b.Navigation("Warehouse"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Order", b => + { + b.Navigation("Products"); + }); + + modelBuilder.Entity("AccountingWarehouseProductsDatabaseImplement.Models.Warehouse", b => + { + b.Navigation("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Order.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..d23e21a --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Order.cs @@ -0,0 +1,97 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + + public DateTime? DateofOrder { get; set; } + + public AccountingWarehouseProductsDataModels.Enums.OrderStatus Status { get; set; } + + private Dictionary? _orderProduct = null; + + [NotMapped] + public Dictionary OrderProducts + { + get + { + if (_orderProduct == null) + { + _orderProduct = Products.ToDictionary(recOP => recOP.ProductId, recOP => (recOP.Product as IProductModel, recOP.Count)); + } + return _orderProduct; + } + } + + [ForeignKey("OrderId")] + public virtual List Products { get; set; } = new(); + + public static Order Create(AccountingWarehouseProductsDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + DateofOrder = model.DateofOrder, + Status = model.Status, + Products = model.OrderProducts.Select(x => new OrderProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + }; + } + + public void Update(OrderBindingModel model) + { + Status = model.Status; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + DateofOrder = DateofOrder, + Status = Status, + OrderProducts = OrderProducts + }; + + public void UpdateProducts(AccountingWarehouseProductsDatabase context, OrderBindingModel model) + { + var orderProducts = context.OrderProducts.Where(rec => rec.OrderId == model.Id).ToList(); + if (orderProducts != null && orderProducts.Count > 0) + { + context.OrderProducts.RemoveRange(orderProducts.Where(rec => !model.OrderProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + + foreach (var updateProduct in orderProducts) + { + updateProduct.Count = model.OrderProducts[updateProduct.ProductId].Item2; + model.OrderProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + + var order = context.Orders.First(x => x.Id == Id); + foreach (var rc in model.OrderProducts) + { + context.OrderProducts.Add(new OrderProduct + { + Order = order, + Product = context.Products.First(x => x.Id == rc.Key), + Count = rc.Value.Item2 + }); + context.SaveChanges(); + } + _orderProduct = null; + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/OrderProduct.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/OrderProduct.cs new file mode 100644 index 0000000..fcf2d6b --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/OrderProduct.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class OrderProduct + { + public int Id { get; set; } + + public int OrderId { get; set; } + public int ProductId { get; set; } + + public int Count { get; set; } + + public virtual Order Order { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Product.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..cdf0c79 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Product.cs @@ -0,0 +1,60 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class Product + { + public int Id { get; set; } + + [Required] + public string ProductName { get; set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [Required] + public DateTime? ExpirationDate { get; set; } + + [Required] + public string Category { get; set; } = string.Empty; + + public static Product? Create(ProductBindingModel model) + { + if (model == null) return null; + + return new Product() + { + Id = model.Id, + ProductName = model.ProductName, + Cost = model.Cost, + ExpirationDate = model.ExpirationDate, + Category = model.Category + }; + } + + public void Update(ProductBindingModel model) + { + if (model == null) return; + ProductName = model.ProductName; + Cost = model.Cost; + ExpirationDate = model.ExpirationDate; + Category = model.Category; + } + + public ProductViewModel GetViewModel => new() + { + Id = Id, + ProductName = ProductName, + Cost = Cost, + ExpirationDate = ExpirationDate, + Category = Category + }; + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Shipment.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Shipment.cs new file mode 100644 index 0000000..faa1bba --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Shipment.cs @@ -0,0 +1,60 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class Shipment + { + public int Id { get; set; } + + [Required] + public DateTime ShipmentDate { get; set; } + + [Required] + public int Count { get; set; } + + [Required] + public string Recipient { get; set; } = string.Empty; + + [Required] + public int OrderId { get; set; } + + public static Shipment? Create(ShipmentBindingModel model) + { + if (model == null) return null; + + return new Shipment() + { + Id = model.Id, + ShipmentDate = model.ShipmentDate, + Count = model.Count, + Recipient = model.Recipient, + OrderId = model.OrderId + }; + } + + public void Update(ShipmentBindingModel model) + { + if (model == null) return; + ShipmentDate = model.ShipmentDate; + Count = model.Count; + Recipient = model.Recipient; + OrderId = model.OrderId; + } + + public ShipmentViewModel GetViewModel => new() + { + Id = Id, + ShipmentDate = ShipmentDate, + Count = Count, + Recipient = Recipient, + OrderId = OrderId + }; + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Stand.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Stand.cs new file mode 100644 index 0000000..6e71f7c --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Stand.cs @@ -0,0 +1,60 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class Stand + { + public int Id { get; set; } + + public string StandName { get; set; } = string.Empty; + + public DateTime? DeliveryDate { get; set; } + + public int Count { get; set; } + + public int ProductId { get; set; } + + public int SupplierId { get; set; } + + public static Stand? Create(StandBindingModel model) + { + if (model == null) return null; + + return new Stand() + { + Id = model.Id, + StandName = model.StandName, + DeliveryDate = model.DeliveryDate, + Count = model.Count, + ProductId = model.ProductId, + SupplierId = model.SupplierId + }; + } + + public void Update(StandBindingModel model) + { + if (model == null) return; + StandName = model.StandName; + DeliveryDate = model.DeliveryDate; + Count = model.Count; + ProductId = model.ProductId; + SupplierId = model.SupplierId; + } + + public StandViewModel GetViewModel => new() + { + Id = Id, + StandName = StandName, + DeliveryDate = DeliveryDate, + Count = Count, + ProductId = ProductId, + SupplierId = SupplierId + }; + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Supplier.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Supplier.cs new file mode 100644 index 0000000..84ef3d8 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Supplier.cs @@ -0,0 +1,56 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class Supplier : ISupplierModel + { + public int Id { get; set; } + + [Required] + public string SupplierName { get; set; } = string.Empty; + + [Required] + public string ContactPerson { get; set; } = string.Empty; + + [Required] + public string Phone { get; set; } = string.Empty; + + public static Supplier? Create(SupplierBindingModel model) + { + if (model == null) return null; + + return new Supplier() + { + Id = model.Id, + SupplierName = model.SupplierName, + ContactPerson = model.ContactPerson, + Phone = model.Phone + }; + } + + public void Update(SupplierBindingModel model) + { + if (model == null) return; + SupplierName = model.SupplierName; + ContactPerson = model.ContactPerson; + Phone = model.Phone; + } + + public SupplierViewModel GetViewModel => new() + { + Id = Id, + SupplierName = SupplierName, + ContactPerson = ContactPerson, + Phone = Phone + }; + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Warehouse.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Warehouse.cs new file mode 100644 index 0000000..7735c86 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/Warehouse.cs @@ -0,0 +1,99 @@ +using AccountingWarehouseProductsContracts.BindingModels; +using AccountingWarehouseProductsContracts.ViewModels; +using AccountingWarehouseProductsDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class Warehouse + { + public int Id { get; set; } + + [Required] + public string WarehouseName { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + [Required] + public int Capacity { get; set; } + + private Dictionary? _warehouseProduct = null; + + [NotMapped] + public Dictionary WarehouseProducts + { + get + { + if (_warehouseProduct == null) + { + _warehouseProduct = Products.ToDictionary(recWP => recWP.ProductId, recWP => (recWP.Product as IProductModel, recWP.Count)); + } + return _warehouseProduct; + } + } + + [ForeignKey("WarehouseId")] + public virtual List Products { get; set; } = new(); + + public static Warehouse Create(AccountingWarehouseProductsDatabase context, WarehouseBindingModel model) + { + return new Warehouse() + { + Id = model.Id, + WarehouseName = model.WarehouseName, + Address = model.Address, + Capacity = model.Capacity, + Products = model.WarehouseProducts.Select(x => new WarehouseProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + }; + } + + public WarehouseViewModel GetViewModel => new() + { + Id = Id, + WarehouseName = WarehouseName, + Address = Address, + Capacity = Capacity, + WarehouseProducts = WarehouseProducts + }; + + public void UpdateProducts(AccountingWarehouseProductsDatabase context, WarehouseBindingModel model) + { + var warehouseProducts = context.WarehouseProducts.Where(rec => rec.WarehouseId == model.Id).ToList(); + if (warehouseProducts != null && warehouseProducts.Count > 0) + { + context.WarehouseProducts.RemoveRange(warehouseProducts.Where(rec => !model.WarehouseProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + + foreach (var updateProduct in warehouseProducts) + { + updateProduct.Count = model.WarehouseProducts[updateProduct.ProductId].Item2; + model.WarehouseProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + + var warehouse = context.Warehouses.First(x => x.Id == Id); + foreach (var rc in model.WarehouseProducts) + { + context.WarehouseProducts.Add(new WarehouseProduct + { + Warehouse = warehouse, + Product = context.Products.First(x => x.Id == rc.Key), + Count = rc.Value.Item2 + }); + context.SaveChanges(); + } + _warehouseProduct = null; + } + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/WarehouseProduct.cs b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/WarehouseProduct.cs new file mode 100644 index 0000000..e90d191 --- /dev/null +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsDatabaseImplement/Models/WarehouseProduct.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AccountingWarehouseProductsDatabaseImplement.Models +{ + public class WarehouseProduct + { + public int Id { get; set; } + + public int WarehouseId { get; set; } + public int ProductId { get; set; } + + public int Count { get; set; } + + public virtual Warehouse Warehouse { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} diff --git a/AccountingWarehouseProducts/AccountingWarehouseProductsView/AccountingWarehouseProductsView.csproj b/AccountingWarehouseProducts/AccountingWarehouseProductsView/AccountingWarehouseProductsView.csproj index b57c89e..5ad16fa 100644 --- a/AccountingWarehouseProducts/AccountingWarehouseProductsView/AccountingWarehouseProductsView.csproj +++ b/AccountingWarehouseProducts/AccountingWarehouseProductsView/AccountingWarehouseProductsView.csproj @@ -8,4 +8,15 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + \ No newline at end of file