diff --git a/BusinessLogic/BusinessLogic/SellLogic.cs b/BusinessLogic/BusinessLogic/SellLogic.cs index aacd8e5..99c9230 100644 --- a/BusinessLogic/BusinessLogic/SellLogic.cs +++ b/BusinessLogic/BusinessLogic/SellLogic.cs @@ -45,7 +45,7 @@ namespace BusinessLogic.BusinessLogic } public IEnumerable GetElements(SellSearchModel? model) { - var sell_list = _sellStorage.GetList(model); + var sell_list = model == null ? _sellStorage.GetFullList(model) : _sellStorage.GetFilteredList(model); if (sell_list is null || sell_list.Count() == 0) { _logger.LogWarning("ReadList return null list"); diff --git a/Contracts/StorageContracts/IPurchaseStorage.cs b/Contracts/StorageContracts/IPurchaseStorage.cs index 855831a..1b141ec 100644 --- a/Contracts/StorageContracts/IPurchaseStorage.cs +++ b/Contracts/StorageContracts/IPurchaseStorage.cs @@ -1,5 +1,6 @@ using Contracts.BindingModels; using Contracts.SearchModels; +using Contracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -10,14 +11,15 @@ namespace Contracts.StorageContracts { public interface IPurchaseStorage { - PurchaseBindingModel? Insert(PurchaseBindingModel model); + PurchaseViewModel? Insert(PurchaseBindingModel model); - IEnumerable GetList(PurchaseSearchModel? model); + List GetFullList(PurchaseSearchModel? model); + List GetFilteredList(PurchaseSearchModel? model); - PurchaseBindingModel? GetElement(PurchaseSearchModel model); + PurchaseViewModel? GetElement(PurchaseSearchModel model); - PurchaseBindingModel? Update(PurchaseBindingModel model); + PurchaseViewModel? Update(PurchaseBindingModel model); - PurchaseBindingModel? Delete(PurchaseSearchModel model); + PurchaseViewModel? Delete(PurchaseSearchModel model); } } diff --git a/DatabaseImplement/Implements/PurchaseStorage.cs b/DatabaseImplement/Implements/PurchaseStorage.cs index 2323dd2..7efd5fa 100644 --- a/DatabaseImplement/Implements/PurchaseStorage.cs +++ b/DatabaseImplement/Implements/PurchaseStorage.cs @@ -1,6 +1,9 @@ using Contracts.BindingModels; using Contracts.SearchModels; using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -11,29 +14,87 @@ namespace DatabaseImplement.Implements { public class PurchaseStorage : IPurchaseStorage { - public PurchaseBindingModel? Delete(PurchaseSearchModel model) + public PurchaseViewModel? Delete(PurchaseSearchModel model) { - throw new NotImplementedException(); - } + using var context = new Database(); + var element = context.Purchases.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Purchases.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } - public PurchaseBindingModel? GetElement(PurchaseSearchModel model) + public PurchaseViewModel? GetElement(PurchaseSearchModel model) { - throw new NotImplementedException(); - } + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.Purchases + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } - public IEnumerable GetList(PurchaseSearchModel? model) + public List GetFullList(PurchaseSearchModel? model) { - throw new NotImplementedException(); - } + using var context = new Database(); + return context.Purchases + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PurchaseSearchModel? model) + { + using var context = new Database(); + return context.Purchases + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } - public PurchaseBindingModel? Insert(PurchaseBindingModel model) + public PurchaseViewModel? Insert(PurchaseBindingModel model) { - throw new NotImplementedException(); - } + using var context = new Database(); + var purchase = Purchase.Create(context, model); + if (purchase == null) + return null; + context.Purchases.Add(purchase); + context.SaveChanges(); + return purchase.GetViewModel; + } - public PurchaseBindingModel? Update(PurchaseBindingModel model) + public PurchaseViewModel? Update(PurchaseBindingModel model) { - throw new NotImplementedException(); - } + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var purchase = context.Purchases.FirstOrDefault(rec => + rec.Id == model.Id); + if (purchase == null) + { + return null; + } + purchase.Update(model); + context.SaveChanges(); + purchase.UpdateProducts(context, model); + transaction.Commit(); + return purchase.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } } } diff --git a/DatabaseImplement/Implements/SellStorage.cs b/DatabaseImplement/Implements/SellStorage.cs index 090188f..d82d531 100644 --- a/DatabaseImplement/Implements/SellStorage.cs +++ b/DatabaseImplement/Implements/SellStorage.cs @@ -17,13 +17,24 @@ namespace DatabaseImplement.Implements { public SellViewModel? Delete(SellSearchModel model) { - throw new NotImplementedException(); + using var context = new Database(); + var element = context.Sells.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Sells.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } public SellViewModel? GetElement(SellSearchModel model) { - throw new NotImplementedException(); - } + using var context = new Database(); + return context.Sells + .Include(x => x.SupplyDoc) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } public IEnumerable GetFilteredList(SellSearchModel? model) { @@ -36,7 +47,7 @@ namespace DatabaseImplement.Implements return context.Sells .Include(x => x.SupplyDoc) .ToList() - .Select((Sell sell, int index) => sell.GetViewModel()) + .Select((Sell sell, int index) => sell.GetViewModel) .ToList(); } @@ -50,7 +61,7 @@ namespace DatabaseImplement.Implements } context.Sells.Add(sell); context.SaveChanges(); - return sell.GetViewModel(); + return sell.GetViewModel; } public SellViewModel? Update(SellBindingModel model) @@ -67,7 +78,7 @@ namespace DatabaseImplement.Implements sell.Update(model); context.SaveChanges(); transaction.Commit(); - return sell.GetViewModel(); + return sell.GetViewModel; } catch { diff --git a/DatabaseImplement/Models/Purchase.cs b/DatabaseImplement/Models/Purchase.cs index a3e999e..aa552b1 100644 --- a/DatabaseImplement/Models/Purchase.cs +++ b/DatabaseImplement/Models/Purchase.cs @@ -1,4 +1,5 @@ using Contracts.BindingModels; +using Contracts.SearchModels; using Contracts.ViewModels; using DataModels.Enums; using DataModels.Models; @@ -11,6 +12,7 @@ using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace DatabaseImplement.Models { @@ -41,7 +43,7 @@ namespace DatabaseImplement.Models } [ForeignKey("PurchaseId")] public virtual List Products { get; set; } = new(); - public static Purchase Create(Database context, SellBindingModel model) + public static Purchase Create(Database context, PurchaseBindingModel model) { return new Purchase() { @@ -51,7 +53,7 @@ namespace DatabaseImplement.Models }; } - public PurchaseBindingModel GetBindingModel() => new() + public PurchaseBindingModel GetBindingModel => new() { Id = Id, DatePurchase = DatePurchase, @@ -60,7 +62,7 @@ namespace DatabaseImplement.Models Status = Status }; - public PurchaseViewModel GetViewModel() => new() + public PurchaseViewModel GetViewModel => new() { Id = Id, DatePurchase = DatePurchase, @@ -87,14 +89,48 @@ namespace DatabaseImplement.Models Status = model.Status }; - public void Update(PurchaseBindingModel model, Purchase purchase) + public void Update(PurchaseBindingModel model) { if (model is null) { throw new ArgumentNullException("Update purchase: binding model is null"); } - DatePurchase = purchase.DatePurchase; + DatePurchase = model.DatePurchase; + UserId = model.UserId; + PurchaseProducts = model.PurchaseProducts; + Status = model.Status; } - } + public void UpdateProducts(Database context, PurchaseBindingModel model) + { + var purchaseProducts = context.PurchaseProducts.Where(rec => + rec.Id == model.Id).ToList(); + if (purchaseProducts != null && purchaseProducts.Count > 0) + { // удалили те, которых нет в модели + context.PurchaseProducts.RemoveRange(purchaseProducts.Where(rec + => !model.PurchaseProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateProduct in purchaseProducts) + { + updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2; + model.PurchaseProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + var purchase = context.Purchases.First(x => x.Id == Id); + foreach (var pc in model.PurchaseProducts) + { + context.PurchaseProducts.Add(new PurchaseProducts + { + Purchase = purchase, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _purchaseProducts = null; + } + + } } diff --git a/DatabaseImplement/Models/Sell.cs b/DatabaseImplement/Models/Sell.cs index 74a441e..e986b30 100644 --- a/DatabaseImplement/Models/Sell.cs +++ b/DatabaseImplement/Models/Sell.cs @@ -27,13 +27,13 @@ namespace DatabaseImplement.Models }; } - public SellBindingModel GetBindingModel() => new() + public SellBindingModel GetBindingModel => new() { Id = Id, DateSell = DateSell, SupplyDocId = SupplyDocId, }; - public SellViewModel GetViewModel() => new() + public SellViewModel GetViewModel => new() { Id = Id, DateSell = DateSell,