From cb37c8ab52be492fdad67f018d0635eee4ef5695 Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Sun, 23 Jun 2024 01:24:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=B7=D0=B0=D1=85=D0=B2=D0=B0=D1=82=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=B5=D0=B2=D1=80=D0=BE=D0=BF=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=D0=B5=20=D0=B3=D0=BE=D1=81=D1=83=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D1=81=D1=82=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BusinessLogic/BusinessLogic.csproj | 1 + BusinessLogic/BusinessLogic/PurchaseLogic.cs | 99 +++++++++++++++++++ .../BindingModels/PurchaseBindingModel.cs | 7 +- Contracts/Converters/PurchaseConverter.cs | 29 ++++++ DatabaseImplement/Database.cs | 1 - 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 BusinessLogic/BusinessLogic/PurchaseLogic.cs create mode 100644 Contracts/Converters/PurchaseConverter.cs diff --git a/BusinessLogic/BusinessLogic.csproj b/BusinessLogic/BusinessLogic.csproj index ceab2d9..62335f6 100644 --- a/BusinessLogic/BusinessLogic.csproj +++ b/BusinessLogic/BusinessLogic.csproj @@ -18,6 +18,7 @@ + diff --git a/BusinessLogic/BusinessLogic/PurchaseLogic.cs b/BusinessLogic/BusinessLogic/PurchaseLogic.cs new file mode 100644 index 0000000..1d46e64 --- /dev/null +++ b/BusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -0,0 +1,99 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.Converters; +using Contracts.Exceptions; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Implements; +using DatabaseImplement.Models; +using DataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BusinessLogic.BusinessLogic +{ + public class PurchaseLogic : IPurchaseLogic + { + private readonly IPurchaseStorage _purchaseStorage; + private readonly ILogger _logger; + + public PurchaseLogic(IPurchaseStorage purchaseStorage, ILogger logger) + { + _purchaseStorage = purchaseStorage; + _logger = logger; + } + + public PurchaseViewModel Create(PurchaseBindingModel model) + { + ArgumentNullException.ThrowIfNull(model); + + var purchase = _purchaseStorage.Insert(model); + if (purchase is null) + { + throw new Exception("Insert operation failed."); + } + + return PurchaseConverter.ToView(purchase); + } + + public PurchaseViewModel Delete(PurchaseSearchModel model) + { + ArgumentNullException.ThrowIfNull(model); + + _logger.LogInformation("Delete purchase. Id: {0}", model.Id); + var purchase = _purchaseStorage.Delete(model); + if (purchase is null) + { + throw new Exception("Delete operation failed."); + } + + return PurchaseConverter.ToView(purchase); + } + + public PurchaseViewModel ReadElement(PurchaseSearchModel model) + { + ArgumentNullException.ThrowIfNull(model); + + _logger.LogInformation("ReadElement. Id: {0}", model.Id); + var purchase = _purchaseStorage.GetElement(model); + if (purchase is null) + { + throw new ElementNotFoundException(); + } + _logger.LogInformation("ReadElement find. Id: {0}", purchase.Id); + + return PurchaseConverter.ToView(purchase); + } + + public IEnumerable ReadElements(PurchaseSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}", model?.Id); + var purchase_list = _purchaseStorage.GetList(model); + if (purchase_list is null || purchase_list.Count() == 0) + { + _logger.LogWarning("ReadList return null list"); + return []; + } + _logger.LogInformation("ReadList. Count: {Count}", purchase_list.Count()); + + return purchase_list.Select(PurchaseConverter.ToView); + } + + public PurchaseViewModel Update(PurchaseBindingModel model) + { + ArgumentNullException.ThrowIfNull(model); + + var purchase = _purchaseStorage.Update(model); + if (purchase is null) + { + throw new Exception("Update operation failed."); + } + return PurchaseConverter.ToView(purchase); + } + } +} diff --git a/Contracts/BindingModels/PurchaseBindingModel.cs b/Contracts/BindingModels/PurchaseBindingModel.cs index c0e577d..14433e7 100644 --- a/Contracts/BindingModels/PurchaseBindingModel.cs +++ b/Contracts/BindingModels/PurchaseBindingModel.cs @@ -1,4 +1,6 @@ -using System; +using DataModels.Enums; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,5 +12,8 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public DateTime DatePurchase { get; set; } + public required IUser User { get; set; } + public required List Products { get; set; } + public PurchaseStatus Status { get; set; } } } diff --git a/Contracts/Converters/PurchaseConverter.cs b/Contracts/Converters/PurchaseConverter.cs new file mode 100644 index 0000000..1b181f5 --- /dev/null +++ b/Contracts/Converters/PurchaseConverter.cs @@ -0,0 +1,29 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.Converters +{ + public class PurchaseConverter + { + public static PurchaseViewModel ToView(PurchaseBindingModel model) => new() + { + Id = model.Id, + DatePurchase = model.DatePurchase, + User = model.User, + Products = model.Products, + }; + + public static PurchaseBindingModel ToBinding(PurchaseViewModel model) => new() + { + Id = model.Id, + DatePurchase = model.DatePurchase, + User = model.User, + Products = model.Products, + }; + } +} diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index f17e76a..7cfb7f0 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -28,7 +28,6 @@ namespace DatabaseImplement public virtual DbSet SupplyProducts { get; set; } = null!; public virtual DbSet Suppliers { get; set; } = null!; public virtual DbSet SupplierProducts { get; set; } = null!; - public virtual DbSet MediaFiles { get; set; } = null!; } } \ No newline at end of file