From 96c186a5906be43eab312fb08c7d13adeb907890 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 1 May 2024 11:55:41 +0400 Subject: [PATCH] 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