diff --git a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs index f9ce33c..9cc1298 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -27,7 +27,8 @@ namespace ComputerShopBusinessLogic.BusinessLogics public List? ReadList(OrderSearchModel? model) { - var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + //model.UserId = -1 для swagger, чтобы можно было считать все заказы всех пользователей + var list = (model == null || model.UserId == -1) ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); @@ -73,11 +74,6 @@ namespace ComputerShopBusinessLogic.BusinessLogics 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) { @@ -102,14 +98,6 @@ namespace ComputerShopBusinessLogic.BusinessLogics _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; } diff --git a/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs index 64d2f4d..6b6f73b 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -24,7 +25,8 @@ namespace ComputerShopBusinessLogic.BusinessLogics public List? ReadList(RequestSearchModel? model) { - var list = model == null ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model); + //model.UserId = -1 для swagger, чтобы можно было считать все заявки всех пользователей + var list = (model == null || model.UserId == -1) ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList requests return null list"); @@ -73,6 +75,13 @@ namespace ComputerShopBusinessLogic.BusinessLogics return true; } + //!!!ПРОВЕРИТЬ + public bool ConnectRequestAssembly(int requestId, int assemblyId) + { + _logger.LogInformation("Connect Assembly {rId} with request {aId}", requestId, assemblyId); + return _requestStorage.ConnectRequestAssembly(requestId, assemblyId); + } + public bool Delete(RequestBindingModel model) { CheckModel(model, false); diff --git a/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs index 6cb19b9..d720d37 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/ShipmentLogic.cs @@ -28,8 +28,8 @@ namespace ComputerShopBusinessLogic.BusinessLogics //!!!ИЛИ СОЗДАТЬ ОТДЕЛЬНЫЙ МЕТОД ReadListUser public List ReadList(ShipmentSearchModel? model) { - _logger.LogInformation("ReadList. ShipmentId:{Id}", model?.Id); - var list = model == null ? _shipmentStorage.GetFullList() : _shipmentStorage.GetFilteredList(model); + //model.UserId = -1 для swagger, чтобы можно было считать все партии товаров всех пользователей + var list = (model == null || model.UserId == -1) ? _shipmentStorage.GetFullList() : _shipmentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); diff --git a/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs index d60bd5b..8253498 100644 --- a/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs +++ b/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs @@ -17,5 +17,6 @@ namespace ComputerShopContracts.BusinessLogicContracts bool Create(RequestBindingModel model); bool Update(RequestBindingModel model); bool Delete(RequestBindingModel model); + bool ConnectRequestAssembly(int requestId, int assemblyId); } } diff --git a/ComputerShopContracts/SearchModels/UserSearchModel.cs b/ComputerShopContracts/SearchModels/UserSearchModel.cs index 7737112..1eee48d 100644 --- a/ComputerShopContracts/SearchModels/UserSearchModel.cs +++ b/ComputerShopContracts/SearchModels/UserSearchModel.cs @@ -12,7 +12,7 @@ namespace ComputerShopContracts.SearchModels public int? Id { get; set; } public string? Login { get; set; } - //!!!ПОИСК ПО ПАРОЛЮ НЕ СТАЛ ДОБАВЛЯТЬ, ИБО СТРАННО + public string Password { get; set; } public string? Email { get; set; } public UserRole? Role { get; set; } diff --git a/ComputerShopContracts/StorageContracts/IRequestStorage.cs b/ComputerShopContracts/StorageContracts/IRequestStorage.cs index 33094f2..a7e3e7d 100644 --- a/ComputerShopContracts/StorageContracts/IRequestStorage.cs +++ b/ComputerShopContracts/StorageContracts/IRequestStorage.cs @@ -17,5 +17,6 @@ namespace ComputerShopContracts.StorageContracts RequestViewModel? Insert(RequestBindingModel model); RequestViewModel? Update(RequestBindingModel model); RequestViewModel? Delete(RequestBindingModel model); + bool ConnectRequestAssembly(int requestId, int assemblyId); } } diff --git a/ComputerShopContracts/ViewModels/RequestViewModel.cs b/ComputerShopContracts/ViewModels/RequestViewModel.cs index 3397928..47b9c41 100644 --- a/ComputerShopContracts/ViewModels/RequestViewModel.cs +++ b/ComputerShopContracts/ViewModels/RequestViewModel.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -23,7 +24,7 @@ namespace ComputerShopContracts.ViewModels public int? AssemblyId { get; set; } - //!!!МБ НАДО БУДЕТ ПРИВЯЗЫВАТЬ НАЗВАНИЕ СБОРКИ И Т.Д. ДЛЯ ОТЧЁТОВ + public IAssemblyModel? Assembly { get; set; } public Dictionary RequestOrders { get; set; } = new(); diff --git a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs index ce75775..c3a6a16 100644 --- a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs @@ -148,5 +148,20 @@ namespace ComputerShopDatabaseImplement.Implements } return null; } + + //!!!ПРОВЕРИТЬ + public bool ConnectRequestAssembly(int requestId, int assemblyId) + { + using var context = new ComputerShopDatabase(); + var request = context.Requests.FirstOrDefault(x => x.Id == requestId); + var assembly = context.Assemblies.FirstOrDefault(x => x.Id == assemblyId); + if (request == null || assembly == null) + { + return false; + } + request.ConnectAssembly(context, assemblyId); + context.SaveChanges(); + return true; + } } } diff --git a/ComputerShopDatabaseImplement/Implements/UserStorage.cs b/ComputerShopDatabaseImplement/Implements/UserStorage.cs index a7da400..a93fb2d 100644 --- a/ComputerShopDatabaseImplement/Implements/UserStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/UserStorage.cs @@ -36,11 +36,19 @@ namespace ComputerShopDatabaseImplement.Implements //id, почта и логин уникальны, можно получать по ним public UserViewModel? GetElement(UserSearchModel model) { + //!!!МБ ЭТУ ПРОВЕРКУ УБРАТЬ if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) { return null; } using var context = new ComputerShopDatabase(); + + //Поиск пользователя при входе в систему по логину, паролю и его роли (чтобы не могли войти в аккаунты другой роли) + if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && model.Role.HasValue) + { + return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password && x.Role == model.Role)?.GetViewModel; + } + //!!!НИЖЕ МБ НЕ НАДО //Получение по логину (пользователей с таким логином будет 1 или 0) if (!string.IsNullOrEmpty(model.Login)) { diff --git a/ComputerShopDatabaseImplement/Models/Request.cs b/ComputerShopDatabaseImplement/Models/Request.cs index bcb925c..3dfa547 100644 --- a/ComputerShopDatabaseImplement/Models/Request.cs +++ b/ComputerShopDatabaseImplement/Models/Request.cs @@ -77,21 +77,32 @@ namespace ComputerShopDatabaseImplement.Models if (model == null) { return; } + if (model.DateRequest != null) + { + DateRequest = model.DateRequest; + } 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; + //if (model.AssemblyId.HasValue && model.AssemblyId > 0) + //{ + // AssemblyId = model.AssemblyId; + // Assembly = context.Assemblies.FirstOrDefault(x => x.Id == model.AssemblyId); + //} + if (!string.IsNullOrEmpty(model.ClientFIO)) + { + ClientFIO = model.ClientFIO; + } context.SaveChanges(); } - //!!!МБ ТУТ НЕ ВСЁ НАДО ПРИСВАИВАТЬ + //!!!МБ ТУТ НАДО ПРИСВАИВАТЬ ЗНАЧЕНИЕ Assembly public RequestViewModel GetViewModel => new() { Id = Id, UserId = UserId, AssemblyId = AssemblyId, + Assembly = Assembly, DateRequest = DateRequest, - ClientFIO = ClientFIO + ClientFIO = ClientFIO, + RequestOrders = RequestOrders }; @@ -122,5 +133,12 @@ namespace ComputerShopDatabaseImplement.Models } _requestOrders = null; } + + //!!!ПРОВЕРИТЬ + public void ConnectAssembly(ComputerShopDatabase context, int assemblyId) + { + AssemblyId = assemblyId; + Assembly = context.Assemblies.First(x => x.Id == assemblyId); + } } } diff --git a/ComputerShopDatabaseImplement/Models/Shipment.cs b/ComputerShopDatabaseImplement/Models/Shipment.cs index 6d14b21..fa429ca 100644 --- a/ComputerShopDatabaseImplement/Models/Shipment.cs +++ b/ComputerShopDatabaseImplement/Models/Shipment.cs @@ -34,7 +34,7 @@ namespace ComputerShopDatabaseImplement.Models private Dictionary? _shipmentOrders = null; [NotMapped] - public Dictionary? ShipmentOrders + public Dictionary ShipmentOrders { get { @@ -58,6 +58,7 @@ namespace ComputerShopDatabaseImplement.Models { Id = model.Id, UserId = model.UserId, + User = context.Users.First(x => x.Id == model.UserId), ProviderName = model.ProviderName, DateShipment = model.DateShipment, Orders = model.ShipmentOrders.Select(x => new ShipmentOrder @@ -69,8 +70,18 @@ namespace ComputerShopDatabaseImplement.Models //!!!МБ ТУТ КАКИЕ-ТО ДРУГИЕ ПОЛЯ НАДО БУДЕТ ОБНОВЛЯТЬ public void Update(ShipmentBindingModel model) { - ProviderName = model.ProviderName; - DateShipment = model.DateShipment; + if (model == null) + { + return; + } + if (!string.IsNullOrEmpty(model.ProviderName)) + { + ProviderName = model.ProviderName; + } + if (model.DateShipment != null) + { + DateShipment = model.DateShipment; + } } //!!!МБ ТУТ ЕЩЁ ЧТО-ТО ПРИСВАИВАТЬ public ShipmentViewModel GetViewModel => new() diff --git a/ComputerShopRestApi/Controllers/OrderController.cs b/ComputerShopRestApi/Controllers/OrderController.cs index ba9cbb5..1746a63 100644 --- a/ComputerShopRestApi/Controllers/OrderController.cs +++ b/ComputerShopRestApi/Controllers/OrderController.cs @@ -2,6 +2,7 @@ using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; using Microsoft.AspNetCore.Mvc; namespace ComputerShopRestApi.Controllers @@ -36,6 +37,26 @@ namespace ComputerShopRestApi.Controllers throw; } } + + /// + /// Получение заказов по id пользователя (полный список, кот. будет выводиться) + /// + /// + /// + [HttpGet] + public List? GetOrders(int? userId) + { + try + { + return _logic.ReadList(new OrderSearchModel { UserId = userId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", userId); + throw; + } + } + [HttpPost] public void CreateOrder(OrderBindingModel model) { diff --git a/ComputerShopRestApi/Controllers/RequestController.cs b/ComputerShopRestApi/Controllers/RequestController.cs index 87d4e41..33f2528 100644 --- a/ComputerShopRestApi/Controllers/RequestController.cs +++ b/ComputerShopRestApi/Controllers/RequestController.cs @@ -19,6 +19,8 @@ namespace ComputerShopRestApi.Controllers _logger = logger; _logic = logic; } + + [HttpGet] public RequestViewModel? GetRequest(int id) { @@ -35,6 +37,27 @@ namespace ComputerShopRestApi.Controllers throw; } } + + /// + /// Получение заявок по id пользователя (полный список, кот. будет выводиться) + /// + /// + /// + [HttpGet] + public List? GetRequests(int userId) + { + try + { + return _logic.ReadList(new RequestSearchModel { UserId = userId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", userId); + throw; + } + } + + [HttpPost] public void CreateRequest(RequestBindingModel model) { @@ -48,6 +71,23 @@ namespace ComputerShopRestApi.Controllers throw; } } + + //!!!ПРОВЕРИТЬ + [HttpPost] + public void ConnectRequestAssembly(int requestId, int assemblyId) + { + try + { + _logic.ConnectRequestAssembly(requestId, assemblyId); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка связывания заявки со сборкой"); + throw; + } + } + + [HttpPost] public void UpdateRequest(RequestBindingModel model) { @@ -61,7 +101,10 @@ namespace ComputerShopRestApi.Controllers throw; } } - //!!!мб HttpPut + + + + [HttpDelete] public void DeleteRequest(RequestBindingModel model) { diff --git a/ComputerShopRestApi/Controllers/ShipmentController.cs b/ComputerShopRestApi/Controllers/ShipmentController.cs index 9ba2a89..2e4cc9f 100644 --- a/ComputerShopRestApi/Controllers/ShipmentController.cs +++ b/ComputerShopRestApi/Controllers/ShipmentController.cs @@ -21,6 +21,7 @@ namespace ComputerShopRestApi.Controllers _logic = logic; } + [HttpGet] public ShipmentViewModel? GetShipment(int id) { @@ -37,6 +38,27 @@ namespace ComputerShopRestApi.Controllers throw; } } + + /// + /// Получение партий по id пользователя (полный список, кот. будет выводиться) + /// + /// + /// + [HttpGet] + public List? GetShipments(int userId) + { + try + { + return _logic.ReadList(new ShipmentSearchModel { UserId = userId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", userId); + throw; + } + } + + [HttpPost] public void CreateShipment(ShipmentBindingModel model) { @@ -50,6 +72,7 @@ namespace ComputerShopRestApi.Controllers throw; } } + [HttpPost] public void UpdateShipment(ShipmentBindingModel model) { @@ -63,7 +86,7 @@ namespace ComputerShopRestApi.Controllers throw; } } - //!!!мб HttpPut + [HttpDelete] public void DeleteShipment(ShipmentBindingModel model) { diff --git a/ComputerShopRestApi/Controllers/UserController.cs b/ComputerShopRestApi/Controllers/UserController.cs index 9319d6d..8f4c384 100644 --- a/ComputerShopRestApi/Controllers/UserController.cs +++ b/ComputerShopRestApi/Controllers/UserController.cs @@ -2,6 +2,7 @@ using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; using Microsoft.AspNetCore.Mvc; namespace ComputerShopRestApi.Controllers @@ -18,17 +19,18 @@ namespace ComputerShopRestApi.Controllers _logic = logic; _logger = logger; } - //!!!мб тут нужен пароль + [HttpGet] - public UserViewModel? Login(string login, string password) + public UserViewModel? LoginImplementer(string login, string password) { try { return _logic.ReadElement(new UserSearchModel { Login = login, - Email = login + Password = password, + Role = UserRole.Исполнитель }); } catch (Exception ex) @@ -38,11 +40,47 @@ namespace ComputerShopRestApi.Controllers } } - [HttpPost] - public void Register(UserBindingModel model) + [HttpGet] + public UserViewModel? LoginGuarantor(string login, string password) { try { + return _logic.ReadElement(new UserSearchModel + { + Login = login, + Password = password, + Role = UserRole.Поручитель + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + + //!!!мб не тут надо присваивать роль + [HttpPost] + public void RegisterImplementer(UserBindingModel model) + { + try + { + model.Role = UserRole.Исполнитель; + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + + [HttpPost] + public void RegisterGuarantor(UserBindingModel model) + { + try + { + model.Role = UserRole.Поручитель; _logic.Create(model); } catch (Exception ex)