diff --git a/BusinessLogic/BusinessLogic/ProductLogic.cs b/BusinessLogic/BusinessLogic/ProductLogic.cs new file mode 100644 index 0000000..e10985d --- /dev/null +++ b/BusinessLogic/BusinessLogic/ProductLogic.cs @@ -0,0 +1,125 @@ +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; + private readonly ILogger _logger; + public ProductLogic(IProductStorage productStorage, ILogger logger) + { + _productStorage = productStorage; + _logger = logger; + } + public bool Create(ProductBindingModel model) + { + CheckModel(model); + if (_productStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(ProductBindingModel model) + { + 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) + { + 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) + { + _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) + { + 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/BindingModels/MediaFileBindingModel.cs b/Contracts/BindingModels/MediaFileBindingModel.cs new file mode 100644 index 0000000..dfa6386 --- /dev/null +++ b/Contracts/BindingModels/MediaFileBindingModel.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 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 new file mode 100644 index 0000000..8f44dbd --- /dev/null +++ b/Contracts/BindingModels/ProductBindingModel.cs @@ -0,0 +1,19 @@ +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.BindingModels +{ + 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/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..e0c32ed --- /dev/null +++ b/Contracts/BindingModels/SellBindingModel.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 SellBindingModel + { + public Guid Id { get; set; } + public DateTime DateSell { get; set; } + } +} diff --git a/Contracts/BindingModels/SupplierBindingModel.cs b/Contracts/BindingModels/SupplierBindingModel.cs new file mode 100644 index 0000000..49f0469 --- /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..44ba57c --- /dev/null +++ b/Contracts/BindingModels/SupplyBindingModel.cs @@ -0,0 +1,21 @@ +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 +{ + public class SupplyBindingModel : ISupply + { + 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; } + 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/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/IProductLogic.cs b/Contracts/BusinessLogicContracts/IProductLogic.cs new file mode 100644 index 0000000..4711e97 --- /dev/null +++ b/Contracts/BusinessLogicContracts/IProductLogic.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 IProductLogic + { + 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/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/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/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/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/ProductSearchModel.cs b/Contracts/SearchModels/ProductSearchModel.cs new file mode 100644 index 0000000..1759010 --- /dev/null +++ b/Contracts/SearchModels/ProductSearchModel.cs @@ -0,0 +1,18 @@ +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; } + public int? Amount { get; set; } + public bool? IsBeingSold { 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/SearchModels/SupplierSearchModel.cs b/Contracts/SearchModels/SupplierSearchModel.cs new file mode 100644 index 0000000..2a889e9 --- /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..dee63a8 --- /dev/null +++ b/Contracts/SearchModels/SupplySearchModel.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 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/StorageContracts/IProductStorage.cs b/Contracts/StorageContracts/IProductStorage.cs new file mode 100644 index 0000000..7d8e2bd --- /dev/null +++ b/Contracts/StorageContracts/IProductStorage.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 IProductStorage + { + 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/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/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); + } +} 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/MediaFileViewModel.cs b/Contracts/ViewModels/MediaFileViewModel.cs new file mode 100644 index 0000000..6acc484 --- /dev/null +++ b/Contracts/ViewModels/MediaFileViewModel.cs @@ -0,0 +1,29 @@ +using Contracts.BindingModels; +using DataModels.Models; +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; + 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 new file mode 100644 index 0000000..efb0338 --- /dev/null +++ b/Contracts/ViewModels/ProductViewModel.cs @@ -0,0 +1,20 @@ +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 double 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 new file mode 100644 index 0000000..b14933c --- /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 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 new file mode 100644 index 0000000..285d63f --- /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 DateSell { get; set; } + public ISupplyDoc? SupplyDoc { get; set; } + } +} diff --git a/Contracts/ViewModels/SupplierViewModel.cs b/Contracts/ViewModels/SupplierViewModel.cs new file mode 100644 index 0000000..09b2af2 --- /dev/null +++ b/Contracts/ViewModels/SupplierViewModel.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 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..08f7046 --- /dev/null +++ b/Contracts/ViewModels/SupplyViewModel.cs @@ -0,0 +1,23 @@ +using DataModels.Enums; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Contracts.ViewModels +{ + public class SupplyViewModel + { + 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(); + public DateTime Date { get; set; } + public SupplyStatus Status { 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/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 new file mode 100644 index 0000000..d4db8bd --- /dev/null +++ b/DataModels/Models/IMediaFile.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 IMediaFile : IId + { + string Name { get; } + string Location { get; } + Guid ProductId { get; } + } +} diff --git a/DataModels/Models/IProduct.cs b/DataModels/Models/IProduct.cs new file mode 100644 index 0000000..bfb12c0 --- /dev/null +++ b/DataModels/Models/IProduct.cs @@ -0,0 +1,19 @@ +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 IsBeingSold { get; } + public double Rate { get; } + int Amount { get; } + // будут браться через mediafilestorage так что скорее всего это тут не надо + // List MediaFiles { 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/ISupplier.cs b/DataModels/Models/ISupplier.cs new file mode 100644 index 0000000..4289d90 --- /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..fff5ab5 --- /dev/null +++ b/DataModels/Models/ISupply.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 DataModels.Models +{ + public interface ISupply: IId + { + string Name { get; } + double Price { get; } + Guid SupplierId { get; } + DateTime Date { get; } + SupplyStatus Status { get; } + Dictionary SupplyProducts { get; } + } +} diff --git a/DataModels/Models/ISupplyDoc.cs b/DataModels/Models/ISupplyDoc.cs new file mode 100644 index 0000000..ca45699 --- /dev/null +++ b/DataModels/Models/ISupplyDoc.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 ISupplyDoc : IId + { + string Name { get; } + + Guid SupplyId { get; } + } +} diff --git a/DatabaseImplement/Database.cs b/DatabaseImplement/Database.cs index 7bb94c7..f17e76a 100644 --- a/DatabaseImplement/Database.cs +++ b/DatabaseImplement/Database.cs @@ -21,5 +21,14 @@ 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 Supplies { get; set; } = null!; + public virtual DbSet SupplyProducts { get; set; } = null!; + public virtual DbSet Suppliers { get; set; } = null!; + public virtual DbSet SupplierProducts { get; set; } = null!; + + public virtual DbSet MediaFiles { get; set; } = null!; } } \ No newline at end of file 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/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/MediaFile.cs b/DatabaseImplement/Models/MediaFile.cs new file mode 100644 index 0000000..bc3c9ec --- /dev/null +++ b/DatabaseImplement/Models/MediaFile.cs @@ -0,0 +1,70 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +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 Guid Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + 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 new file mode 100644 index 0000000..7edb9f4 --- /dev/null +++ b/DatabaseImplement/Models/Product.cs @@ -0,0 +1,104 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace DatabaseImplement.Models +{ + 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; } + + 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 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) + { + throw new ArgumentNullException("Update product: binding model is null"); + } + + Name = model.Name; + Price = model.Price; + Rate = model.Rate; + 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 + }; + } + } + } +} diff --git a/DatabaseImplement/Models/Purchase.cs b/DatabaseImplement/Models/Purchase.cs new file mode 100644 index 0000000..5430b34 --- /dev/null +++ b/DatabaseImplement/Models/Purchase.cs @@ -0,0 +1,49 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +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; + 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 new file mode 100644 index 0000000..03a7f26 --- /dev/null +++ b/DatabaseImplement/Models/Sell.cs @@ -0,0 +1,44 @@ +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatabaseImplement.Models +{ + 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; + } + } +} 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 new file mode 100644 index 0000000..64e75f4 --- /dev/null +++ b/DatabaseImplement/Models/Supply.cs @@ -0,0 +1,118 @@ +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 + { + 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; + 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 virtual Supplier Supplier { get; set; } + public static Supply Create(Database context, SupplyBindingModel model) + { + return new Supply() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Date = model.Date, + SupplierId = model.SupplierId, + 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 + { + 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(); + 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(); + } +}