From 2d89e0973bc057fe044d6f590c5c97eeacb7ea7a Mon Sep 17 00:00:00 2001 From: ujijrujijr Date: Sat, 27 Apr 2024 15:55:58 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D0=BA=D1=83=20(=D0=9F=D0=A0=D0=9E=D0=92=D0=95=D0=A0=D0=98?= =?UTF-8?q?=D0=A2=D0=AC)=20=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=81=D0=BB=D0=BE=D0=B9=20=D0=B1=D0=B0?= =?UTF-8?q?=D0=B7=D1=8B=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20(=D0=9F?= =?UTF-8?q?=D0=A0=D0=9E=D0=92=D0=95=D0=A0=D0=98=D0=A2=D0=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 174 ++++++++++++++++++ .../BusinessLogics/RequestLogic.cs | 108 +++++++++++ .../BusinessLogics/ShipmentLogic.cs | 112 +++++++++++ .../BusinessLogics/UserLogic.cs | 1 + .../BusinessLogicContracts/IOrderLogic.cs | 4 +- .../ViewModels/OrderViewModel.cs | 3 - .../ViewModels/RequestViewModel.cs | 2 - .../ViewModels/ShipmentViewModel.cs | 4 - .../Implements/OrderStorage.cs | 121 +++++++++++- .../Implements/RequestStorage.cs | 45 +++-- .../Implements/ShipmentStorage.cs | 11 +- .../Implements/UserStorage.cs | 24 ++- ComputerShopDatabaseImplement/Models/Order.cs | 1 - .../Models/Request.cs | 17 +- .../Models/Shipment.cs | 1 - 15 files changed, 577 insertions(+), 51 deletions(-) create mode 100644 ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs create mode 100644 ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs create mode 100644 ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs diff --git a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..f9ce33c --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,174 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + //Хранение всех заказов + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _orderStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement order not found"); + return null; + } + _logger.LogInformation("ReadElement order found. Id:{Id}", element.Id); + return element; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.DateCreate > DateTime.Now) + { + throw new ArgumentException($"Дата создания заказа {model.DateCreate} не может быть в будущем"); + } + } + + public bool Create(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Invalid order status"); + return false; + } + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + + } + + //!!!ПРОВЕРИТЬ + public bool Update(OrderBindingModel model) + { + CheckModel(model, false); + var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + //!!!ПРОВЕРИТЬ + //if (model.Status != null & ChangeStatus(model, order.Status)) + //{ + // ordee.DateCreate[model.DateCreate; + // return true; + //} + //model.Status = order.Status; + //model.Sum = order.Sum; + return true; + } + + public bool Delete(OrderBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_orderStorage.Delete(model) == null) + { + _logger.LogWarning("Delete order operation failed"); + return false; + } + return true; + } + + //!!!ПРОВЕРИТЬ + private bool ChangeStatus(OrderBindingModel model, OrderStatus newStatus) + { + CheckModel(model, false); + var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (newStatus - model.Status == 1) + { + model.Status = newStatus; + + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update order operation failed"); + return false; + } + return true; + } + if (order.Status + 1 != newStatus) + { + _logger.LogWarning("Change status operation failed. Incorrect new status: {newStatus}. Current status: {currStatus}", newStatus, order.Status); + return false; + } + _logger.LogWarning("Changing status operation faled: current:{Status}: required:{newStatus}.", model.Status, newStatus); + throw new ArgumentException($"Невозможно присвоить статус {newStatus} заказу с текущим статусом {model.Status}"); + } + + //Перевод заказа в состояние выполнения + public bool TakeOrderInWork(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } + + //Перевод заказа в состояние готовности + public bool FinishOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Готов); + } + //Перевод заказа в состояние выдачи (окончательное завершение) + public bool DeliveryOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выдан); + } + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs new file mode 100644 index 0000000..64d2f4d --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs @@ -0,0 +1,108 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class RequestLogic : IRequestLogic + { + private readonly ILogger _logger; + private readonly IRequestStorage _requestStorage; + public RequestLogic(ILogger logger, IRequestStorage requestStorage) + { + _logger = logger; + _requestStorage = requestStorage; + } + + public List? ReadList(RequestSearchModel? model) + { + var list = model == null ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList requests return null list"); + return null; + } + _logger.LogInformation("ReadList requests.Count:{Count}", list.Count); + return list; + } + + public RequestViewModel? ReadElement(RequestSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + var element = _requestStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement request not found"); + return null; + } + _logger.LogInformation("ReadElement reguest found Id:{Id}", element.Id); + return element; + } + + public bool Create(RequestBindingModel model) + { + CheckModel(model); + if (_requestStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(RequestBindingModel model) + { + CheckModel(model); + if (_requestStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(RequestBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_requestStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + //!!!мб проверять, что есть userID + private void CheckModel(RequestBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО клиента, оформившего заявку", nameof(model.ClientFIO)); + } + if (model.DateRequest > DateTime.Now) + { + throw new ArgumentException($"Дата оформления заявки {model.DateRequest} не может быть в будущем"); + } + } + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs new file mode 100644 index 0000000..6cb19b9 --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs @@ -0,0 +1,112 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class ShipmentLogic : IShipmentLogic + { + private readonly ILogger _logger; + private readonly IShipmentStorage _shipmentStorage; + + public ShipmentLogic(ILogger logger, IShipmentStorage shipmentStorage) + { + _logger = logger; + _shipmentStorage = shipmentStorage; + } + + //!!!ТУТ МБ ПО-ДРУГОМУ ПРИСВАИВАТЬ ЗНАЧЕНИЕ ДЛЯ list (через fulllist) + //!!!ИЛИ СОЗДАТЬ ОТДЕЛЬНЫЙ МЕТОД ReadListUser + public List ReadList(ShipmentSearchModel? model) + { + _logger.LogInformation("ReadList. ShipmentId:{Id}", model?.Id); + var list = model == null ? _shipmentStorage.GetFullList() : _shipmentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList Count:{Count}", list.Count); + return list; + } + + public ShipmentViewModel? ReadElement(ShipmentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var shipment = _shipmentStorage.GetElement(model); + if (shipment == null) + { + _logger.LogWarning("ReadElement shipment not found"); + return null; + } + _logger.LogInformation("ReadElement found. Id:{Id}", shipment.Id); + return shipment; + } + + public bool Create(ShipmentBindingModel model) + { + CheckModel(model); + if (_shipmentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ShipmentBindingModel model) + { + CheckModel(model); + if (_shipmentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ShipmentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shipmentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ShipmentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ProviderName)) + { + throw new ArgumentNullException("Нет имени поставщика", nameof(model.ProviderName)); + } + if (model.DateShipment > DateTime.Now) + { + throw new ArgumentException($"Дата поступления {model.DateShipment} партии товаров не может быть в будущем"); + } + } + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs index 5bafe23..b966799 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs @@ -88,6 +88,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics return true; } + //!!!мб проверять, что есть userID private void CheckModel(UserBindingModel model, bool withParams = true) { if (model == null) diff --git a/ComputerShopContracts/BusinessLogicContracts/IOrderLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IOrderLogic.cs index acc6410..b12b584 100644 --- a/ComputerShopContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/ComputerShopContracts/BusinessLogicContracts/IOrderLogic.cs @@ -14,7 +14,9 @@ namespace ComputerShopContracts.BusinessLogicContracts { List? ReadList(OrderSearchModel? model); OrderViewModel? ReadElement(OrderSearchModel model); - bool CreateOrder(OrderBindingModel model); + bool Create(OrderBindingModel model); + bool Update(OrderBindingModel model); + bool Delete(OrderBindingModel model); bool TakeOrderInWork(OrderBindingModel model); bool FinishOrder(OrderBindingModel model); bool DeliveryOrder(OrderBindingModel model); diff --git a/ComputerShopContracts/ViewModels/OrderViewModel.cs b/ComputerShopContracts/ViewModels/OrderViewModel.cs index b63f378..83bc579 100644 --- a/ComputerShopContracts/ViewModels/OrderViewModel.cs +++ b/ComputerShopContracts/ViewModels/OrderViewModel.cs @@ -16,9 +16,6 @@ namespace ComputerShopContracts.ViewModels public int UserId { get; set; } - //!!!МБ НЕ НУЖЕН string.Empty - [DisplayName("Ник пользователя")] - public string UserLogin { get; set; } = string.Empty; //!!!ТУТ МБ НАДО DisplayName (НО ВЯРД ЛИ) diff --git a/ComputerShopContracts/ViewModels/RequestViewModel.cs b/ComputerShopContracts/ViewModels/RequestViewModel.cs index 0a23b2c..3397928 100644 --- a/ComputerShopContracts/ViewModels/RequestViewModel.cs +++ b/ComputerShopContracts/ViewModels/RequestViewModel.cs @@ -18,8 +18,6 @@ namespace ComputerShopContracts.ViewModels public int UserId { get; set; } //!!!МБ ДОБАВИТЬ НИК ПОЛЬЗОВАТЕЛЯ, СОЗДАВШЕГО ЗАЯВКУ - [DisplayName("Ник пользователя")] - public string UserLogin { get; set; } = string.Empty; //id сборки public int? AssemblyId { get; set; } diff --git a/ComputerShopContracts/ViewModels/ShipmentViewModel.cs b/ComputerShopContracts/ViewModels/ShipmentViewModel.cs index 6bc5176..67d274f 100644 --- a/ComputerShopContracts/ViewModels/ShipmentViewModel.cs +++ b/ComputerShopContracts/ViewModels/ShipmentViewModel.cs @@ -17,10 +17,6 @@ namespace ComputerShopContracts.ViewModels //!!!ТУТ МБ НЕ НУЖЕН DisplayName public int UserId { get; set; } - //!!!МБ НЕ НУЖЕН string.Empty - [DisplayName("Ник пользователя")] - public string UserLogin { get; set; } = string.Empty; - public Dictionary ShipmentOrders { get; set; } = new(); //!!!МБ НЕ НУЖЕН string.Empty diff --git a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs index 45aad83..b9b86da 100644 --- a/ComputerShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs @@ -1,6 +1,10 @@ -using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; using System.Collections.Generic; using System.Linq; @@ -10,19 +14,116 @@ using System.Threading.Tasks; namespace ComputerShopDatabaseImplement.Implements { //!!!ПОДОБИЕ component + //!!! ДОПИСАТЬ public class OrderStorage : IOrderStorage { - //!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод) //!!!ТУТ СВЯЗЬ С НЕСКОЛЬКИМИ СУЩНОСТЯМИ - public List GetFullList() + //!!!НЕ ФАКТ, ЧТО ПРАВИЛЬНО + //!!!мб присоединять user + public List GetFullList() { - //using var context = new ComputerShopDatabase(); - //return context.Orders - // .Include(x => x.Shi) - // .ThenInclude(x => x.Order) - // .ToList() - // .Select(x => x.GetViewModel) - // .ToList(); + using var context = new ComputerShopDatabase(); + return context.Orders + .Include(x => x.ShipmentOrders) + .ThenInclude(x => x.Shipment) + .Include(x => x.RequestOrders) + .ThenInclude(x => x.Request) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + //!!!ПРОВЕРИТЬ + public List GetFilteredList(OrderSearchModel model) + { + using var context = new ComputerShopDatabase(); + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Orders + .Where(x => (x.UserId == model.UserId) && (x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)) + .Include(x => x.ShipmentOrders) + .ThenInclude(x => x.Shipment) + .Include(x => x.RequestOrders) + .ThenInclude(x => x.Request) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.Status.HasValue) + { + return context.Orders + .Where(x => (x.UserId == model.UserId) && (x.Status == model.Status)) + .Include(x => x.ShipmentOrders) + .ThenInclude(x => x.Shipment) + .Include(x => x.RequestOrders) + .ThenInclude(x => x.Request) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + //возвращение просто всех заказов пользователя (замена GetFullList) + return context.Orders + .Where(x => x.UserId == model.UserId) + .Include(x => x.ShipmentOrders) + .ThenInclude(x => x.Shipment) + .Include(x => x.RequestOrders) + .ThenInclude(x => x.Request) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + //!!!ПРОВЕРИТЬ + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Orders + .Include(x => x.ShipmentOrders) + .ThenInclude(x => x.Shipment) + .Include(x => x.RequestOrders) + .ThenInclude(x => x.Request) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new ComputerShopDatabase(); + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputerShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputerShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; } } } diff --git a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs index 6929916..02c6703 100644 --- a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs @@ -15,51 +15,65 @@ namespace ComputerShopDatabaseImplement.Implements //!!!ПОДОБИЕ textile public class RequestStorage : IRequestStorage { - //!!!ТУТ НАДО ПОЛУЧАТЬ СПИСОК ТОЛЬКО ПОЛЬЗОВАТЕЛЯ, КАК-ТО ДОБАВИТЬ (мб убрать этот метод) + //!!!КАК ПРИСОЕДИНИТЬ СБОРКУ И НАДО ЛИ? public List GetFullList() { using var context = new ComputerShopDatabase(); return context.Requests .Include(x => x.Orders) .ThenInclude(x => x.Order) + .Include(x => x.Assembly) .ToList() .Select(x => x.GetViewModel) .ToList(); } + + //!!!тут добавил .Include(x => x.Assembly) public List GetFilteredList(RequestSearchModel model) { using var context = new ComputerShopDatabase(); + if (model.DateFrom.HasValue && model.DateTo.HasValue) { return context.Requests - .Where(x => x.UserId == model.UserId) + .Where(x => x.UserId == model.UserId && x.DateRequest >= model.DateFrom && x.DateRequest <= model.DateTo) .Include(x => x.Orders) .ThenInclude(x => x.Order) - .Where(x => x.DateRequest >= model.DateFrom && x.DateRequest <= model.DateTo) + .Include(x => x.Assembly) .Select(x => x.GetViewModel) .ToList(); } + //Получение заявок по сборкам else if (model.AssemblyId.HasValue) { return context.Requests - .Where(x => x.UserId == model.UserId) + .Where(x => x.UserId == model.UserId && x.AssemblyId == model.AssemblyId) .Include(x => x.Orders) .ThenInclude(x => x.Order) - .Where(x => x.AssemblyId == model.AssemblyId) + .Include(x => x.Assembly) .Select(x => x.GetViewModel) .ToList(); } + else if (!string.IsNullOrEmpty(model.ClientFIO)) { return context.Requests - .Where(x => x.UserId == model.UserId) + .Where(x => x.UserId == model.UserId && x.ClientFIO == model.ClientFIO) .Include(x => x.Orders) .ThenInclude(x => x.Order) - .Where(x => x.ClientFIO == model.ClientFIO) + .Include (x => x.Assembly) .Select(x => x.GetViewModel) .ToList(); } - return new(); + + //возвращение просто всех заявок пользователя (замена GetFullList) + return context.Requests + .Where(x => x.UserId == model.UserId) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Include(x => x.Assembly) + .Select(x => x.GetViewModel) + .ToList(); } //!!!мб тут надо будет в FirstOrDefault добавить другие параметры @@ -74,8 +88,7 @@ namespace ComputerShopDatabaseImplement.Implements .Where(x => x.UserId == model.UserId) .Include(x => x.Orders) .ThenInclude(x => x.Order) - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) - || (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO)) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } @@ -93,11 +106,7 @@ namespace ComputerShopDatabaseImplement.Implements //!!!мб тут вернуть другое значение //!!!МБ ТУТ НЕЛЬЗЯ WHERE первым //!!!мб тут и не надо where по пользователю (попробовать потом без него) - return context.Requests - .Where(x => x.UserId == model.UserId) - .Include(x => x.Orders) - .ThenInclude(x => x.Order) - .FirstOrDefault(x => x.Id == newRequest.Id)?.GetViewModel; + return newRequest.GetViewModel; } public RequestViewModel? Update(RequestBindingModel model) @@ -115,11 +124,7 @@ namespace ComputerShopDatabaseImplement.Implements context.SaveChanges(); request.UpdateOrders(context, model); transaction.Commit(); - return context.Requests - .Include(x => x.Orders) - .ThenInclude(x => x.Order) - .FirstOrDefault(x => x.Id == model.Id)? - .GetViewModel; + return request.GetViewModel; } catch { diff --git a/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs b/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs index ce61c14..30b7294 100644 --- a/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/ShipmentStorage.cs @@ -33,9 +33,18 @@ namespace ComputerShopDatabaseImplement.Implements public List GetFilteredList(ShipmentSearchModel model) { using var context = new ComputerShopDatabase(); + if (model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && string.IsNullOrEmpty(model.ProviderName)) + { + return context.Shipments + .Where(x => x.UserId == model.Id) + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Select(x => x.GetViewModel) + .ToList(); + } //!!!мб добавить if (model.Id.HasValue) //!!!МБ WHERE НЕЛЬЗЯ СТАВИТЬ ПЕРВЫМ - if (model.DateFrom.HasValue && model.DateTo.HasValue) + else if (model.DateFrom.HasValue && model.DateTo.HasValue) { return context.Shipments .Where(x => x.UserId == model.UserId) diff --git a/ComputerShopDatabaseImplement/Implements/UserStorage.cs b/ComputerShopDatabaseImplement/Implements/UserStorage.cs index eeabf9a..137796d 100644 --- a/ComputerShopDatabaseImplement/Implements/UserStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/UserStorage.cs @@ -3,6 +3,7 @@ using ComputerShopContracts.SearchModels; using ComputerShopContracts.StorageContracts; using ComputerShopContracts.ViewModels; using ComputerShopDatabaseImplement.Models; +using ComputerShopDataModels.Enums; using System; using System.Collections.Generic; using System.Linq; @@ -14,14 +15,16 @@ namespace ComputerShopDatabaseImplement.Implements public class UserStorage : IUserStorage { //!!!ТУТ МБ РАЗДЕЛИТЬ НА 2 МЕТОДА: исполнителя и поручителя и добавить where - public List GetFullList() + public List GetFullList(UserRole role) { using var context = new ComputerShopDatabase(); - return context.Users.Select(x => x.GetViewModel).ToList(); + return context.Users.Where(x => x.Role == role).Select(x => x.GetViewModel).ToList(); } //!!!ТУТ МБ РАЗДЕЛИТЬ НА 2 МЕТОДА: исполнителя и поручителя и добавить where //!!!ДОПИСАТЬ + //!!!мб не надо, т.к. есть getelement для получения 1 пользователя и получение всех пользователей по роли. + //при фильтрации всегда (?) будет либо 1, либо все пользователи роли public List GetFilteredList(UserSearchModel model) { if (string.IsNullOrEmpty(model.Login)) @@ -29,13 +32,24 @@ namespace ComputerShopDatabaseImplement.Implements return new(); } using var context = new ComputerShopDatabase(); - + if (!string.IsNullOrEmpty(model.Login)) + { + + } } - //!!!ДОПИСАТЬ + //!!!ПРОВЕРИТЬ public UserViewModel? GetElement(UserSearchModel model) { - + if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Users.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) + || (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) + || (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)) + ?.GetViewModel; } public UserViewModel? Insert(UserBindingModel model) diff --git a/ComputerShopDatabaseImplement/Models/Order.cs b/ComputerShopDatabaseImplement/Models/Order.cs index 890407b..f214007 100644 --- a/ComputerShopDatabaseImplement/Models/Order.cs +++ b/ComputerShopDatabaseImplement/Models/Order.cs @@ -73,7 +73,6 @@ namespace ComputerShopDatabaseImplement.Models { Id = Id, UserId = UserId, - UserLogin = User.Login, DateCreate = DateCreate, Status = Status, Sum = Sum diff --git a/ComputerShopDatabaseImplement/Models/Request.cs b/ComputerShopDatabaseImplement/Models/Request.cs index 9868507..e128e32 100644 --- a/ComputerShopDatabaseImplement/Models/Request.cs +++ b/ComputerShopDatabaseImplement/Models/Request.cs @@ -23,6 +23,9 @@ namespace ComputerShopDatabaseImplement.Models public int? AssemblyId { get; set; } + //!!!МБ ТУТ НЕ НАДО, НО НА ПОДОБИЕ С ЗАКАЗОМ И СВЯЗЬЮ С implmenter В ЛАБАХ НАДО + public virtual Assembly? Assembly { get; set; } = new(); + [Required] public DateTime DateRequest { get; set; } @@ -52,12 +55,15 @@ namespace ComputerShopDatabaseImplement.Models [ForeignKey("RequestId")] public virtual List Orders { get; set; } = new(); + public static Request Create(ComputerShopDatabase context, RequestBindingModel model) { return new Request() { Id = model.Id, UserId = model.UserId, + User = context.Users.First(x => x.Id == model.UserId), AssemblyId = model.AssemblyId, + Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : null, DateRequest = model.DateRequest, ClientFIO = model.ClientFIO, Orders = model.RequestOrders.Select(x => new RequestOrder @@ -69,23 +75,28 @@ namespace ComputerShopDatabaseImplement.Models //!!!МБ ТУТ ЕЩЁ ЧТО-ТО ОБНОВИТЬ public void Update(RequestBindingModel model) { + if (model == null) { + return; + } + using var context = new ComputerShopDatabase(); DateRequest = model.DateRequest; + AssemblyId = model.AssemblyId.HasValue ? model.AssemblyId : AssemblyId; + Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : Assembly; ClientFIO = model.ClientFIO; + context.SaveChanges(); } //!!!МБ ТУТ НЕ ВСЁ НАДО ПРИСВАИВАТЬ public RequestViewModel GetViewModel => new() { Id = Id, UserId = UserId, - UserLogin = User.Login, AssemblyId = AssemblyId, DateRequest = DateRequest, ClientFIO = ClientFIO }; - //!!!ДОПИСАТЬ UpdateOrders + //!!!МБ ЭТОТ МЕТОД БУДЕТ НЕ НУЖЕН, ПОТОМУ ЧТО ПО ЗАДАНИЮ НЕ НАПИСАНО, ЧТО МОЖНО ОБНОВЛЯТЬ - //!!!МБ НАЗВАТЬ КАК-ТО ПО-ДРУГОМУ (мб метод вынести в Implement) //!!!МБ ПЕРЕДАВАТЬ ЧТО-ТО ДРУГОЕ //!!!ПРОВЕРИТЬ, ЧТО ВСЁ ПРАВИЛЬНО ИЗВЛЕКАЮ //!!!делаю как в Textile diff --git a/ComputerShopDatabaseImplement/Models/Shipment.cs b/ComputerShopDatabaseImplement/Models/Shipment.cs index 7f48b95..6d14b21 100644 --- a/ComputerShopDatabaseImplement/Models/Shipment.cs +++ b/ComputerShopDatabaseImplement/Models/Shipment.cs @@ -77,7 +77,6 @@ namespace ComputerShopDatabaseImplement.Models { Id = Id, UserId = UserId, - UserLogin = User.Login, ProviderName = ProviderName, DateShipment = DateShipment, ShipmentOrders = ShipmentOrders