From c7e0c3823aa874866cb3c87a04df4c6a692bd618 Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Tue, 11 Jun 2024 12:18:10 +0400 Subject: [PATCH 1/8] scroogie models update --- .../BindingModels/MediaFileBindingModel.cs | 15 ++++++++++++ .../BindingModels/ProductBindingModel.cs | 16 +++++++++++++ .../BindingModels/PurchaseBindingModel.cs | 14 +++++++++++ Contracts/BindingModels/SellBindingModel.cs | 13 ++++++++++ .../IEmployeeProductLogic.cs | 24 +++++++++++++++++++ .../BusinessLogicContracts/IPurchaseLogic.cs | 24 +++++++++++++++++++ .../BusinessLogicContracts/ISellLogic.cs | 24 +++++++++++++++++++ .../IUserProductLogic.cs | 21 ++++++++++++++++ Contracts/Contracts.csproj | 4 ++++ Contracts/SearchModels/ProductSearchModel.cs | 16 +++++++++++++ Contracts/SearchModels/PurchaseSearchModel.cs | 20 ++++++++++++++++ Contracts/SearchModels/SellSearchModel.cs | 16 +++++++++++++ Contracts/StorageContracts/IProductStorage.cs | 23 ++++++++++++++++++ .../StorageContracts/IPurchaseStorage.cs | 23 ++++++++++++++++++ Contracts/StorageContracts/ISellStorage.cs | 23 ++++++++++++++++++ .../ViewModels/EmployeeProductViewModel.cs | 15 ++++++++++++ Contracts/ViewModels/PurchaseViewModel.cs | 19 +++++++++++++++ Contracts/ViewModels/SellViewModel.cs | 16 +++++++++++++ Contracts/ViewModels/UserProductViewModel.cs | 15 ++++++++++++ DataModels/Enums/PurchaseStatus.cs | 15 ++++++++++++ DataModels/Models/IMediaFile.cs | 14 +++++++++++ DataModels/Models/IProduct.cs | 17 +++++++++++++ DataModels/Models/IPurchase.cs | 15 ++++++++++++ DataModels/Models/ISell.cs | 13 ++++++++++ DataModels/Models/ISupplyDoc.cs | 13 ++++++++++ DatabaseImplement/Models/MediaFile.cs | 18 ++++++++++++++ DatabaseImplement/Models/Product.cs | 12 ++++++++++ DatabaseImplement/Models/Purchase.cs | 21 ++++++++++++++++ DatabaseImplement/Models/Sell.cs | 12 ++++++++++ 29 files changed, 491 insertions(+) create mode 100644 Contracts/BindingModels/MediaFileBindingModel.cs create mode 100644 Contracts/BindingModels/ProductBindingModel.cs create mode 100644 Contracts/BindingModels/PurchaseBindingModel.cs create mode 100644 Contracts/BindingModels/SellBindingModel.cs create mode 100644 Contracts/BusinessLogicContracts/IEmployeeProductLogic.cs create mode 100644 Contracts/BusinessLogicContracts/IPurchaseLogic.cs create mode 100644 Contracts/BusinessLogicContracts/ISellLogic.cs create mode 100644 Contracts/BusinessLogicContracts/IUserProductLogic.cs create mode 100644 Contracts/SearchModels/ProductSearchModel.cs create mode 100644 Contracts/SearchModels/PurchaseSearchModel.cs create mode 100644 Contracts/SearchModels/SellSearchModel.cs create mode 100644 Contracts/StorageContracts/IProductStorage.cs create mode 100644 Contracts/StorageContracts/IPurchaseStorage.cs create mode 100644 Contracts/StorageContracts/ISellStorage.cs create mode 100644 Contracts/ViewModels/EmployeeProductViewModel.cs create mode 100644 Contracts/ViewModels/PurchaseViewModel.cs create mode 100644 Contracts/ViewModels/SellViewModel.cs create mode 100644 Contracts/ViewModels/UserProductViewModel.cs create mode 100644 DataModels/Enums/PurchaseStatus.cs create mode 100644 DataModels/Models/IMediaFile.cs create mode 100644 DataModels/Models/IProduct.cs create mode 100644 DataModels/Models/IPurchase.cs create mode 100644 DataModels/Models/ISell.cs create mode 100644 DataModels/Models/ISupplyDoc.cs create mode 100644 DatabaseImplement/Models/MediaFile.cs create mode 100644 DatabaseImplement/Models/Product.cs create mode 100644 DatabaseImplement/Models/Purchase.cs create mode 100644 DatabaseImplement/Models/Sell.cs diff --git a/Contracts/BindingModels/MediaFileBindingModel.cs b/Contracts/BindingModels/MediaFileBindingModel.cs new file mode 100644 index 0000000..656354a --- /dev/null +++ b/Contracts/BindingModels/MediaFileBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class MediaFileBindingModel + { + public Guid Id { get; set; } + public Guid ProductId { get; set; } + public string Location { get; set; } = string.Empty; + } +} diff --git a/Contracts/BindingModels/ProductBindingModel.cs b/Contracts/BindingModels/ProductBindingModel.cs new file mode 100644 index 0000000..6be324e --- /dev/null +++ b/Contracts/BindingModels/ProductBindingModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class ProductBindingModel + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public double Price { get; set; } + public int Amount { get; set; } + } +} diff --git a/Contracts/BindingModels/PurchaseBindingModel.cs b/Contracts/BindingModels/PurchaseBindingModel.cs new file mode 100644 index 0000000..c0e577d --- /dev/null +++ b/Contracts/BindingModels/PurchaseBindingModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class PurchaseBindingModel + { + public Guid Id { get; set; } + public DateTime DatePurchase { get; set; } + } +} diff --git a/Contracts/BindingModels/SellBindingModel.cs b/Contracts/BindingModels/SellBindingModel.cs new file mode 100644 index 0000000..7f2c663 --- /dev/null +++ b/Contracts/BindingModels/SellBindingModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class SellBindingModel + { + public DateTime DateSell { get; set; } + } +} diff --git a/Contracts/BusinessLogicContracts/IEmployeeProductLogic.cs b/Contracts/BusinessLogicContracts/IEmployeeProductLogic.cs new file mode 100644 index 0000000..c61ace2 --- /dev/null +++ b/Contracts/BusinessLogicContracts/IEmployeeProductLogic.cs @@ -0,0 +1,24 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface IEmployeeProductLogic + { + EmployeeProductViewModel Create(ProductBindingModel model); + + EmployeeProductViewModel Update(ProductBindingModel model); + + EmployeeProductViewModel ReadElement(ProductSearchModel model); + + IEnumerable ReadElements(ProductSearchModel? model); + + EmployeeProductViewModel Delete(ProductSearchModel model); + } +} diff --git a/Contracts/BusinessLogicContracts/IPurchaseLogic.cs b/Contracts/BusinessLogicContracts/IPurchaseLogic.cs new file mode 100644 index 0000000..65892dd --- /dev/null +++ b/Contracts/BusinessLogicContracts/IPurchaseLogic.cs @@ -0,0 +1,24 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface IPurchaseLogic + { + PurchaseViewModel Create(PurchaseBindingModel model); + + PurchaseViewModel Update(PurchaseBindingModel model); + + PurchaseViewModel ReadElement(PurchaseSearchModel model); + + IEnumerable ReadElements(PurchaseSearchModel? model); + + PurchaseViewModel Delete(PurchaseSearchModel model); + } +} diff --git a/Contracts/BusinessLogicContracts/ISellLogic.cs b/Contracts/BusinessLogicContracts/ISellLogic.cs new file mode 100644 index 0000000..471cfb2 --- /dev/null +++ b/Contracts/BusinessLogicContracts/ISellLogic.cs @@ -0,0 +1,24 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface ISellLogic + { + SellViewModel Create(SellBindingModel model); + + SellViewModel Update(SellBindingModel model); + + SellViewModel ReadElement(SellSearchModel model); + + IEnumerable ReadElements(SellSearchModel? model); + + SellViewModel Delete(SellSearchModel model); + } +} diff --git a/Contracts/BusinessLogicContracts/IUserProductLogic.cs b/Contracts/BusinessLogicContracts/IUserProductLogic.cs new file mode 100644 index 0000000..049cfdc --- /dev/null +++ b/Contracts/BusinessLogicContracts/IUserProductLogic.cs @@ -0,0 +1,21 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface IUserProductLogic + { + + UserProductViewModel ReadElement(ProductSearchModel model); + + IEnumerable ReadElements(ProductSearchModel? model); + + UserProductViewModel Delete(ProductSearchModel model); + } +} diff --git a/Contracts/Contracts.csproj b/Contracts/Contracts.csproj index fa71b7a..f1b5623 100644 --- a/Contracts/Contracts.csproj +++ b/Contracts/Contracts.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/Contracts/SearchModels/ProductSearchModel.cs b/Contracts/SearchModels/ProductSearchModel.cs new file mode 100644 index 0000000..a6527dd --- /dev/null +++ b/Contracts/SearchModels/ProductSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.SearchModels +{ + public class ProductSearchModel + { + public Guid? Id { get; set; } + public string? Name { get; set; } + public double? Price { get; set; } + public double? Rate { get; set; } + } +} diff --git a/Contracts/SearchModels/PurchaseSearchModel.cs b/Contracts/SearchModels/PurchaseSearchModel.cs new file mode 100644 index 0000000..1dc89d7 --- /dev/null +++ b/Contracts/SearchModels/PurchaseSearchModel.cs @@ -0,0 +1,20 @@ +using DataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace Contracts.SearchModels +{ + public class PurchaseSearchModel + { + public Guid? Id { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + public double? PriceFrom { get; set; } + public double? PriceTo { get; set; } + public PurchaseStatus? Status { get; set; } + } +} diff --git a/Contracts/SearchModels/SellSearchModel.cs b/Contracts/SearchModels/SellSearchModel.cs new file mode 100644 index 0000000..8564cf4 --- /dev/null +++ b/Contracts/SearchModels/SellSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.SearchModels +{ + public class SellSearchModel + { + public Guid? Id { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + public Guid? SupplyDocId { get; set; } + } +} diff --git a/Contracts/StorageContracts/IProductStorage.cs b/Contracts/StorageContracts/IProductStorage.cs new file mode 100644 index 0000000..49ce4e3 --- /dev/null +++ b/Contracts/StorageContracts/IProductStorage.cs @@ -0,0 +1,23 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.StorageContracts +{ + public interface IProductStorage + { + ProductBindingModel? Insert(ProductBindingModel model); + + IEnumerable GetList(ProductSearchModel? model); + + ProductBindingModel? GetElement(ProductSearchModel model); + + ProductBindingModel? Update(ProductBindingModel model); + + ProductBindingModel? Delete(ProductSearchModel model); + } +} diff --git a/Contracts/StorageContracts/IPurchaseStorage.cs b/Contracts/StorageContracts/IPurchaseStorage.cs new file mode 100644 index 0000000..855831a --- /dev/null +++ b/Contracts/StorageContracts/IPurchaseStorage.cs @@ -0,0 +1,23 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.StorageContracts +{ + public interface IPurchaseStorage + { + PurchaseBindingModel? Insert(PurchaseBindingModel model); + + IEnumerable GetList(PurchaseSearchModel? model); + + PurchaseBindingModel? GetElement(PurchaseSearchModel model); + + PurchaseBindingModel? Update(PurchaseBindingModel model); + + PurchaseBindingModel? Delete(PurchaseSearchModel model); + } +} diff --git a/Contracts/StorageContracts/ISellStorage.cs b/Contracts/StorageContracts/ISellStorage.cs new file mode 100644 index 0000000..ba31175 --- /dev/null +++ b/Contracts/StorageContracts/ISellStorage.cs @@ -0,0 +1,23 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.StorageContracts +{ + public interface ISellStorage + { + SellBindingModel? Insert(SellBindingModel model); + + IEnumerable GetList(SellSearchModel? model); + + SellBindingModel? GetElement(SellSearchModel model); + + SellBindingModel? Update(SellBindingModel model); + + SellBindingModel? Delete(SellSearchModel model); + } +} diff --git a/Contracts/ViewModels/EmployeeProductViewModel.cs b/Contracts/ViewModels/EmployeeProductViewModel.cs new file mode 100644 index 0000000..ecf629a --- /dev/null +++ b/Contracts/ViewModels/EmployeeProductViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class EmployeeProductViewModel + { + public Guid Guid { get; set; } + public string Name { get; set; } = string.Empty; + public double Price { get; set; } + } +} diff --git a/Contracts/ViewModels/PurchaseViewModel.cs b/Contracts/ViewModels/PurchaseViewModel.cs new file mode 100644 index 0000000..f777858 --- /dev/null +++ b/Contracts/ViewModels/PurchaseViewModel.cs @@ -0,0 +1,19 @@ +using DataModels.Enums; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class PurchaseViewModel + { + public Guid Id { get; set; } + public DateTime PurchaseDate { get; set; } + public required IUser User { get; set; } + public required List Products { get; set; } + public PurchaseStatus Status { get; set; } + } +} diff --git a/Contracts/ViewModels/SellViewModel.cs b/Contracts/ViewModels/SellViewModel.cs new file mode 100644 index 0000000..54a40bd --- /dev/null +++ b/Contracts/ViewModels/SellViewModel.cs @@ -0,0 +1,16 @@ +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class SellViewModel + { + public Guid Id { get; set; } + public DateTime SellDate { get; set; } + public ISupplyDoc? SupplyDoc { get; set; } + } +} diff --git a/Contracts/ViewModels/UserProductViewModel.cs b/Contracts/ViewModels/UserProductViewModel.cs new file mode 100644 index 0000000..2da47eb --- /dev/null +++ b/Contracts/ViewModels/UserProductViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class UserProductViewModel + { + public string Name { get; set; } = string.Empty; + public double Price { get; set; } + public double Rate { get; set; } + } +} diff --git a/DataModels/Enums/PurchaseStatus.cs b/DataModels/Enums/PurchaseStatus.cs new file mode 100644 index 0000000..1107f4d --- /dev/null +++ b/DataModels/Enums/PurchaseStatus.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Enums +{ + public enum PurchaseStatus + { + Unknown = -1, + Processing = 0, + Complited = 1 + } +} diff --git a/DataModels/Models/IMediaFile.cs b/DataModels/Models/IMediaFile.cs new file mode 100644 index 0000000..34eec99 --- /dev/null +++ b/DataModels/Models/IMediaFile.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface IMediaFile + { + string Name { get; } + string Location { get; } + } +} diff --git a/DataModels/Models/IProduct.cs b/DataModels/Models/IProduct.cs new file mode 100644 index 0000000..b263796 --- /dev/null +++ b/DataModels/Models/IProduct.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface IProduct : IId + { + string Name { get; } + double Price { get; } + bool IBeingSold { get; } + public double Rate { get; } + int Amount { get; } + } +} diff --git a/DataModels/Models/IPurchase.cs b/DataModels/Models/IPurchase.cs new file mode 100644 index 0000000..e13e222 --- /dev/null +++ b/DataModels/Models/IPurchase.cs @@ -0,0 +1,15 @@ +using DataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface IPurchase : IId + { + DateTime DatePurchase { get; } + PurchaseStatus Status { get; } + } +} diff --git a/DataModels/Models/ISell.cs b/DataModels/Models/ISell.cs new file mode 100644 index 0000000..c10496f --- /dev/null +++ b/DataModels/Models/ISell.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface ISell : IId + { + DateTime DateSell { get; } + } +} diff --git a/DataModels/Models/ISupplyDoc.cs b/DataModels/Models/ISupplyDoc.cs new file mode 100644 index 0000000..cb1f6c4 --- /dev/null +++ b/DataModels/Models/ISupplyDoc.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface ISupplyDoc : IId + { + string Name { get; } + } +} diff --git a/DatabaseImplement/Models/MediaFile.cs b/DatabaseImplement/Models/MediaFile.cs new file mode 100644 index 0000000..8c7e084 --- /dev/null +++ b/DatabaseImplement/Models/MediaFile.cs @@ -0,0 +1,18 @@ +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class MediaFile : IMediaFile + { + [Required] + public string Name { get; set; } + [Required] + public string Location { get; set; } + } +} diff --git a/DatabaseImplement/Models/Product.cs b/DatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..6f6a242 --- /dev/null +++ b/DatabaseImplement/Models/Product.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class Product + { + } +} diff --git a/DatabaseImplement/Models/Purchase.cs b/DatabaseImplement/Models/Purchase.cs new file mode 100644 index 0000000..ecb1efc --- /dev/null +++ b/DatabaseImplement/Models/Purchase.cs @@ -0,0 +1,21 @@ +using DataModels.Enums; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class Purchase : IPurchase + { + public Guid Id { get; set; } + [Required] + public DateTime DatePurchase { get; set; } + [Required] + public PurchaseStatus Status { get; private set; } = PurchaseStatus.Unknown; + + } +} diff --git a/DatabaseImplement/Models/Sell.cs b/DatabaseImplement/Models/Sell.cs new file mode 100644 index 0000000..3376e82 --- /dev/null +++ b/DatabaseImplement/Models/Sell.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + internal class Sell + { + } +} From 34fd2b04c49707cda359c4c8d3a4f27e0071cf18 Mon Sep 17 00:00:00 2001 From: the Date: Mon, 17 Jun 2024 14:04:22 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=98=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=D1=8B=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?,=20Binding=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8,=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/MediaFileBindingModel.cs | 6 ++++-- .../BindingModels/ProductBindingModel.cs | 7 +++++-- .../BindingModels/SupplierBindingModel.cs | 17 ++++++++++++++++ Contracts/BindingModels/SupplyBindingModel.cs | 20 +++++++++++++++++++ .../BindingModels/SupplyDocBindingModel.cs | 17 ++++++++++++++++ DataModels/Enums/SupplyStatus.cs | 15 ++++++++++++++ DataModels/Models/IMediaFile.cs | 3 ++- DataModels/Models/IProduct.cs | 4 +++- DataModels/Models/ISupplier.cs | 15 ++++++++++++++ DataModels/Models/ISupply.cs | 18 +++++++++++++++++ DataModels/Models/ISupplyDoc.cs | 4 +++- DatabaseImplement/Models/MediaFile.cs | 4 ++++ DatabaseImplement/Models/Product.cs | 18 +++++++++++++++-- 13 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 Contracts/BindingModels/SupplierBindingModel.cs create mode 100644 Contracts/BindingModels/SupplyBindingModel.cs create mode 100644 Contracts/BindingModels/SupplyDocBindingModel.cs create mode 100644 DataModels/Enums/SupplyStatus.cs create mode 100644 DataModels/Models/ISupplier.cs create mode 100644 DataModels/Models/ISupply.cs diff --git a/Contracts/BindingModels/MediaFileBindingModel.cs b/Contracts/BindingModels/MediaFileBindingModel.cs index 656354a..dfa6386 100644 --- a/Contracts/BindingModels/MediaFileBindingModel.cs +++ b/Contracts/BindingModels/MediaFileBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,10 +7,11 @@ using System.Threading.Tasks; namespace Contracts.BindingModels { - public class MediaFileBindingModel + public class MediaFileBindingModel : IMediaFile { public Guid Id { get; set; } public Guid ProductId { get; set; } + public string Name { get; set; } = string.Empty; public string Location { get; set; } = string.Empty; } } diff --git a/Contracts/BindingModels/ProductBindingModel.cs b/Contracts/BindingModels/ProductBindingModel.cs index 6be324e..8f44dbd 100644 --- a/Contracts/BindingModels/ProductBindingModel.cs +++ b/Contracts/BindingModels/ProductBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,11 +7,13 @@ using System.Threading.Tasks; namespace Contracts.BindingModels { - public class ProductBindingModel + public class ProductBindingModel : IProduct { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; public double Price { get; set; } public int Amount { get; set; } + public bool IsBeingSold { get; set; } + public double Rate { get; set; } } } diff --git a/Contracts/BindingModels/SupplierBindingModel.cs b/Contracts/BindingModels/SupplierBindingModel.cs new file mode 100644 index 0000000..20c9701 --- /dev/null +++ b/Contracts/BindingModels/SupplierBindingModel.cs @@ -0,0 +1,17 @@ +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class SupplierBindingModel : ISupplier + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public Dictionary AvailibleProducts { get; set; } = new(); + public int Deals { get; set; } = 0; + } +} diff --git a/Contracts/BindingModels/SupplyBindingModel.cs b/Contracts/BindingModels/SupplyBindingModel.cs new file mode 100644 index 0000000..9611abc --- /dev/null +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -0,0 +1,20 @@ +using DataModels.Enums; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + internal class SupplyBindingModel : ISupply + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public DateTime Date { get; set; } + public double Price { get; set; } + public SupplyStatus Status { get; set; } + public Dictionary SupplyProducts { get; set; } = new(); + } +} diff --git a/Contracts/BindingModels/SupplyDocBindingModel.cs b/Contracts/BindingModels/SupplyDocBindingModel.cs new file mode 100644 index 0000000..8767a72 --- /dev/null +++ b/Contracts/BindingModels/SupplyDocBindingModel.cs @@ -0,0 +1,17 @@ +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + public class SupplyDocBindingModel : ISupplyDoc + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public Guid SupplyId { get; set; } + + } +} diff --git a/DataModels/Enums/SupplyStatus.cs b/DataModels/Enums/SupplyStatus.cs new file mode 100644 index 0000000..fad035a --- /dev/null +++ b/DataModels/Enums/SupplyStatus.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Enums +{ + public enum SupplyStatus + { + Pending = -1, + Arriving = 0, + Completed = 1 + } +} diff --git a/DataModels/Models/IMediaFile.cs b/DataModels/Models/IMediaFile.cs index 34eec99..d4db8bd 100644 --- a/DataModels/Models/IMediaFile.cs +++ b/DataModels/Models/IMediaFile.cs @@ -6,9 +6,10 @@ using System.Threading.Tasks; namespace DataModels.Models { - public interface IMediaFile + public interface IMediaFile : IId { string Name { get; } string Location { get; } + Guid ProductId { get; } } } diff --git a/DataModels/Models/IProduct.cs b/DataModels/Models/IProduct.cs index b263796..bfb12c0 100644 --- a/DataModels/Models/IProduct.cs +++ b/DataModels/Models/IProduct.cs @@ -10,8 +10,10 @@ namespace DataModels.Models { string Name { get; } double Price { get; } - bool IBeingSold { get; } + bool IsBeingSold { get; } public double Rate { get; } int Amount { get; } + // будут браться через mediafilestorage так что скорее всего это тут не надо + // List MediaFiles { get; } } } diff --git a/DataModels/Models/ISupplier.cs b/DataModels/Models/ISupplier.cs new file mode 100644 index 0000000..d2ac2e2 --- /dev/null +++ b/DataModels/Models/ISupplier.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface ISupplier : IId + { + string Name { get; } + Dictionary AvailibleProducts { get; } + int Deals { get; } + } +} diff --git a/DataModels/Models/ISupply.cs b/DataModels/Models/ISupply.cs new file mode 100644 index 0000000..a728595 --- /dev/null +++ b/DataModels/Models/ISupply.cs @@ -0,0 +1,18 @@ +using DataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataModels.Models +{ + public interface ISupply: IId + { + string Name { get; } + double Price { get; } + DateTime Date { get; } + SupplyStatus Status { get; } + Dictionary SupplyProducts { get; } + } +} diff --git a/DataModels/Models/ISupplyDoc.cs b/DataModels/Models/ISupplyDoc.cs index cb1f6c4..ca45699 100644 --- a/DataModels/Models/ISupplyDoc.cs +++ b/DataModels/Models/ISupplyDoc.cs @@ -9,5 +9,7 @@ namespace DataModels.Models public interface ISupplyDoc : IId { string Name { get; } - } + + Guid SupplyId { get; } + } } diff --git a/DatabaseImplement/Models/MediaFile.cs b/DatabaseImplement/Models/MediaFile.cs index 8c7e084..71eec3c 100644 --- a/DatabaseImplement/Models/MediaFile.cs +++ b/DatabaseImplement/Models/MediaFile.cs @@ -10,9 +10,13 @@ namespace DatabaseImplement.Models { public class MediaFile : IMediaFile { + [Required] + public Guid Id { get; set; } [Required] public string Name { get; set; } [Required] public string Location { get; set; } + [Required] + public Guid ProductId { get; set; } } } diff --git a/DatabaseImplement/Models/Product.cs b/DatabaseImplement/Models/Product.cs index 6f6a242..a615734 100644 --- a/DatabaseImplement/Models/Product.cs +++ b/DatabaseImplement/Models/Product.cs @@ -1,12 +1,26 @@ -using System; +using DataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DatabaseImplement.Models { - public class Product + public class Product : IProduct { + [Required] + public Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public double Rate { get; set; } + [Required] + public bool IsBeingSold { get; set; } + [Required] + public int Amount { get; set; } } } From 147ed7ad07bf474981dec324ff9c46bc74a3f01d Mon Sep 17 00:00:00 2001 From: the Date: Mon, 17 Jun 2024 14:32:12 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=92=D1=8C=D1=8E=D1=88=D0=BA=D0=B8=20(?= =?UTF-8?q?=D1=81=D0=BB=D0=B5=D0=B3=D0=BA=D0=B0=20=D0=BE=D1=82=D0=BB=D0=B8?= =?UTF-8?q?=D1=87=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20=D0=BE=D1=82=20=D1=81?= =?UTF-8?q?=D1=85=D0=B5=D0=BC=D1=8B,=20=D0=BE=D1=81=D0=BE=D0=B1=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=BE=20=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=BE=D0=BA?= =?UTF-8?q?=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0),=20=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogicContracts/IProductLogic.cs | 17 +++++++++++++++++ .../SearchModels/MediaFileSearchModel.cs | 14 ++++++++++++++ Contracts/SearchModels/SupplierSearchModel.cs | 16 ++++++++++++++++ .../SearchModels/SupplyDocSearchModel.cs | 15 +++++++++++++++ Contracts/SearchModels/SupplySearchModel.cs | 16 ++++++++++++++++ Contracts/ViewModels/MediaFileViewModel.cs | 14 ++++++++++++++ Contracts/ViewModels/ProductViewModel.cs | 17 +++++++++++++++++ Contracts/ViewModels/SupplierViewModel.cs | 15 +++++++++++++++ Contracts/ViewModels/SupplyDocViewModel.cs | 14 ++++++++++++++ Contracts/ViewModels/SupplyViewModel.cs | 19 +++++++++++++++++++ 10 files changed, 157 insertions(+) create mode 100644 Contracts/BusinessLogicContracts/IProductLogic.cs create mode 100644 Contracts/SearchModels/MediaFileSearchModel.cs create mode 100644 Contracts/SearchModels/SupplierSearchModel.cs create mode 100644 Contracts/SearchModels/SupplyDocSearchModel.cs create mode 100644 Contracts/SearchModels/SupplySearchModel.cs create mode 100644 Contracts/ViewModels/MediaFileViewModel.cs create mode 100644 Contracts/ViewModels/ProductViewModel.cs create mode 100644 Contracts/ViewModels/SupplierViewModel.cs create mode 100644 Contracts/ViewModels/SupplyDocViewModel.cs create mode 100644 Contracts/ViewModels/SupplyViewModel.cs diff --git a/Contracts/BusinessLogicContracts/IProductLogic.cs b/Contracts/BusinessLogicContracts/IProductLogic.cs new file mode 100644 index 0000000..b19b8e8 --- /dev/null +++ b/Contracts/BusinessLogicContracts/IProductLogic.cs @@ -0,0 +1,17 @@ +using Contracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface IProductLogic + { + bool Create(ProductBindingModel model); + bool Update(ProductBindingModel model); + bool Delete(ProductBindingModel model); + + } +} diff --git a/Contracts/SearchModels/MediaFileSearchModel.cs b/Contracts/SearchModels/MediaFileSearchModel.cs new file mode 100644 index 0000000..7c81b55 --- /dev/null +++ b/Contracts/SearchModels/MediaFileSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.SearchModels +{ + public class MediaFileSearchModel + { + public Guid? Id { get; set; } + public Guid? ProductId { get; set; } + } +} diff --git a/Contracts/SearchModels/SupplierSearchModel.cs b/Contracts/SearchModels/SupplierSearchModel.cs new file mode 100644 index 0000000..2082afa --- /dev/null +++ b/Contracts/SearchModels/SupplierSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.SearchModels +{ + public class SupplierSearchModel + { + public Guid? Id { get; set; } + public string? Name { get; set; } + public List? AvailibleProducts { get; set; } + public int? Deals { get; set; } + } +} diff --git a/Contracts/SearchModels/SupplyDocSearchModel.cs b/Contracts/SearchModels/SupplyDocSearchModel.cs new file mode 100644 index 0000000..017227a --- /dev/null +++ b/Contracts/SearchModels/SupplyDocSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.SearchModels +{ + public class SupplyDocSearchModel + { + public Guid? Id { get; set; } + public string? Name { get; set; } + public Guid? SupplyId { get; set; } + } +} diff --git a/Contracts/SearchModels/SupplySearchModel.cs b/Contracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..9f50d53 --- /dev/null +++ b/Contracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.SearchModels +{ + public class SupplySearchModel + { + public Guid? Id { get; set; } + public List? Products { get; set; } + public DateTime? DateStart { get; set; } + public DateTime? DateEnd { get; set; } + } +} diff --git a/Contracts/ViewModels/MediaFileViewModel.cs b/Contracts/ViewModels/MediaFileViewModel.cs new file mode 100644 index 0000000..d2ca0f6 --- /dev/null +++ b/Contracts/ViewModels/MediaFileViewModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class MediaFileViewModel + { + public Guid Id { get; set; } + public string Location { get; set; } = string.Empty; + } +} diff --git a/Contracts/ViewModels/ProductViewModel.cs b/Contracts/ViewModels/ProductViewModel.cs new file mode 100644 index 0000000..4a5d4fd --- /dev/null +++ b/Contracts/ViewModels/ProductViewModel.cs @@ -0,0 +1,17 @@ +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class ProductViewModel + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public int Price { get; set; } + public List MediaFiles { get; set; } = new(); + } +} diff --git a/Contracts/ViewModels/SupplierViewModel.cs b/Contracts/ViewModels/SupplierViewModel.cs new file mode 100644 index 0000000..337dc1d --- /dev/null +++ b/Contracts/ViewModels/SupplierViewModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class SupplierViewModel + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + public Dictionary AvailibleProducts = new(); + } +} diff --git a/Contracts/ViewModels/SupplyDocViewModel.cs b/Contracts/ViewModels/SupplyDocViewModel.cs new file mode 100644 index 0000000..7f8a084 --- /dev/null +++ b/Contracts/ViewModels/SupplyDocViewModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class SupplyDocViewModel + { + public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; + } +} diff --git a/Contracts/ViewModels/SupplyViewModel.cs b/Contracts/ViewModels/SupplyViewModel.cs new file mode 100644 index 0000000..9dca657 --- /dev/null +++ b/Contracts/ViewModels/SupplyViewModel.cs @@ -0,0 +1,19 @@ +using DataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class SupplyViewModel + { + public Guid Id { get; set; } + public string SupplierName { get; set; } = string.Empty; + public double Price { get; set; } + public Dictionary Products { get; set; } = new(); + public DateTime Date { get; set; } + public SupplyStatus Status { get; set; } + } +} From 9ffb97cbd098cc909c9997ef94cd94fef353724b Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Thu, 20 Jun 2024 22:32:22 +0400 Subject: [PATCH 4/8] I hate this shit --- Contracts/BindingModels/SellBindingModel.cs | 1 + Contracts/ViewModels/MediaFileViewModel.cs | 19 +++++++- Contracts/ViewModels/ProductViewModel.cs | 3 ++ Contracts/ViewModels/PurchaseViewModel.cs | 2 +- Contracts/ViewModels/SellViewModel.cs | 2 +- DatabaseImplement/Database.cs | 4 ++ DatabaseImplement/Models/MediaFile.cs | 54 +++++++++++++++++++-- DatabaseImplement/Models/Product.cs | 48 +++++++++++++++++- DatabaseImplement/Models/Purchase.cs | 30 +++++++++++- DatabaseImplement/Models/Sell.cs | 36 +++++++++++++- 10 files changed, 188 insertions(+), 11 deletions(-) diff --git a/Contracts/BindingModels/SellBindingModel.cs b/Contracts/BindingModels/SellBindingModel.cs index 7f2c663..e0c32ed 100644 --- a/Contracts/BindingModels/SellBindingModel.cs +++ b/Contracts/BindingModels/SellBindingModel.cs @@ -8,6 +8,7 @@ namespace Contracts.BindingModels { public class SellBindingModel { + public Guid Id { get; set; } public DateTime DateSell { get; set; } } } diff --git a/Contracts/ViewModels/MediaFileViewModel.cs b/Contracts/ViewModels/MediaFileViewModel.cs index d2ca0f6..6acc484 100644 --- a/Contracts/ViewModels/MediaFileViewModel.cs +++ b/Contracts/ViewModels/MediaFileViewModel.cs @@ -1,4 +1,6 @@ -using System; +using Contracts.BindingModels; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,5 +12,18 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public string Location { get; set; } = string.Empty; - } + public string Name { get; set; } = string.Empty; + public Guid ProductId { get; set; } + public MediaFileBindingModel GetBindingModel() + { + return new MediaFileBindingModel + { + Id = Id, + Name = Name, + Location = Location, + ProductId = ProductId + }; + } + + } } diff --git a/Contracts/ViewModels/ProductViewModel.cs b/Contracts/ViewModels/ProductViewModel.cs index 4a5d4fd..1424a09 100644 --- a/Contracts/ViewModels/ProductViewModel.cs +++ b/Contracts/ViewModels/ProductViewModel.cs @@ -12,6 +12,9 @@ namespace Contracts.ViewModels public Guid Id { get; set; } public string Name { get; set; } = string.Empty; public int Price { get; set; } + public double Rating { get; set; } + public bool IsBeingSold { get; set; } + public int Amount { get; set; } public List MediaFiles { get; set; } = new(); } } diff --git a/Contracts/ViewModels/PurchaseViewModel.cs b/Contracts/ViewModels/PurchaseViewModel.cs index f777858..b14933c 100644 --- a/Contracts/ViewModels/PurchaseViewModel.cs +++ b/Contracts/ViewModels/PurchaseViewModel.cs @@ -11,7 +11,7 @@ namespace Contracts.ViewModels public class PurchaseViewModel { public Guid Id { get; set; } - public DateTime PurchaseDate { 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/ViewModels/SellViewModel.cs b/Contracts/ViewModels/SellViewModel.cs index 54a40bd..285d63f 100644 --- a/Contracts/ViewModels/SellViewModel.cs +++ b/Contracts/ViewModels/SellViewModel.cs @@ -10,7 +10,7 @@ namespace Contracts.ViewModels public class SellViewModel { public Guid Id { get; set; } - public DateTime SellDate { get; set; } + public DateTime DateSell { get; set; } public ISupplyDoc? SupplyDoc { get; set; } } } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index 7bb94c7..69e2127 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -21,5 +21,9 @@ namespace DatabaseImplement public virtual DbSet Roles { get; set; } = null!; public virtual DbSet Users { get; set; } = null!; + public virtual DbSet Sells { get; set; } = null!; + public virtual DbSet Purchases { get; set; } = null!; + public virtual DbSet Products { get; set; } = null!; + public virtual DbSet MediaFiles { get; set; } = null!; } } \ No newline at end of file diff --git a/DatabaseImplement/Models/MediaFile.cs b/DatabaseImplement/Models/MediaFile.cs index 71eec3c..bc3c9ec 100644 --- a/DatabaseImplement/Models/MediaFile.cs +++ b/DatabaseImplement/Models/MediaFile.cs @@ -1,4 +1,6 @@ -using DataModels.Models; +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -13,10 +15,56 @@ namespace DatabaseImplement.Models [Required] public Guid Id { get; set; } [Required] - public string Name { get; set; } + public string Name { get; set; } = string.Empty; [Required] - public string Location { get; set; } + public string Location { get; set; } = string.Empty; [Required] public Guid ProductId { get; set; } + + public MediaFileBindingModel GetBindingModel() + { + return new MediaFileBindingModel + { + Id = Id, + Name = Name, + Location = Location, + ProductId = ProductId + }; + } + + public static MediaFile ToMediaFileFromView(MediaFileViewModel model, MediaFile mediaFile) + { + return new MediaFile + { + Id = model.Id, + Name = model.Name, + Location = model.Location, + ProductId = model.ProductId + }; + } + + public static MediaFile ToMediaFileFromBinding(MediaFileBindingModel model, MediaFile mediaFile) + { + return new MediaFile + { + Id = model.Id, + Name = model.Name, + Location = model.Location, + ProductId = model.ProductId + }; + } + + public void Update(MediaFileBindingModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + // Обновление свойств на основе модели привязки + Name = model.Name; + Location = model.Location; + ProductId = model.ProductId; + } } } diff --git a/DatabaseImplement/Models/Product.cs b/DatabaseImplement/Models/Product.cs index a615734..bc14d3e 100644 --- a/DatabaseImplement/Models/Product.cs +++ b/DatabaseImplement/Models/Product.cs @@ -1,4 +1,6 @@ -using DataModels.Models; +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -22,5 +24,49 @@ namespace DatabaseImplement.Models public bool IsBeingSold { get; set; } [Required] public int Amount { get; set; } + + public ProductBindingModel GetBindingModel() => new() + { + Id = Id, + Name = Name, + Price = Price, + Rate = Rate, + IsBeingSold = IsBeingSold, + Amount = Amount + }; + + public static Product ToProductFromView(ProductViewModel model, Product product) => new() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Rate = model.Rating, + IsBeingSold = model.IsBeingSold, + Amount = model.Amount + }; + + public static Product ToProductFromBinding(ProductBindingModel model, Product product) => new() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Rate = model.Rate, + IsBeingSold = model.IsBeingSold, + Amount = model.Amount + }; + + public void Update(ProductBindingModel model, Product product) + { + if (model is null) + { + throw new ArgumentNullException("Update product: binding model is null"); + } + + Name = model.Name; + Price = model.Price; + Rate = model.Rate; + IsBeingSold = model.IsBeingSold; + Amount = model.Amount; + } } } diff --git a/DatabaseImplement/Models/Purchase.cs b/DatabaseImplement/Models/Purchase.cs index ecb1efc..5430b34 100644 --- a/DatabaseImplement/Models/Purchase.cs +++ b/DatabaseImplement/Models/Purchase.cs @@ -1,4 +1,6 @@ -using DataModels.Enums; +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Enums; using DataModels.Models; using System; using System.Collections.Generic; @@ -16,6 +18,32 @@ namespace DatabaseImplement.Models public DateTime DatePurchase { get; set; } [Required] public PurchaseStatus Status { get; private set; } = PurchaseStatus.Unknown; + public PurchaseBindingModel GetBindingModel() => new() + { + Id = Id, + DatePurchase = DatePurchase + }; + public static Purchase ToPurchaseFromView(PurchaseViewModel model, Purchase purchase) => new() + { + Id = model.Id, + DatePurchase = model.DatePurchase + }; + + public static Purchase ToPurchaseFromBinding(PurchaseBindingModel model, Purchase purchase) => new() + { + Id = model.Id, + DatePurchase = model.DatePurchase, + }; + + public void Update(PurchaseBindingModel model, Purchase purchase) + { + if (model is null) + { + throw new ArgumentNullException("Update purchase: binding model is null"); + } + + DatePurchase = purchase.DatePurchase; + } } } diff --git a/DatabaseImplement/Models/Sell.cs b/DatabaseImplement/Models/Sell.cs index 3376e82..03a7f26 100644 --- a/DatabaseImplement/Models/Sell.cs +++ b/DatabaseImplement/Models/Sell.cs @@ -1,4 +1,7 @@ -using System; +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,36 @@ using System.Threading.Tasks; namespace DatabaseImplement.Models { - internal class Sell + public class Sell : ISell { + public Guid Id { get; set; } + public DateTime DateSell { get; set; } + + public SellBindingModel GetBindingModel() => new() + { + Id = Id, + DateSell = DateSell + }; + public static Sell ToSellFromView(SellViewModel model, Sell sell) => new() + { + Id = model.Id, + DateSell = model.DateSell + }; + + public static Sell ToSellFromBinding(SellBindingModel model, Sell sell) => new() + { + Id = model.Id, + DateSell = model.DateSell, + }; + + public void Update(SellBindingModel model, Sell sell) + { + if (model is null) + { + throw new ArgumentNullException("Update user: binding model is null"); + } + + DateSell = sell.DateSell; + } } } From 2cc2ac4280f15b0f05ce2f53cf7bbad4a19c19e6 Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Jun 2024 11:01:06 +0400 Subject: [PATCH 5/8] interface galore --- Contracts/BindingModels/SupplyBindingModel.cs | 2 +- .../BusinessLogicContracts/IProductLogic.cs | 5 ++++- .../BusinessLogicContracts/ISupplierLogic.cs | 20 ++++++++++++++++++ .../BusinessLogicContracts/ISupplyDocLogic.cs | 20 ++++++++++++++++++ .../BusinessLogicContracts/ISupplyLogic.cs | 20 ++++++++++++++++++ Contracts/StorageContracts/IProductStorage.cs | 18 +++++++--------- .../StorageContracts/ISupplierStorage.cs | 21 +++++++++++++++++++ .../StorageContracts/ISupplyDocStorage.cs | 21 +++++++++++++++++++ Contracts/StorageContracts/ISupplyStorage.cs | 21 +++++++++++++++++++ 9 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 Contracts/BusinessLogicContracts/ISupplierLogic.cs create mode 100644 Contracts/BusinessLogicContracts/ISupplyDocLogic.cs create mode 100644 Contracts/BusinessLogicContracts/ISupplyLogic.cs create mode 100644 Contracts/StorageContracts/ISupplierStorage.cs create mode 100644 Contracts/StorageContracts/ISupplyDocStorage.cs create mode 100644 Contracts/StorageContracts/ISupplyStorage.cs diff --git a/Contracts/BindingModels/SupplyBindingModel.cs b/Contracts/BindingModels/SupplyBindingModel.cs index 9611abc..9c2e2f1 100644 --- a/Contracts/BindingModels/SupplyBindingModel.cs +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Contracts.BindingModels { - internal class SupplyBindingModel : ISupply + public class SupplyBindingModel : ISupply { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; diff --git a/Contracts/BusinessLogicContracts/IProductLogic.cs b/Contracts/BusinessLogicContracts/IProductLogic.cs index b19b8e8..4711e97 100644 --- a/Contracts/BusinessLogicContracts/IProductLogic.cs +++ b/Contracts/BusinessLogicContracts/IProductLogic.cs @@ -1,4 +1,6 @@ using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -12,6 +14,7 @@ namespace Contracts.BusinessLogicContracts bool Create(ProductBindingModel model); bool Update(ProductBindingModel model); bool Delete(ProductBindingModel model); - + List? ReadList(ProductSearchModel? model); + ProductViewModel? ReadElement(ProductSearchModel model); } } diff --git a/Contracts/BusinessLogicContracts/ISupplierLogic.cs b/Contracts/BusinessLogicContracts/ISupplierLogic.cs new file mode 100644 index 0000000..24ed51c --- /dev/null +++ b/Contracts/BusinessLogicContracts/ISupplierLogic.cs @@ -0,0 +1,20 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface ISupplierLogic + { + List? ReadList(SupplierSearchModel? model); + SupplierViewModel? ReadElement(SupplierSearchModel model); + bool Create(SupplierBindingModel model); + bool Update(SupplierBindingModel model); + bool Delete(SupplierBindingModel model); + } +} diff --git a/Contracts/BusinessLogicContracts/ISupplyDocLogic.cs b/Contracts/BusinessLogicContracts/ISupplyDocLogic.cs new file mode 100644 index 0000000..3d441c1 --- /dev/null +++ b/Contracts/BusinessLogicContracts/ISupplyDocLogic.cs @@ -0,0 +1,20 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface ISupplyDocLogic + { + List? ReadList(SupplyDocSearchModel? model); + SupplyDocViewModel? ReadElement(SupplyDocSearchModel model); + bool Create(SupplyDocBindingModel model); + bool Update(SupplyDocBindingModel model); + bool Delete(SupplyDocBindingModel model); + } +} diff --git a/Contracts/BusinessLogicContracts/ISupplyLogic.cs b/Contracts/BusinessLogicContracts/ISupplyLogic.cs new file mode 100644 index 0000000..d21ff5d --- /dev/null +++ b/Contracts/BusinessLogicContracts/ISupplyLogic.cs @@ -0,0 +1,20 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BusinessLogicContracts +{ + public interface ISupplyLogic + { + List? ReadList(SupplySearchModel? model); + SupplyViewModel? ReadElement(SupplySearchModel model); + bool Create(SupplyBindingModel model); + bool Update(SupplyBindingModel model); + bool Delete(SupplyBindingModel model); + } +} diff --git a/Contracts/StorageContracts/IProductStorage.cs b/Contracts/StorageContracts/IProductStorage.cs index 49ce4e3..7d8e2bd 100644 --- a/Contracts/StorageContracts/IProductStorage.cs +++ b/Contracts/StorageContracts/IProductStorage.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,11 @@ namespace Contracts.StorageContracts { public interface IProductStorage { - ProductBindingModel? Insert(ProductBindingModel model); - - IEnumerable GetList(ProductSearchModel? model); - - ProductBindingModel? GetElement(ProductSearchModel model); - - ProductBindingModel? Update(ProductBindingModel model); - - ProductBindingModel? Delete(ProductSearchModel model); - } + List GetFullList(); + List GetFilteredList(ProductSearchModel model); + ProductViewModel? GetElement(ProductSearchModel model); + ProductViewModel? Insert(ProductBindingModel model); + ProductViewModel? Update(ProductBindingModel model); + ProductViewModel? Delete(ProductBindingModel model); + } } diff --git a/Contracts/StorageContracts/ISupplierStorage.cs b/Contracts/StorageContracts/ISupplierStorage.cs new file mode 100644 index 0000000..dc2f1fe --- /dev/null +++ b/Contracts/StorageContracts/ISupplierStorage.cs @@ -0,0 +1,21 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.StorageContracts +{ + public interface ISupplierStorage + { + List GetFullList(); + List GetFilteredList(SupplierSearchModel model); + SupplierViewModel? GetElement(SupplierSearchModel model); + SupplierViewModel? Insert(SupplierBindingModel model); + SupplierViewModel? Update(SupplierBindingModel model); + SupplierViewModel? Delete(SupplierBindingModel model); + } +} diff --git a/Contracts/StorageContracts/ISupplyDocStorage.cs b/Contracts/StorageContracts/ISupplyDocStorage.cs new file mode 100644 index 0000000..d10ab0e --- /dev/null +++ b/Contracts/StorageContracts/ISupplyDocStorage.cs @@ -0,0 +1,21 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.StorageContracts +{ + public interface ISupplyDocStorage + { + List GetFullList(); + List GetFilteredList(SupplyDocSearchModel model); + SupplyDocViewModel? GetElement(SupplyDocSearchModel model); + SupplyDocViewModel? Insert(SupplyDocBindingModel model); + SupplyDocViewModel? Update(SupplyDocBindingModel model); + SupplyDocViewModel? Delete(SupplyDocBindingModel model); + } +} diff --git a/Contracts/StorageContracts/ISupplyStorage.cs b/Contracts/StorageContracts/ISupplyStorage.cs new file mode 100644 index 0000000..2f81c46 --- /dev/null +++ b/Contracts/StorageContracts/ISupplyStorage.cs @@ -0,0 +1,21 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.StorageContracts +{ + public interface ISupplyStorage + { + List GetFullList(); + List GetFilteredList(SupplySearchModel model); + SupplyViewModel? GetElement(SupplySearchModel model); + SupplyViewModel? Insert(SupplyBindingModel model); + SupplyViewModel? Update(SupplyBindingModel model); + SupplyViewModel? Delete(SupplyBindingModel model); + } +} From 43aef44d98db69acc935c691915078a26a995cd8 Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Jun 2024 12:18:43 +0400 Subject: [PATCH 6/8] supply model --- BusinessLogic/BusinessLogic/ProductLogic.cs | 47 ++++++++ Contracts/BindingModels/SupplyBindingModel.cs | 2 +- Contracts/ViewModels/SupplyViewModel.cs | 4 +- DataModels/Models/ISupply.cs | 2 +- DatabaseImplement/Database.cs | 2 + DatabaseImplement/Models/Supply.cs | 108 ++++++++++++++++++ DatabaseImplement/Models/SupplyProduct.cs | 24 ++++ 7 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 BusinessLogic/BusinessLogic/ProductLogic.cs create mode 100644 DatabaseImplement/Models/Supply.cs create mode 100644 DatabaseImplement/Models/SupplyProduct.cs diff --git a/BusinessLogic/BusinessLogic/ProductLogic.cs b/BusinessLogic/BusinessLogic/ProductLogic.cs new file mode 100644 index 0000000..a177c19 --- /dev/null +++ b/BusinessLogic/BusinessLogic/ProductLogic.cs @@ -0,0 +1,47 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +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 ProductLogic : IProductLogic + { + private readonly IProductStorage _productStorage; + public ProductLogic(IProductStorage productStorage) + { + _productStorage = productStorage; + } + public bool Create(ProductBindingModel model) + { + throw new NotImplementedException(); + } + + public bool Delete(ProductBindingModel model) + { + throw new NotImplementedException(); + } + + public ProductViewModel? ReadElement(ProductSearchModel model) + { + throw new NotImplementedException(); + } + + public List? ReadList(ProductSearchModel? model) + { + throw new NotImplementedException(); + } + + public bool Update(ProductBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/Contracts/BindingModels/SupplyBindingModel.cs b/Contracts/BindingModels/SupplyBindingModel.cs index 9c2e2f1..32f3458 100644 --- a/Contracts/BindingModels/SupplyBindingModel.cs +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -15,6 +15,6 @@ namespace Contracts.BindingModels public DateTime Date { get; set; } public double Price { get; set; } public SupplyStatus Status { get; set; } - public Dictionary SupplyProducts { get; set; } = new(); + public Dictionary SupplyProducts { get; set; } = new(); } } diff --git a/Contracts/ViewModels/SupplyViewModel.cs b/Contracts/ViewModels/SupplyViewModel.cs index 9dca657..fca9ae7 100644 --- a/Contracts/ViewModels/SupplyViewModel.cs +++ b/Contracts/ViewModels/SupplyViewModel.cs @@ -1,4 +1,5 @@ using DataModels.Enums; +using DataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,9 +11,10 @@ namespace Contracts.ViewModels public class SupplyViewModel { public Guid Id { get; set; } + public string Name { get; set; } = string.Empty; public string SupplierName { get; set; } = string.Empty; public double Price { get; set; } - public Dictionary Products { get; set; } = new(); + public Dictionary Products { get; set; } = new(); public DateTime Date { get; set; } public SupplyStatus Status { get; set; } } diff --git a/DataModels/Models/ISupply.cs b/DataModels/Models/ISupply.cs index a728595..0fddfba 100644 --- a/DataModels/Models/ISupply.cs +++ b/DataModels/Models/ISupply.cs @@ -13,6 +13,6 @@ namespace DataModels.Models double Price { get; } DateTime Date { get; } SupplyStatus Status { get; } - Dictionary SupplyProducts { get; } + Dictionary SupplyProducts { get; } } } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index 69e2127..b8320eb 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -24,6 +24,8 @@ namespace DatabaseImplement public virtual DbSet Sells { get; set; } = null!; public virtual DbSet Purchases { get; set; } = null!; public virtual DbSet Products { get; set; } = null!; + public virtual DbSet Supplies { get; set; } = null!; + public virtual DbSet SupplyProducts { get; set; } = null!; public virtual DbSet MediaFiles { get; set; } = null!; } } \ No newline at end of file diff --git a/DatabaseImplement/Models/Supply.cs b/DatabaseImplement/Models/Supply.cs new file mode 100644 index 0000000..b30e50a --- /dev/null +++ b/DatabaseImplement/Models/Supply.cs @@ -0,0 +1,108 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Enums; +using DataModels.Models; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class Supply : ISupply + { + [Required] + public Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + [Required] + public DateTime Date { get; set; } + [Required] + public SupplyStatus Status { get; set; } = SupplyStatus.Pending; + private Dictionary? _supplyProducts = null; + [NotMapped] + public Dictionary SupplyProducts + { + get + { + if (_supplyProducts == null) + { + _supplyProducts = Products + .ToDictionary(recPC => recPC.Id, recPC => + (recPC.Product as IProduct, recPC.Count)); + } + return _supplyProducts; + } + } + [ForeignKey("SupplyId")] + public virtual List Products { get; set; } = new(); + + public static Supply Create(Database context, SupplyBindingModel model) + { + return new Supply() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Products = model.SupplyProducts.Select(x => new + SupplyProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SupplyBindingModel model) + { + Name = model.Name; + Price = model.Price; + } + public SupplyViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Price = Price, + Products = SupplyProducts, + //supplierName сделать + Date = Date, + Status = Status + }; + public void UpdateComponents(Database context, SupplyBindingModel model) + { + var supplyProducts = context.SupplyProducts.Where(rec => + rec.Id == model.Id).ToList(); + if (supplyProducts != null && supplyProducts.Count > 0) + { // удалили те, которых нет в модели + context.SupplyProducts.RemoveRange(supplyProducts.Where(rec + => !model.SupplyProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateProduct in supplyProducts) + { + updateProduct.Count = model.SupplyProducts[updateProduct.ProductId].Item2; + model.SupplyProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + var supply = context.Supplies.First(x => x.Id == Id); + foreach (var pc in model.SupplyProducts) + { + context.SupplyProducts.Add(new SupplyProduct + { + Supply = supply, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _supplyProducts = null; + } + } +} diff --git a/DatabaseImplement/Models/SupplyProduct.cs b/DatabaseImplement/Models/SupplyProduct.cs new file mode 100644 index 0000000..f55ed47 --- /dev/null +++ b/DatabaseImplement/Models/SupplyProduct.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class SupplyProduct + { + public Guid Id { get; set; } + [Required] + public Guid SupplyId { get; set; } + [Required] + public Guid ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Supply Supply { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} From 5f76bf27509a75bff5c2618e64511ccc7d9662fc Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Jun 2024 15:10:46 +0400 Subject: [PATCH 7/8] =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D0=BA?= =?UTF-8?q?=D1=80=D1=83=D0=B4=D0=B0=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/SupplierBindingModel.cs | 2 +- Contracts/BindingModels/SupplyBindingModel.cs | 1 + Contracts/SearchModels/SupplierSearchModel.cs | 2 +- Contracts/SearchModels/SupplySearchModel.cs | 5 +- Contracts/ViewModels/SupplierViewModel.cs | 5 +- Contracts/ViewModels/SupplyViewModel.cs | 2 + DataModels/Models/ISupplier.cs | 2 +- DataModels/Models/ISupply.cs | 1 + DatabaseImplement/Database.cs | 5 +- .../Implements/SupplierStorage.cs | 121 ++++++++++++++ DatabaseImplement/Implements/SupplyStorage.cs | 147 ++++++++++++++++++ DatabaseImplement/Models/Supplier.cs | 109 +++++++++++++ DatabaseImplement/Models/SupplierProduct.cs | 22 +++ DatabaseImplement/Models/Supply.cs | 34 ++-- 14 files changed, 438 insertions(+), 20 deletions(-) create mode 100644 DatabaseImplement/Implements/SupplierStorage.cs create mode 100644 DatabaseImplement/Implements/SupplyStorage.cs create mode 100644 DatabaseImplement/Models/Supplier.cs create mode 100644 DatabaseImplement/Models/SupplierProduct.cs diff --git a/Contracts/BindingModels/SupplierBindingModel.cs b/Contracts/BindingModels/SupplierBindingModel.cs index 20c9701..49f0469 100644 --- a/Contracts/BindingModels/SupplierBindingModel.cs +++ b/Contracts/BindingModels/SupplierBindingModel.cs @@ -11,7 +11,7 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; - public Dictionary AvailibleProducts { get; set; } = new(); + public Dictionary AvailibleProducts { get; set; } = new(); public int Deals { get; set; } = 0; } } diff --git a/Contracts/BindingModels/SupplyBindingModel.cs b/Contracts/BindingModels/SupplyBindingModel.cs index 32f3458..44ba57c 100644 --- a/Contracts/BindingModels/SupplyBindingModel.cs +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -12,6 +12,7 @@ namespace Contracts.BindingModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; + public Guid SupplierId { get; set; } public DateTime Date { get; set; } public double Price { get; set; } public SupplyStatus Status { get; set; } diff --git a/Contracts/SearchModels/SupplierSearchModel.cs b/Contracts/SearchModels/SupplierSearchModel.cs index 2082afa..2a889e9 100644 --- a/Contracts/SearchModels/SupplierSearchModel.cs +++ b/Contracts/SearchModels/SupplierSearchModel.cs @@ -10,7 +10,7 @@ namespace Contracts.SearchModels { public Guid? Id { get; set; } public string? Name { get; set; } - public List? AvailibleProducts { get; set; } + //public List? AvailibleProducts { get; set; } public int? Deals { get; set; } } } diff --git a/Contracts/SearchModels/SupplySearchModel.cs b/Contracts/SearchModels/SupplySearchModel.cs index 9f50d53..6fd24c8 100644 --- a/Contracts/SearchModels/SupplySearchModel.cs +++ b/Contracts/SearchModels/SupplySearchModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Enums; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,7 +10,7 @@ namespace Contracts.SearchModels public class SupplySearchModel { public Guid? Id { get; set; } - public List? Products { get; set; } + public SupplyStatus? Status { get; set; } public DateTime? DateStart { get; set; } public DateTime? DateEnd { get; set; } } diff --git a/Contracts/ViewModels/SupplierViewModel.cs b/Contracts/ViewModels/SupplierViewModel.cs index 337dc1d..09b2af2 100644 --- a/Contracts/ViewModels/SupplierViewModel.cs +++ b/Contracts/ViewModels/SupplierViewModel.cs @@ -1,4 +1,5 @@ -using System; +using DataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,6 +11,6 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; - public Dictionary AvailibleProducts = new(); + public Dictionary AvailibleProducts = new(); } } diff --git a/Contracts/ViewModels/SupplyViewModel.cs b/Contracts/ViewModels/SupplyViewModel.cs index fca9ae7..08f7046 100644 --- a/Contracts/ViewModels/SupplyViewModel.cs +++ b/Contracts/ViewModels/SupplyViewModel.cs @@ -2,6 +2,7 @@ using DataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -12,6 +13,7 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; + public Guid SupplierId { get; set; } public string SupplierName { get; set; } = string.Empty; public double Price { get; set; } public Dictionary Products { get; set; } = new(); diff --git a/DataModels/Models/ISupplier.cs b/DataModels/Models/ISupplier.cs index d2ac2e2..4289d90 100644 --- a/DataModels/Models/ISupplier.cs +++ b/DataModels/Models/ISupplier.cs @@ -9,7 +9,7 @@ namespace DataModels.Models public interface ISupplier : IId { string Name { get; } - Dictionary AvailibleProducts { get; } + Dictionary AvailibleProducts { get; } int Deals { get; } } } diff --git a/DataModels/Models/ISupply.cs b/DataModels/Models/ISupply.cs index 0fddfba..fff5ab5 100644 --- a/DataModels/Models/ISupply.cs +++ b/DataModels/Models/ISupply.cs @@ -11,6 +11,7 @@ namespace DataModels.Models { string Name { get; } double Price { get; } + Guid SupplierId { get; } DateTime Date { get; } SupplyStatus Status { get; } Dictionary SupplyProducts { get; } diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index b8320eb..f17e76a 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -26,6 +26,9 @@ namespace DatabaseImplement public virtual DbSet Products { get; set; } = null!; public virtual DbSet Supplies { get; set; } = null!; public virtual DbSet SupplyProducts { get; set; } = null!; - public virtual DbSet MediaFiles { 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 diff --git a/DatabaseImplement/Implements/SupplierStorage.cs b/DatabaseImplement/Implements/SupplierStorage.cs new file mode 100644 index 0000000..c44c0bf --- /dev/null +++ b/DatabaseImplement/Implements/SupplierStorage.cs @@ -0,0 +1,121 @@ +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; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Implements +{ + public class SupplierStorage : ISupplierStorage + { + public SupplierViewModel? Delete(SupplierBindingModel model) + { + using var context = new Database(); + var element = context.Suppliers + .Include(x => x.Products) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Suppliers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public SupplierViewModel? GetElement(SupplierSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.Suppliers + .Include(x => x.Name) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(SupplierSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Deals.HasValue) + { + return new(); + } + using var context = new Database(); + if (!string.IsNullOrEmpty(model.Name)) + { + return context.Suppliers + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => model.Name == x.Name) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Suppliers + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => model.Deals <= x.Deals) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Suppliers + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SupplierViewModel? Insert(SupplierBindingModel model) + { + using var context = new Database(); + var newProduct = Supplier.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Suppliers.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public SupplierViewModel? Update(SupplierBindingModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Suppliers.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(context, model); + context.SaveChanges(); + product.UpdateProducts(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/DatabaseImplement/Implements/SupplyStorage.cs b/DatabaseImplement/Implements/SupplyStorage.cs new file mode 100644 index 0000000..64d3c8f --- /dev/null +++ b/DatabaseImplement/Implements/SupplyStorage.cs @@ -0,0 +1,147 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Implements +{ + public class SupplyStorage : ISupplyStorage + { + public SupplyViewModel? Delete(SupplyBindingModel model) + { + using var context = new Database(); + var element = context.Supplies + .Include(x => x.Products) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Supplies.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public SupplyViewModel? GetElement(SupplySearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(SupplySearchModel model) + { + if (!model.DateStart.HasValue && !model.DateEnd.HasValue && model.Status == null) + { + return new(); + } + using var context = new Database(); + if (model.DateStart.HasValue && model.DateEnd.HasValue) + { + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.Id == model.Id || model.DateStart <= x.Date && x.Date <= model.DateEnd) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.DateEnd.HasValue) + { + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.Id == model.Id || x.Date <= model.DateEnd) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.DateStart.HasValue) + { + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => x.Id == model.Id || model.DateStart <= x.Date) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .Where(x => model.Status.Equals(x.Status)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Supplies + .Include(x => x.Supplier) + .Include(x => x.Products) + .ThenInclude(x => x.Product) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SupplyViewModel? Insert(SupplyBindingModel model) + { + using var context = new Database(); + var newProduct = Supply.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Supplies.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public SupplyViewModel? Update(SupplyBindingModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Supplies.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateProducts(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/DatabaseImplement/Models/Supplier.cs b/DatabaseImplement/Models/Supplier.cs new file mode 100644 index 0000000..2901ee2 --- /dev/null +++ b/DatabaseImplement/Models/Supplier.cs @@ -0,0 +1,109 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace DatabaseImplement.Models +{ + public class Supplier : ISupplier + { + public Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public int Deals { get; set; } + private Dictionary? _availibleProducts = null; + [NotMapped] + public Dictionary AvailibleProducts + { + get + { + if (_availibleProducts == null) + { + _availibleProducts = Products + .ToDictionary(recPC => recPC.Id, recPC => + (recPC.Product as IProduct, recPC.Count)); + } + return _availibleProducts; + } + } + [ForeignKey("SupplierId")] + public virtual List Products { get; set; } = new(); + + public static Supplier Create(Database context, SupplierBindingModel model) + { + return new Supplier() + { + Id = model.Id, + Name = model.Name, + Products = model.AvailibleProducts.Select(x => new + SupplierProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(Database context, SupplierBindingModel model) + { + Name = model.Name; + Products = model.AvailibleProducts.Select(x => new + SupplierProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(); + } + public SupplierViewModel GetViewModel + { + get + { + var context = new Database(); + return new() + { + Id = Id, + Name = Name, + AvailibleProducts = AvailibleProducts + }; + } + } + public void UpdateProducts(Database context, SupplierBindingModel model) + { + var supplierProducts = context.SupplierProducts.Where(rec => + rec.Id == model.Id).ToList(); + if (supplierProducts != null && supplierProducts.Count > 0) + { // удалили те, которых нет в модели + context.SupplierProducts.RemoveRange(supplierProducts.Where(rec + => !model.AvailibleProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateProduct in supplierProducts) + { + updateProduct.Count = model.AvailibleProducts[updateProduct.ProductId].Item2; + model.AvailibleProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + var supplier = context.Suppliers.First(x => x.Id == Id); + foreach (var pc in model.AvailibleProducts) + { + context.SupplierProducts.Add(new SupplierProduct + { + Supplier = supplier, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _availibleProducts = null; + } + } +} diff --git a/DatabaseImplement/Models/SupplierProduct.cs b/DatabaseImplement/Models/SupplierProduct.cs new file mode 100644 index 0000000..95ce3af --- /dev/null +++ b/DatabaseImplement/Models/SupplierProduct.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + public class SupplierProduct + { + public Guid Id { get; set; } + [Required] + public Guid SupplierId { get; set; } + [Required] + public Guid ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Supplier Supplier { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} diff --git a/DatabaseImplement/Models/Supply.cs b/DatabaseImplement/Models/Supply.cs index b30e50a..64e75f4 100644 --- a/DatabaseImplement/Models/Supply.cs +++ b/DatabaseImplement/Models/Supply.cs @@ -16,13 +16,14 @@ namespace DatabaseImplement.Models { public class Supply : ISupply { - [Required] public Guid Id { get; set; } [Required] public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } [Required] + public Guid SupplierId { get; set; } + [Required] public DateTime Date { get; set; } [Required] public SupplyStatus Status { get; set; } = SupplyStatus.Pending; @@ -43,7 +44,7 @@ namespace DatabaseImplement.Models } [ForeignKey("SupplyId")] public virtual List Products { get; set; } = new(); - + public virtual Supplier Supplier { get; set; } public static Supply Create(Database context, SupplyBindingModel model) { return new Supply() @@ -51,6 +52,8 @@ namespace DatabaseImplement.Models Id = model.Id, Name = model.Name, Price = model.Price, + Date = model.Date, + SupplierId = model.SupplierId, Products = model.SupplyProducts.Select(x => new SupplyProduct { @@ -64,17 +67,24 @@ namespace DatabaseImplement.Models Name = model.Name; Price = model.Price; } - public SupplyViewModel GetViewModel => new() + public SupplyViewModel GetViewModel { - Id = Id, - Name = Name, - Price = Price, - Products = SupplyProducts, - //supplierName сделать - Date = Date, - Status = Status - }; - public void UpdateComponents(Database context, SupplyBindingModel model) + get + { + var context = new Database(); + return new() + { + Id = Id, + Name = Name, + Price = Price, + Products = SupplyProducts, + Date = Date, + Status = Status, + SupplierName = context.Suppliers.FirstOrDefault(x => x.Id == Id)?.Name ?? string.Empty, + }; + } + } + public void UpdateProducts(Database context, SupplyBindingModel model) { var supplyProducts = context.SupplyProducts.Where(rec => rec.Id == model.Id).ToList(); From 644b5668ab0376ba869d0e7b8dd5b71706037371 Mon Sep 17 00:00:00 2001 From: the Date: Sat, 22 Jun 2024 18:13:07 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0,=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=B0=D0=BA=D1=82=D1=8B,=20?= =?UTF-8?q?=D0=BA=D1=80=D1=83=D0=B4=20=D0=BA=D0=BE=D1=80=D0=BE=D1=87=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BusinessLogic/BusinessLogic/ProductLogic.cs | 90 +++++++++- BusinessLogic/BusinessLogic/SupplierLogic.cs | 114 +++++++++++++ BusinessLogic/BusinessLogic/SupplyLogic.cs | 105 ++++++++++++ Contracts/SearchModels/ProductSearchModel.cs | 2 + Contracts/SearchModels/SupplySearchModel.cs | 1 + Contracts/ViewModels/ProductViewModel.cs | 2 +- .../Implements/ProductStorage.cs | 161 ++++++++++++++++++ DatabaseImplement/Models/Product.cs | 36 +++- 8 files changed, 502 insertions(+), 9 deletions(-) create mode 100644 BusinessLogic/BusinessLogic/SupplierLogic.cs create mode 100644 BusinessLogic/BusinessLogic/SupplyLogic.cs create mode 100644 DatabaseImplement/Implements/ProductStorage.cs diff --git a/BusinessLogic/BusinessLogic/ProductLogic.cs b/BusinessLogic/BusinessLogic/ProductLogic.cs index a177c19..e10985d 100644 --- a/BusinessLogic/BusinessLogic/ProductLogic.cs +++ b/BusinessLogic/BusinessLogic/ProductLogic.cs @@ -15,33 +15,111 @@ namespace BusinessLogic.BusinessLogic public class ProductLogic : IProductLogic { private readonly IProductStorage _productStorage; - public ProductLogic(IProductStorage productStorage) + private readonly ILogger _logger; + public ProductLogic(IProductStorage productStorage, ILogger logger) { _productStorage = productStorage; + _logger = logger; } public bool Create(ProductBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + if (_productStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; } public bool Delete(ProductBindingModel model) { - throw new NotImplementedException(); + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_productStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; } public ProductViewModel? ReadElement(ProductSearchModel model) { - throw new NotImplementedException(); + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id); + var element = _productStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; } public List? ReadList(ProductSearchModel? model) { - throw new NotImplementedException(); + _logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; } public bool Update(ProductBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + if (_productStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(ProductBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия продукта", nameof(model.Name)); + } + if (model.Price < 0) + { + throw new Exception("Цена продукта не может быть ниже нуля"); + } + if (model.Amount < 0) + { + throw new Exception("Кол-во продуктов не может быть ниже нуля"); + } + if (model.Rate < 0.0 || model.Rate > 5.0) + { + throw new Exception("Рейтинг продукта должен быть в пределах от 0.0 до 5.0"); + } + _logger.LogInformation("Product. ProductName:{Name}.Price:{ Price}. Id: { Id}", model.Name, model.Price, model.Id); + var element = _productStorage.GetElement(new ProductSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Продукт с таким названием уже есть"); + } } } } diff --git a/BusinessLogic/BusinessLogic/SupplierLogic.cs b/BusinessLogic/BusinessLogic/SupplierLogic.cs new file mode 100644 index 0000000..e71654e --- /dev/null +++ b/BusinessLogic/BusinessLogic/SupplierLogic.cs @@ -0,0 +1,114 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +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 SupplierLogic : ISupplierLogic + { + private readonly ISupplierStorage _supplierStorage; + private readonly ILogger _logger; + public SupplierLogic(ISupplierStorage supplierStorage, ILogger logger) + { + _supplierStorage = supplierStorage; + _logger = logger; + } + public bool Create(SupplierBindingModel model) + { + CheckModel(model); + if (_supplierStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(SupplierBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_supplierStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public SupplierViewModel? ReadElement(SupplierSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id); + var element = _supplierStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public List? ReadList(SupplierSearchModel? model) + { + _logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _supplierStorage.GetFullList() : _supplierStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Update(SupplierBindingModel model) + { + CheckModel(model); + if (_supplierStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + private void CheckModel(SupplierBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия поставщика", nameof(model.Name)); + } + if (model.Deals < 0) + { + throw new Exception("Кол-во сделок не может быть меньше нуля"); + } + foreach(var item in model.AvailibleProducts.Values) + { + if (item.Item2 < 0) + { + throw new Exception("Кол-во доступных продуктов не должно быть отрицательным"); + } + } + } + } +} diff --git a/BusinessLogic/BusinessLogic/SupplyLogic.cs b/BusinessLogic/BusinessLogic/SupplyLogic.cs new file mode 100644 index 0000000..63a40d9 --- /dev/null +++ b/BusinessLogic/BusinessLogic/SupplyLogic.cs @@ -0,0 +1,105 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.SearchModels; +using Contracts.StorageContracts; +using Contracts.ViewModels; +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 SupplyLogic : ISupplyLogic + { + private readonly ISupplyStorage _supplyStorage; + private readonly ILogger _logger; + public SupplyLogic(ISupplyStorage supplyStorage, ILogger logger) + { + _supplyStorage = supplyStorage; + _logger = logger; + } + + public bool Create(SupplyBindingModel model) + { + CheckModel(model); + if (_supplyStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(SupplyBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_supplyStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public SupplyViewModel? ReadElement(SupplySearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id); + var element = _supplyStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public List? ReadList(SupplySearchModel? model) + { + _logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _supplyStorage.GetFullList() : _supplyStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Update(SupplyBindingModel model) + { + CheckModel(model); + if (_supplyStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(SupplyBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия поставки", nameof(model.Name)); + } + } + } +} diff --git a/Contracts/SearchModels/ProductSearchModel.cs b/Contracts/SearchModels/ProductSearchModel.cs index a6527dd..1759010 100644 --- a/Contracts/SearchModels/ProductSearchModel.cs +++ b/Contracts/SearchModels/ProductSearchModel.cs @@ -12,5 +12,7 @@ namespace Contracts.SearchModels public string? Name { get; set; } public double? Price { get; set; } public double? Rate { get; set; } + public int? Amount { get; set; } + public bool? IsBeingSold { get; set; } } } diff --git a/Contracts/SearchModels/SupplySearchModel.cs b/Contracts/SearchModels/SupplySearchModel.cs index 6fd24c8..dee63a8 100644 --- a/Contracts/SearchModels/SupplySearchModel.cs +++ b/Contracts/SearchModels/SupplySearchModel.cs @@ -10,6 +10,7 @@ namespace Contracts.SearchModels public class SupplySearchModel { public Guid? Id { get; set; } + public string Name { get; set; } = string.Empty; public SupplyStatus? Status { get; set; } public DateTime? DateStart { get; set; } public DateTime? DateEnd { get; set; } diff --git a/Contracts/ViewModels/ProductViewModel.cs b/Contracts/ViewModels/ProductViewModel.cs index 1424a09..efb0338 100644 --- a/Contracts/ViewModels/ProductViewModel.cs +++ b/Contracts/ViewModels/ProductViewModel.cs @@ -11,7 +11,7 @@ namespace Contracts.ViewModels { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; - public int Price { get; set; } + public double Price { get; set; } public double Rating { get; set; } public bool IsBeingSold { get; set; } public int Amount { get; set; } diff --git a/DatabaseImplement/Implements/ProductStorage.cs b/DatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..feb9fc5 --- /dev/null +++ b/DatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,161 @@ +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; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new Database(); + var element = context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ProductSearchModel model) + { + if (!model.IsBeingSold.HasValue && !model.Price.HasValue && !model.Rate.HasValue && !model.Amount.HasValue && string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new Database(); + if (model.Price.HasValue) + { + return context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .Where(x => x.Price <= model.Price) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Rate.HasValue) + { + return context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .Where(x => x.Rate <= model.Rate) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Amount.HasValue && model.IsBeingSold.HasValue) + { + return context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .Where(x => x.IsBeingSold == model.IsBeingSold && x.Amount >= model.Amount) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .Where(x => x.Name == model.Name) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Products + .Include(x => x.Name) + .Include(x => x.Price) + .Include(x => x.IsBeingSold) + .Include(x => x.Rate) + .Include(x => x.Amount) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new Database(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Products.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/DatabaseImplement/Models/Product.cs b/DatabaseImplement/Models/Product.cs index bc14d3e..7edb9f4 100644 --- a/DatabaseImplement/Models/Product.cs +++ b/DatabaseImplement/Models/Product.cs @@ -7,6 +7,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace DatabaseImplement.Models { @@ -55,7 +56,21 @@ namespace DatabaseImplement.Models Amount = model.Amount }; - public void Update(ProductBindingModel model, Product product) + public static Product Create(Database context, ProductBindingModel model) + { + return new Product() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Rate = model.Rate, + IsBeingSold = model.IsBeingSold, + Amount = model.Amount + }; + } + + + public void Update(ProductBindingModel model) { if (model is null) { @@ -68,5 +83,22 @@ namespace DatabaseImplement.Models IsBeingSold = model.IsBeingSold; Amount = model.Amount; } - } + + public ProductViewModel GetViewModel + { + get + { + var context = new Database(); + return new() + { + Id = Id, + Name = Name, + Price = Price, + IsBeingSold = IsBeingSold, + Rating = Rate, + Amount = Amount + }; + } + } + } }