diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ClientLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ClientLogic.cs index 8149fcc..2885afd 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ClientLogic.cs @@ -28,7 +28,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic CheckModel(model); if (_storage.Insert(model) == null) { - _logger.LogWarning("Add operation failed"); + _logger.LogWarning("Create operation failed"); return false; } return true; @@ -91,10 +91,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic private void CheckModel(ClientBindingModel model, bool withParams = true) { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } if (!withParams) { return; diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs index b769e84..4addd9e 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs @@ -13,12 +13,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic private readonly ILogger _logger; private readonly ICostItemStorage _storage; + public CostItemLogic(ILogger logger, ICostItemStorage storage) { + _logger = logger; + _storage = storage; + } + public bool Create(CostItemBindingModel model) { CheckModel(model); if (_storage.Insert(model) == null) { - _logger.LogWarning("Add operation failed"); + _logger.LogWarning("Create operation failed"); return false; } return true; @@ -79,23 +84,22 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic private void CheckModel(CostItemBindingModel model, bool withParams = true) { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } if (!withParams) { return; } if (model.Price <= 0) { - throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price)); + throw new ArgumentNullException("Затраты должны быть больше 0", nameof(model.Price)); + } + if (string.IsNullOrEmpty(model.Name)) { + throw new ArgumentNullException("Отсутствует название статьи затрат", nameof(model.Price)); } _logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}"); var element = _storage.GetElement(new CostItemSearchModel { ID = model.ID }); if (element != null && element.ID != model.EmployeeID) { - throw new InvalidOperationException("Продукт с таким названием уже есть"); + throw new InvalidOperationException("Такая статья затрат уде есть"); } } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/EmployeeLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/EmployeeLogic.cs index a2dfb21..b7d5461 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/EmployeeLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/EmployeeLogic.cs @@ -10,46 +10,42 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ElectronicsShopBusinessLogic.BusinessLogic -{ - public class EmployeeLogic : IEmployeeLogic - { +namespace ElectronicsShopBusinessLogic.BusinessLogic { + public class EmployeeLogic : IEmployeeLogic { private readonly ILogger _logger; private readonly IEmployeeStorage _storage; - public bool Add(EmployeeBindingModel model) - { + public EmployeeLogic(ILogger loger, IEmployeeStorage storage) { + _logger = loger; + _storage = storage; + } + + public bool Create(EmployeeBindingModel model) { CheckModel(model); - if (_storage.Insert(model) == null) - { - _logger.LogWarning("Add operation failed"); + if (_storage.Insert(model) == null) { + _logger.LogWarning("Create operation failed"); return false; } return true; } - public bool Delete(EmployeeBindingModel model) - { + public bool Delete(EmployeeBindingModel model) { CheckModel(model, false); _logger.LogInformation($"Delete.ID:{model.ID}"); - if (_storage.Delete(model) == null) - { + if (_storage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } - public EmployeeViewModel? ReadElemet(EmployeeSearchModel model) - { - if (model == null) - { + public EmployeeViewModel? ReadElemet(EmployeeSearchModel model) { + if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation($"ReadElement.Login:{model.Login}.ID:{model.ID}"); var element = _storage.GetElement(model); - if (element == null) - { + if (element == null) { _logger.LogWarning("ReadElement. element not fount"); return null; } @@ -57,12 +53,10 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return element; } - public List? ReadList(EmployeeSearchModel model) - { + public List? ReadList(EmployeeSearchModel model) { _logger.LogInformation($"ReadList. ClientID:{model?.ID}"); var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ; - if (list == null) - { + if (list == null) { _logger.LogWarning("ReadList. return null list"); return null; } @@ -70,48 +64,39 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return list; } - public bool Update(EmployeeBindingModel model) - { + public bool Update(EmployeeBindingModel model) { CheckModel(model); - if (_storage.Update(model) == null) - { + if (_storage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } - private void CheckModel(EmployeeBindingModel model, bool withParams = true) - { - if (model == null) - { + private void CheckModel(EmployeeBindingModel model, bool withParams = true) { + if (model == null) { throw new ArgumentNullException(nameof(model)); } - if (!withParams) - { + if (!withParams) { return; } - if (string.IsNullOrEmpty(model.Login)) - { - throw new ArgumentNullException("Нет логина пользователя", nameof(model.Login)); + if (string.IsNullOrEmpty(model.Login)) { + throw new ArgumentNullException("Нет логина сотрудника", nameof(model.Login)); } - if (string.IsNullOrEmpty(model.EmployeeFIO)) - { - throw new ArgumentNullException("Нет имени пользователя", nameof(model.EmployeeFIO)); + if (string.IsNullOrEmpty(model.EmployeeFIO)) { + throw new ArgumentNullException("Нет имени сотрудника", nameof(model.EmployeeFIO)); } - if (string.IsNullOrEmpty(model.Password)) - { - throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); + if (string.IsNullOrEmpty(model.Password)) { + throw new ArgumentNullException("Нет пароля сотрудника", nameof(model.Password)); } _logger.LogInformation($"Client. ID:{model.ID}.ClientFIO:{model.EmployeeFIO}.Password:{model.Password}.Email:{model.Login}."); - var element = _storage.GetElement(new EmployeeSearchModel - { + var element = _storage.GetElement(new EmployeeSearchModel { Login = model.Login }); - if (element != null && element.Login != model.Login) - { - throw new InvalidOperationException("Клиент с такой почтой уже есть"); + if (element != null && element.Login != model.Login) { + throw new InvalidOperationException("Сотрудник с таким логином уже есть"); } } - } + } +} diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs index cfb20cc..b8ee1d1 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -44,9 +44,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic } private void CheckModel(OrderBindingModel model, bool withParams = true) { - if (model == null) { - throw new ArgumentNullException(nameof(model)); - } if (!withParams) { return; } @@ -56,7 +53,10 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic if (model.Sum <= 0) { throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum)); } - _logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}."); + if (model.ClientID < 0) { + throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientID)); + } + _logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}.ClientID:{model.ClientID}"); } public OrderViewModel? ReadElement(OrderSearchModel model) diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PayLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PaymentLogic.cs similarity index 74% rename from ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PayLogic.cs rename to ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PaymentLogic.cs index 5d3ec65..ce6f6e5 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PayLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/PaymentLogic.cs @@ -8,11 +8,16 @@ using Microsoft.Extensions.Logging; namespace ElectronicsShopBusinessLogic.BusinessLogic { - public class PayLogic : IPaymentLogic + public class PaymentLogic : IPaymentLogic { private readonly ILogger _logger; private readonly IPaymentStorage _storage; + public PaymentLogic(ILogger logger, IPaymentStorage storage) { + _logger = logger; + _storage = storage; + } + public bool CreatePay(PaymentBindingModel model) { CheckModel(model); @@ -44,37 +49,29 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic public List? ReadList(PaymentSearchModel? model) { _logger.LogInformation($"ReadList. ClientID:{model?.ID}"); - /*var list = model == null ? _storage.GetFullList() : _storage.GetFillteredList(model); ; + var list = model == null ? _storage.GetFullList() : _storage.GetFillteredList(model); ; if (list == null) { _logger.LogWarning("ReadList. return null list"); return null; } - _logger.LogInformation($"ReadList.Count:{list.Count}");*/ + _logger.LogInformation($"ReadList.Count:{list.Count}"); return null; } private void CheckModel(PaymentBindingModel model, bool withParams = true) { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } if (!withParams) { return; } if (model.SumPayment <= 0) { - throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.SumPayment)); - } - _logger.LogInformation($"Payment. ID:{model.ID}.ProductID:{model.ProductID}.Sum:{model.SumPayment}.OrderID:{model.OrderID}.PayOption{model.PayOption}"); - var element = _storage.GetElement(new PaymentSearchModel { ID = model.ID }); - if (element != null && element.PayOption != model.PayOption) - { - throw new InvalidOperationException("Продукт с таким названием уже есть"); + throw new ArgumentNullException("Сумма оплаты должна быть больше 0", nameof(model.SumPayment)); } + _logger.LogInformation($"Payment. ID:{model.ID}.ProductID:{model.ProductID}.Sum:{model.SumPayment}.OrderID:{model.OrderID}" + + $".PayOption{model.PayOption}"); } } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs index 4db2864..cb0987a 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ProductLogic.cs @@ -84,9 +84,6 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic private void CheckModel(ProductBindingModel model, bool withParams = true) { - if (model == null) { - throw new ArgumentNullException(nameof(model)); - } if (!withParams) { return; } diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IEmployeeLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IEmployeeLogic.cs index 5d5eba8..537754d 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IEmployeeLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IEmployeeLogic.cs @@ -12,7 +12,7 @@ namespace ElectronicsShopContracts.BusinessLogicContracts { List? ReadList(EmployeeSearchModel model); EmployeeViewModel? ReadElemet(EmployeeSearchModel model); - bool Add(EmployeeBindingModel model); + bool Create(EmployeeBindingModel model); bool Update(EmployeeBindingModel model); bool Delete(EmployeeBindingModel model); } diff --git a/ElectronicsShop/ElectronicsShopContracts/SearchModels/ClientSearchModel.cs b/ElectronicsShop/ElectronicsShopContracts/SearchModels/ClientSearchModel.cs index 9632f91..2c9b389 100644 --- a/ElectronicsShop/ElectronicsShopContracts/SearchModels/ClientSearchModel.cs +++ b/ElectronicsShop/ElectronicsShopContracts/SearchModels/ClientSearchModel.cs @@ -10,5 +10,6 @@ namespace ElectronicsShopContracts.SearchModels { public int? ID { get; set; } public string? Email { get; set; } + public string? Passwrod { get; set; } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/ICostItemStorage.cs b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/ICostItemStorage.cs index 7b47d0d..d59750e 100644 --- a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/ICostItemStorage.cs +++ b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/ICostItemStorage.cs @@ -12,7 +12,7 @@ namespace ElectronicsShopContracts.StorageContracts { List GetFullList(); List GetFillteredList(CostItemSearchModel model); - CostItemViewModel GetElement(CostItemSearchModel model); + CostItemViewModel? GetElement(CostItemSearchModel model); CostItemViewModel? Insert(CostItemBindingModel model); CostItemViewModel? Update(CostItemBindingModel model); CostItemViewModel? Delete(CostItemBindingModel model); diff --git a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IPaymentStorage.cs b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IPaymentStorage.cs index 9f630c4..f80d0d4 100644 --- a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IPaymentStorage.cs +++ b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IPaymentStorage.cs @@ -11,8 +11,10 @@ namespace ElectronicsShopContracts.StorageContracts { public interface IPaymentStorage { - List GetFullList(); + List? GetFullList(); + List? GetFillteredList(PaymentSearchModel model); PaymentViewModel? GetElement(PaymentSearchModel model); + PaymentViewModel? Insert(PaymentBindingModel model); } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs index 7da2eb0..1de11e2 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs @@ -20,6 +20,6 @@ namespace ElectronicsShopDataBaseImplement public virtual DbSet OrderProducts { set; get; } public virtual DbSet Employees { set; get; } public virtual DbSet CostItems { set; get; } - + public virtual DbSet Payments { get; set; } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ClientStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ClientStorage.cs index a139737..6a92378 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ClientStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ClientStorage.cs @@ -3,6 +3,8 @@ using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query.Internal; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +15,29 @@ namespace ElectronicsShopDataBaseImplement.Implements { public class ClientStorage : IClientStorage { - public ClientViewModel? Delete(ClientBindingModel model) + public ClientViewModel? Insert(ClientBindingModel model) { + var newComponent = Client.Create(model); + if (newComponent == null) { + return null; + } + using var context = new Database(); + context.Clients.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) { + using var context = new Database(); + var component = context.Clients.FirstOrDefault(x => x.ID == model.ID); + if (component == null) { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) { using var context = new Database(); var element = context.Clients.FirstOrDefault(rec => rec.ID == model.ID); @@ -28,14 +52,21 @@ namespace ElectronicsShopDataBaseImplement.Implements public ClientViewModel? GetElement(ClientSearchModel model) { - if (string.IsNullOrEmpty(model.Email) && !model.ID.HasValue) - { - return null; - } using var context = new Database(); - return context.Clients.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email || - (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + if (model.ID.HasValue) { + return context.Clients + .Include(x => x.Orders) + .FirstOrDefault(x => model.ID.HasValue && + x.ID == model.ID) + ?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Passwrod)) { + return context.Clients + .Include (x => x.Orders) + .FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Passwrod)) + ?.GetViewModel; + } + return new(); } public List GetFilteredList(ClientSearchModel model) @@ -45,39 +76,20 @@ namespace ElectronicsShopDataBaseImplement.Implements return new(); } using var context = new Database(); - return context.Clients.Where(x => x.Email.Contains(model.Email)).Select(x => x.GetViewModel).ToList(); + return context.Clients + .Include(x => x.Orders) + .Where(x => x.Email + .Contains(model.Email)) + .Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new Database(); - return context.Clients.Select(x => x.GetViewModel).ToList(); - } - - public ClientViewModel? Insert(ClientBindingModel model) - { - var newComponent = Client.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new Database(); - context.Clients.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public ClientViewModel? Update(ClientBindingModel model) - { - using var context = new Database(); - var component = context.Clients.FirstOrDefault(x => x.ID == model.ID); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; + return context.Clients + .Include(x => x.Orders) + .Select(x => x.GetViewModel) + .ToList(); } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs index 6e1f5b1..816065c 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs @@ -13,7 +13,29 @@ namespace ElectronicsShopDataBaseImplement.Implements { public class CostItemStorage : ICostItemStorage { - public CostItemViewModel? Delete(CostItemBindingModel model) + public CostItemViewModel? Insert(CostItemBindingModel model) { + var newComponent = CostItem.Create(model); + if (newComponent == null) { + return null; + } + using var context = new Database(); + context.CostItems.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public CostItemViewModel? Update(CostItemBindingModel model) { + using var context = new Database(); + var component = context.CostItems.FirstOrDefault(x => x.ID == model.ID); + if (component == null) { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public CostItemViewModel? Delete(CostItemBindingModel model) { using var context = new Database(); var element = context.CostItems.FirstOrDefault(rec => rec.ID == model.ID); @@ -26,16 +48,17 @@ namespace ElectronicsShopDataBaseImplement.Implements return null; } - public CostItemViewModel GetElement(CostItemSearchModel model) + public CostItemViewModel? GetElement(CostItemSearchModel model) { if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; } using var context = new Database(); - return context.CostItems.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name || - (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + return context.CostItems + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) + && x.Name == model.Name) || + (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; } public List GetFillteredList(CostItemSearchModel model) @@ -45,7 +68,8 @@ namespace ElectronicsShopDataBaseImplement.Implements return new(); } using var context = new Database(); - return context.CostItems.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList(); + return context.CostItems.Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel).ToList(); } public List GetFullList() @@ -53,31 +77,5 @@ namespace ElectronicsShopDataBaseImplement.Implements using var context = new Database(); return context.CostItems.Select(x => x.GetViewModel).ToList(); } - - public CostItemViewModel? Insert(CostItemBindingModel model) - { - var newComponent = CostItem.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new Database(); - context.CostItems.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public CostItemViewModel? Update(CostItemBindingModel model) - { - using var context = new Database(); - var component = context.CostItems.FirstOrDefault(x => x.ID == model.ID); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs index ba0f55e..a4714aa 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs @@ -9,7 +9,29 @@ namespace ElectronicsShopDataBaseImplement.Implements { public class EmployeeStorage : IEmployeeStorage { - public EmployeeViewModel? Delete(EmployeeBindingModel model) + public EmployeeViewModel? Insert(EmployeeBindingModel model) { + var newComponent = Employee.Create(model); + if (newComponent == null) { + return null; + } + using var context = new Database(); + context.Employees.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public EmployeeViewModel? Update(EmployeeBindingModel model) { + using var context = new Database(); + var component = context.Employees.FirstOrDefault(x => x.ID == model.ID); + if (component == null) { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public EmployeeViewModel? Delete(EmployeeBindingModel model) { using var context = new Database(); var element = context.Employees.FirstOrDefault(rec => rec.ID == model.ID); @@ -29,15 +51,12 @@ namespace ElectronicsShopDataBaseImplement.Implements return null; } using var context = new Database(); - return context.Employees.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Login) && x.EmployeeFIO == model.EmployeFIO || - (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + return context.Employees + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && + x.EmployeeFIO == model.EmployeFIO || + (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; } - public object GetElement(CostItemSearchModel costItemSearchModel) { - throw new NotImplementedException(); - } - public List GetFilteredList(EmployeeSearchModel model) { if (string.IsNullOrEmpty(model.Login)) @@ -45,7 +64,9 @@ namespace ElectronicsShopDataBaseImplement.Implements return new(); } using var context = new Database(); - return context.Employees.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList(); + return context.Employees.Where(x => x.Login + .Contains(model.Login)) + .Select(x => x.GetViewModel).ToList(); } public List GetFullList() @@ -53,31 +74,5 @@ namespace ElectronicsShopDataBaseImplement.Implements using var context = new Database(); return context.Employees.Select(x => x.GetViewModel).ToList(); } - - public EmployeeViewModel? Insert(EmployeeBindingModel model) - { - var newComponent = Employee.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new Database(); - context.Employees.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public EmployeeViewModel? Update(EmployeeBindingModel model) - { - using var context = new Database(); - var component = context.Employees.FirstOrDefault(x => x.ID == model.ID); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs index a947f6b..9002793 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs @@ -10,7 +10,38 @@ namespace ElectronicsShopDataBaseImplement.Implements { public class OrderStorage : IOrderStorage { - public OrderViewModel? Delete(OrderBindingModel model) + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new Database(); + var newComponent = Order.Create(context,model); + if (newComponent == null) + { + return null; + } + context.Orders.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) { + using var context = new Database(); + using var transcation = context.Database.BeginTransaction(); + try { + var product = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); + if (product == null) { + return null; + } + product.UpdateProducts(context, model); + transcation.Commit(); + return product.GetViewModel; + } + catch { + transcation.Rollback(); + throw; + } + } + + public OrderViewModel? Delete(OrderBindingModel model) { using var context = new Database(); var element = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); @@ -29,8 +60,11 @@ namespace ElectronicsShopDataBaseImplement.Implements return null; } using var context = new Database(); - return context.Orders.FirstOrDefault(x => - (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; + return context.Orders + .Include(x => x.Payments) + .Include(x => x.Products) + .ThenInclude(x => x._product) + .FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) @@ -51,33 +85,11 @@ namespace ElectronicsShopDataBaseImplement.Implements public List GetFullList() { using var context = new Database(); - return context.Orders.Select(x => x.GetViewModel).ToList(); + return context.Orders + .Include(x => x.Products) + .ThenInclude(x => x._product).ToList() + .Select(x => x.GetViewModel).ToList(); } - public OrderViewModel? Insert(OrderBindingModel model) - { - using var context = new Database(); - var newComponent = Order.Create(context,model); - if (newComponent == null) - { - return null; - } - context.Orders.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public OrderViewModel? Update(OrderBindingModel model) - { - using var context = new Database(); - var component = context.Orders.FirstOrDefault(x => x.ID == model.ID); - if (component == null) - { - return null; - } - component.Update(context ,model); - context.SaveChanges(); - return component.GetViewModel; - } - } + } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/PaymentStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/PaymentStorage.cs new file mode 100644 index 0000000..f88a194 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/PaymentStorage.cs @@ -0,0 +1,49 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Security; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Implements { + public class PaymentStorage : IPaymentStorage { + public PaymentViewModel? Insert(PaymentBindingModel model) { + var newPayment = Payment.Create(model); + if (newPayment == null) { + return null; + } + using var context = new Database(); + context.Payments.Add(newPayment); + context.SaveChanges(); + return newPayment.GetViewModel; + } + + public PaymentViewModel? GetElement(PaymentSearchModel model) { + if (model.ProductID.HasValue || model.OrderID.HasValue) { + return null; + } + using var context = new Database(); + return context.Payments.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel; + } + + public List? GetFillteredList(PaymentSearchModel model) { + if (model.ProductID.HasValue || model.OrderID.HasValue) { + return new(); + } + using var context = new Database(); + return context.Payments + .Where(x => x.ID == model.ID) + .Select(x => x.GetViewModel).ToList(); + } + + public List? GetFullList() { + using var context = new Database(); + return context.Payments.Select(x => x.GetViewModel).ToList(); + } + } +} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs index 0b53295..6cae2bf 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs @@ -13,7 +13,29 @@ namespace ElectronicsShopDataBaseImplement.Implements { public class ProductStorage : IProductStorage { - public ProductViewModel? Delete(ProductBindingModel model) + public ProductViewModel? Insert(ProductBindingModel model) { + var newComponent = Product.Create(model); + if (newComponent == null) { + return null; + } + using var context = new Database(); + context.Products.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) { + using var context = new Database(); + var component = context.Products.FirstOrDefault(x => x.ID == model.ID); + if (component == null) { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ProductViewModel? Delete(ProductBindingModel model) { using var context = new Database(); var element = context.Products.FirstOrDefault(rec => rec.ID == model.ID); @@ -33,8 +55,8 @@ namespace ElectronicsShopDataBaseImplement.Implements return null; } using var context = new Database(); - return context.Products.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.ProductName) && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + return context.Products.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && + (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; } public List GetFilteredList(ProductSearchModel model) @@ -47,38 +69,10 @@ namespace ElectronicsShopDataBaseImplement.Implements return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList(); } - public List GetFullList() { using var context = new Database(); return context.Products.Select(x => x.GetViewModel).ToList(); } - - public ProductViewModel? Insert(ProductBindingModel model) - { - var newComponent = Product.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new Database(); - context.Products.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - - public ProductViewModel? Update(ProductBindingModel model) - { - using var context = new Database(); - var component = context.Products.FirstOrDefault(x => x.ID == model.ID); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } } }