From 92a8fefd662395e66b446d5f7b8790bd4480505f Mon Sep 17 00:00:00 2001 From: Safgerd Date: Fri, 19 May 2023 16:11:31 +0400 Subject: [PATCH] Contracktor: Fixes --- .../BusinessLogic/OrderLogic.cs | 46 ++++- .../BindingModels/RequestBindingModel.cs | 4 +- .../BusinessLogicContracts/IOrderLogic.cs | 6 +- .../IRequestComponentLogic.cs | 2 + .../ISellerReportLogic.cs | 12 -- .../ComputerStoreContracts.csproj | 4 + .../SearchModels/SellerSearchModel.cs | 1 + .../ViewModels/RequestViewModel.cs | 4 +- .../Models/IRequestModel.cs | 4 +- .../Implements/RequestStorage.cs | 36 +++- .../Models/Order.cs | 2 +- .../Models/Request.cs | 10 +- .../ComputerStoreRestAPI.csproj | 6 + .../Controllers/MainController.cs | 161 +++++++++++++++++- .../Controllers/SellerController.cs | 63 +++++++ ComputerStoreRestAPI/Program.cs | 7 + 16 files changed, 339 insertions(+), 29 deletions(-) delete mode 100644 ComputerStoreContracts/BusinessLogicContracts/ISellerReportLogic.cs create mode 100644 ComputerStoreRestAPI/Controllers/SellerController.cs diff --git a/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs b/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs index 87d6240..f771627 100644 --- a/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ComputerStoreBusinessLogic/BusinessLogic/OrderLogic.cs @@ -42,6 +42,29 @@ namespace ComputerStoreBusinessLogic.BusinessLogic return true; } + public bool DeleteOrder (OrderBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. ID:{ID}", model.ID); + if (_orderStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool UpdateOrder (OrderBindingModel model) + { + CheckModel(model); + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool StatusUpdate(OrderBindingModel rawModel, OrderStatus newStatus) { var viewModel = _orderStorage.GetElement(new OrderSearchModel @@ -99,7 +122,28 @@ namespace ComputerStoreBusinessLogic.BusinessLogic return StatusUpdate(model, OrderStatus.Given); } - public List? ReadList(OrderSearchModel? model) + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Order ID:{ OrderID}", model.ID); + + var element = _orderStorage.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(OrderSearchModel? model) { _logger.LogInformation("Order. OrderID:{Id}", model?.ID); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); diff --git a/ComputerStoreContracts/BindingModels/RequestBindingModel.cs b/ComputerStoreContracts/BindingModels/RequestBindingModel.cs index 48e243e..e1d8d53 100644 --- a/ComputerStoreContracts/BindingModels/RequestBindingModel.cs +++ b/ComputerStoreContracts/BindingModels/RequestBindingModel.cs @@ -13,5 +13,7 @@ namespace ComputerStoreContracts.BindingModels public int OrderID { get; set; } public double Price { get; set; } public int? PCID { get; set; } - } + + public List<(IComponentModel, int)> RequestComponents { get; set; } = new(); + } } diff --git a/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs index 6334f53..0150912 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IOrderLogic.cs @@ -16,5 +16,9 @@ namespace ComputerStoreContracts.BusinessLogicContracts bool TakeOrderInProcess(OrderBindingModel model); bool TakeOrderReady(OrderBindingModel model); bool TakeOrderGiven(OrderBindingModel model); - } + + OrderViewModel? ReadElement(OrderSearchModel model); + bool UpdateOrder(OrderBindingModel model); + bool DeleteOrder(OrderBindingModel model); + } } diff --git a/ComputerStoreContracts/BusinessLogicContracts/IRequestComponentLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/IRequestComponentLogic.cs index e5d4166..87ebe40 100644 --- a/ComputerStoreContracts/BusinessLogicContracts/IRequestComponentLogic.cs +++ b/ComputerStoreContracts/BusinessLogicContracts/IRequestComponentLogic.cs @@ -11,5 +11,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts public interface IRequestComponentLogic { List? ReadList(RequestSearchModel? model); + + } } diff --git a/ComputerStoreContracts/BusinessLogicContracts/ISellerReportLogic.cs b/ComputerStoreContracts/BusinessLogicContracts/ISellerReportLogic.cs deleted file mode 100644 index 3354c36..0000000 --- a/ComputerStoreContracts/BusinessLogicContracts/ISellerReportLogic.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ComputerStoreContracts.BusinessLogicContracts -{ - internal class ISellerReportLogic - { - } -} diff --git a/ComputerStoreContracts/ComputerStoreContracts.csproj b/ComputerStoreContracts/ComputerStoreContracts.csproj index 0eeba33..7899907 100644 --- a/ComputerStoreContracts/ComputerStoreContracts.csproj +++ b/ComputerStoreContracts/ComputerStoreContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/ComputerStoreContracts/SearchModels/SellerSearchModel.cs b/ComputerStoreContracts/SearchModels/SellerSearchModel.cs index 65400c8..214d236 100644 --- a/ComputerStoreContracts/SearchModels/SellerSearchModel.cs +++ b/ComputerStoreContracts/SearchModels/SellerSearchModel.cs @@ -10,5 +10,6 @@ namespace ComputerStoreContracts.SearchModels { public int? ID { get; set; } public string? Username { get; set; } + public string? Password { get; set; } } } diff --git a/ComputerStoreContracts/ViewModels/RequestViewModel.cs b/ComputerStoreContracts/ViewModels/RequestViewModel.cs index c339340..1e52e88 100644 --- a/ComputerStoreContracts/ViewModels/RequestViewModel.cs +++ b/ComputerStoreContracts/ViewModels/RequestViewModel.cs @@ -22,5 +22,7 @@ namespace ComputerStoreContracts.ViewModels [DisplayName("PC's name")] public string? PCName { get; set; } = string.Empty; - } + + public List<(IComponentModel, int)> RequestComponents { get; set; } = new(); + } } diff --git a/ComputerStoreDataModels/Models/IRequestModel.cs b/ComputerStoreDataModels/Models/IRequestModel.cs index c21a563..6f12f57 100644 --- a/ComputerStoreDataModels/Models/IRequestModel.cs +++ b/ComputerStoreDataModels/Models/IRequestModel.cs @@ -11,5 +11,7 @@ namespace ComputerStoreDataModels.Models int OrderID { get; } int? PCID { get; } double Price { get; } - } + + public List<(IComponentModel, int)> RequestComponents { get; } + } } diff --git a/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs b/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs index 13b4384..711dbb0 100644 --- a/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs +++ b/ComputerStoreDatabaseImplement/Implements/RequestStorage.cs @@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -55,13 +56,24 @@ namespace ComputerStoreDatabaseImplement.Implements using var context = new ComputerStoreDatabase(); var newRequest = Request.Create(context, model); if (newRequest == null) { return null; } + context.Requests.Add(newRequest); + context.SaveChanges(); + + var requestList = GetFullList(); + var currentRequest = requestList.Last(); + + if (currentRequest == null) { return null; } + foreach (var reqcomp in newRequest.RequestComponents) + { + context.RequestComponents.Add(new RequestComponent { ComponentID = reqcomp.Item1.ID, RequestID = currentRequest.ID, Count = reqcomp.Item2 }); + } + context.SaveChanges(); - return context.Requests - .Include(x => x.Order) - .Include(y => y.PCs) - .FirstOrDefault(x => x.ID == model.ID)?.GetViewModel; - } + + return newRequest.GetViewModel; + + } public RequestViewModel? Update(RequestBindingModel model) { @@ -71,8 +83,19 @@ namespace ComputerStoreDatabaseImplement.Implements { return null; } + if (request.PCID != null) + { + return null; + } - request.Update(model); + context.RequestComponents.RemoveRange(context.RequestComponents.Where(x => x.RequestID == request.ID)); + + foreach (var reqcomp in request.RequestComponents) + { + context.RequestComponents.Add(new RequestComponent { ComponentID = reqcomp.Item1.ID, RequestID = request.ID, Count = reqcomp.Item2 }); + } + + request.Update(model); context.SaveChanges(); return context.Requests @@ -92,6 +115,7 @@ namespace ComputerStoreDatabaseImplement.Implements if (request != null) { + context.RequestComponents.RemoveRange(context.RequestComponents.Where(x => x.RequestID == request.ID)); context.Requests.Remove(request); context.SaveChanges(); diff --git a/ComputerStoreDatabaseImplement/Models/Order.cs b/ComputerStoreDatabaseImplement/Models/Order.cs index d605ef5..19d49da 100644 --- a/ComputerStoreDatabaseImplement/Models/Order.cs +++ b/ComputerStoreDatabaseImplement/Models/Order.cs @@ -64,7 +64,7 @@ namespace ComputerStoreDatabaseImplement.Models DateImplement = model.DateImplement, _consignments = model.OrderConsignments.Select(x => context.Consignments.First(y => y.ID == x.ID)).ToList(), _requests = model.OrderRequests.Select(x => context.Requests.First(y => y.ID == x.ID)).ToList(), - }; + }; } public void Update(OrderBindingModel? model) diff --git a/ComputerStoreDatabaseImplement/Models/Request.cs b/ComputerStoreDatabaseImplement/Models/Request.cs index 8a14bb2..18bb3ad 100644 --- a/ComputerStoreDatabaseImplement/Models/Request.cs +++ b/ComputerStoreDatabaseImplement/Models/Request.cs @@ -34,17 +34,22 @@ namespace ComputerStoreDatabaseImplement.Models [ForeignKey("RequestID")] public virtual List PersonalComputers { get; set; } = new(); + [NotMapped] + public List<(IComponentModel, int)> RequestComponents { get; set; } = new(); + public static Request? Create(ComputerStoreDatabase context, RequestBindingModel model) { if (model == null) { return null; } + return new Request() { ID = model.ID, OrderID = model.OrderID, - Price = model.Price + Price = model.Price, + RequestComponents = model.RequestComponents, }; } @@ -56,6 +61,7 @@ namespace ComputerStoreDatabaseImplement.Models } Price = model.Price; PCID = model.PCID; + RequestComponents = model.RequestComponents; } public void UpdatePCID(ComputerStoreDatabase context) @@ -74,7 +80,7 @@ namespace ComputerStoreDatabaseImplement.Models OrderID = OrderID, Price = Price, PCID = PCID, - PCName = context.PCs.FirstOrDefault(rec => rec.ID == PCID)?.Name + PCName = context.PCS.FirstOrDefault(rec => rec.ID == PCID)?.Name }; } } diff --git a/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj b/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj index c6f0236..6dc7a2b 100644 --- a/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj +++ b/ComputerStoreRestAPI/ComputerStoreRestAPI.csproj @@ -7,6 +7,12 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/ComputerStoreRestAPI/Controllers/MainController.cs b/ComputerStoreRestAPI/Controllers/MainController.cs index a31701e..948c9b4 100644 --- a/ComputerStoreRestAPI/Controllers/MainController.cs +++ b/ComputerStoreRestAPI/Controllers/MainController.cs @@ -18,7 +18,22 @@ namespace ComputerStoreRestAPI.Controllers private readonly IEmployeeReportLogic _employeeReportLogic; private readonly IRequestLogic _requestLogic; - public MainController(ILogger logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic, IRequestComponentLogic requestComponentLogic, IRequestLogic requestLogic) + private readonly IOrderLogic _orderLogic; + private readonly IConsignmentLogic _consignmentLogic; + private readonly IReportOrderLogic _reportOrderLogic; + + + public MainController( + ILogger logger, + IComponentLogic componentLogic, + IPCLogic pcLogic, + IProductLogic productLogic, + IEmployeeReportLogic employeeReportLogic, + IRequestComponentLogic requestComponentLogic, + IRequestLogic requestLogic, + IOrderLogic orderLogic, + IConsignmentLogic consignmentLogic, + IReportOrderLogic reportOrderLogic) { _logger = logger; _componentLogic = componentLogic; @@ -27,6 +42,9 @@ namespace ComputerStoreRestAPI.Controllers _employeeReportLogic = employeeReportLogic; _requestComponentLogic = requestComponentLogic; _requestLogic = requestLogic; + _orderLogic = orderLogic; + _consignmentLogic = consignmentLogic; + _reportOrderLogic = reportOrderLogic; } [HttpGet] @@ -225,7 +243,13 @@ namespace ComputerStoreRestAPI.Controllers } } - [HttpGet] + + + + + + #region Request + [HttpGet] public List? GetRequestList() { try @@ -238,5 +262,136 @@ namespace ComputerStoreRestAPI.Controllers throw; } } - } + + [HttpGet] + public RequestViewModel? GetRequest(int id) + { + try + { + return _requestLogic.ReadElement(new RequestSearchModel { ID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Receiving request error."); + throw; + } + } + + [HttpDelete("{id}")] + public bool DeleteRequest(int id) + { + try + { + return _requestLogic.Delete(new RequestBindingModel { ID = id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Removing request error."); + throw; + } + } + + [HttpPatch] + public bool UpdateRequest(RequestBindingModel request) + { + try + { + return _requestLogic.Update(request); + } + catch (Exception ex) + { + _logger.LogError(ex, "Updating request error."); + throw; + } + } + + [HttpPost] + public bool InsertRequest(RequestBindingModel request) + { + try + { + return _requestLogic.Create(request); + } + catch (Exception ex) + { + _logger.LogError(ex, "Inserting request error."); + throw; + } + } + #endregion + + + + #region Order + [HttpGet] + public List? GetOrdersList() + { + try + { + return _orderLogic.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Receiving list of orders error."); + throw; + } + } + + [HttpPatch] + public bool UpdateOrderInProcess(OrderBindingModel order) + { + try + { + return _orderLogic.TakeOrderInProcess(order); + } + catch (Exception ex) + { + _logger.LogError(ex, "Updating order in process error."); + throw; + } + } + + [HttpPatch] + public bool UpdateOrderReady(OrderBindingModel order) + { + try + { + return _orderLogic.TakeOrderReady(order); + } + catch (Exception ex) + { + _logger.LogError(ex, "Updating order ready error."); + throw; + } + } + + [HttpPatch] + public bool UpdateOrderGiven(OrderBindingModel order) + { + try + { + return _orderLogic.TakeOrderGiven(order); + } + catch (Exception ex) + { + _logger.LogError(ex, "Updating order given error."); + throw; + } + } + + [HttpPost] + public bool InsertOrder(OrderBindingModel order) + { + try + { + return _orderLogic.CreateOrder(order); + } + catch (Exception ex) + { + _logger.LogError(ex, "Inserting order error."); + throw; + } + } + #endregion + } } diff --git a/ComputerStoreRestAPI/Controllers/SellerController.cs b/ComputerStoreRestAPI/Controllers/SellerController.cs new file mode 100644 index 0000000..d5665dd --- /dev/null +++ b/ComputerStoreRestAPI/Controllers/SellerController.cs @@ -0,0 +1,63 @@ +using ComputerStoreContracts.BindingModels; +using ComputerStoreContracts.BusinessLogicContracts; +using ComputerStoreContracts.SearchModels; +using ComputerStoreContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace ComputerStoreRestAPI.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class SellerController : Controller + { + private readonly ILogger _logger; + private readonly ISellerLogic _logic; + + public SellerController(ISellerLogic logic, ILogger logger) + { + _logger = logger; + _logic = logic; + } + + [HttpGet] + public SellerViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new SellerSearchModel { Username = login, Password = password }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Seller logging in error"); throw; + } + } + + [HttpPost] + public bool Register(SellerBindingModel seller) + { + try + { + return _logic.Create(seller); + } + catch (Exception ex) + { + _logger.LogError(ex, "Seller registration error "); + throw; + } + } + + [HttpPatch] + public bool UpdateData(SellerBindingModel seller) + { + try + { + return _logic.Update(seller); + } + catch (Exception ex) + { + _logger.LogError(ex, "Update of data error "); + throw; + } + } + } +} diff --git a/ComputerStoreRestAPI/Program.cs b/ComputerStoreRestAPI/Program.cs index 5e1826a..0021f51 100644 --- a/ComputerStoreRestAPI/Program.cs +++ b/ComputerStoreRestAPI/Program.cs @@ -20,6 +20,13 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient();