From 381b8c6ec4e06d4bcec509303387ad99d351ee8c Mon Sep 17 00:00:00 2001 From: antoc0der <1@DESKTOP-K1L8ND3> Date: Mon, 22 Apr 2024 20:37:14 +0400 Subject: [PATCH] ooooop --- .../FlowerShopDataBase.cs | 3 +- .../{Implemeter.cs => Implementer.cs} | 4 +- .../ImplementerStorage.cs | 87 +++++++++++++++ FlowerShopDatabaseImplement/Order.cs | 18 ++- FlowerShopDatabaseImplement/OrderStorage.cs | 26 ++--- .../Controllers/ImplementerController.cs | 103 ++++++++++++++++++ 6 files changed, 220 insertions(+), 21 deletions(-) rename FlowerShopDatabaseImplement/{Implemeter.cs => Implementer.cs} (91%) create mode 100644 FlowerShopDatabaseImplement/ImplementerStorage.cs create mode 100644 FlowerShopRestApi/Controllers/ImplementerController.cs diff --git a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs index 1ff39cb..a45aff5 100644 --- a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs +++ b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs @@ -23,6 +23,7 @@ namespace FlowerShopDatabaseImplement public virtual DbSet FlowerComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } - } + } } diff --git a/FlowerShopDatabaseImplement/Implemeter.cs b/FlowerShopDatabaseImplement/Implementer.cs similarity index 91% rename from FlowerShopDatabaseImplement/Implemeter.cs rename to FlowerShopDatabaseImplement/Implementer.cs index a029132..bdd085c 100644 --- a/FlowerShopDatabaseImplement/Implemeter.cs +++ b/FlowerShopDatabaseImplement/Implementer.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using FlowerShopContracts.BindingModels; using FlowerShopContracts.ViewModels; -using FlowerShopDataModels; +using FlowerShopDataModels.Models; namespace FlowerShopDatabaseImplement.Models { diff --git a/FlowerShopDatabaseImplement/ImplementerStorage.cs b/FlowerShopDatabaseImplement/ImplementerStorage.cs new file mode 100644 index 0000000..1837f7c --- /dev/null +++ b/FlowerShopDatabaseImplement/ImplementerStorage.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new FlowerShopDataBase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + context.Implementers.Remove(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password) && + !model.Id.HasValue) + { + return null; + } + using var context = new FlowerShopDataBase(); + return context.Implementers + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ImplementerFIO) || x.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)) + ?.GetViewModel; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + return new(); + } + + public List GetFullList() + { + using var context = new FlowerShopDataBase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new FlowerShopDataBase(); + var res = Implementer.Create(model); + if (res != null) + { + context.Implementers.Add(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new FlowerShopDataBase(); + var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + return res?.GetViewModel; + } + } +} diff --git a/FlowerShopDatabaseImplement/Order.cs b/FlowerShopDatabaseImplement/Order.cs index c63cf15..8ade00c 100644 --- a/FlowerShopDatabaseImplement/Order.cs +++ b/FlowerShopDatabaseImplement/Order.cs @@ -31,7 +31,9 @@ namespace FlowerShopDatabaseImplement.Models public int ClientId { get; private set; } public virtual Client? Client { get; private set; } public virtual Flower? Flower { get; set; } - public static Order? Create(OrderBindingModel model) + public int? ImplementerId { get; private set; } = null; + public virtual Implementer? Implementer { get; private set; } + public static Order? Create(OrderBindingModel model) { if (model == null) { @@ -47,7 +49,10 @@ namespace FlowerShopDatabaseImplement.Models DateImplement = model.DateImplement, FlowerId = model.FlowerId, ClientId = model.ClientId, - }; + ImplementerId = model.ImplementerId, + Implementer = (model.ImplementerId.HasValue ? context.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId) + : null), + }; } public void Update(OrderBindingModel? model) @@ -58,7 +63,8 @@ namespace FlowerShopDatabaseImplement.Models } Status = model.Status; DateImplement = model.DateImplement; - } + ImplementerId = model.ImplementerId; + } public OrderViewModel GetViewModel => new() { @@ -67,12 +73,14 @@ namespace FlowerShopDatabaseImplement.Models Sum = Sum, Status = Status, ClientId = ClientId, - DateCreate = DateCreate, + ImplementerId = ImplementerId, + DateCreate = DateCreate, DateImplement = DateImplement, FlowerName = Flower?.FlowerName ?? String.Empty, Id = Id, ClientFIO = Client?.ClientFIO ?? String.Empty, - }; + ImplementerFIO = (Implementer != null ? Implementer.ImplementerFIO : string.Empty) + }; } } diff --git a/FlowerShopDatabaseImplement/OrderStorage.cs b/FlowerShopDatabaseImplement/OrderStorage.cs index 4d20bc9..5697ce1 100644 --- a/FlowerShopDatabaseImplement/OrderStorage.cs +++ b/FlowerShopDatabaseImplement/OrderStorage.cs @@ -18,14 +18,12 @@ namespace FlowerShopDatabaseImplement.Implements public List GetFullList() { using var context = new FlowerShopDataBase(); - return context.Orders.Include(x => x.Flower).Include(x => x.Client) - .Select(x => x.GetViewModel) - .ToList(); + return context.Orders.Include(x => x.Flower). + Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(OrderSearchModel model) { - - using var context = new FlowerShopDataBase(); return context.Orders.Include(x => x.Flower) .Where(x => ((!model.Id.HasValue || x.Id == model.Id) && @@ -37,13 +35,13 @@ namespace FlowerShopDatabaseImplement.Implements } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return null; - } + using var context = new FlowerShopDataBase(); - return context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; - } + return context.Orders.Include(x => x.Flower).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault( + x => ((model.Id.HasValue && x.Id == model.Id) || + (model.ImplementerId.HasValue && model.Status.HasValue && + x.ImplementerId == model.ImplementerId && x.Status == model.Status)))?.GetViewModel; + } public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -59,9 +57,9 @@ namespace FlowerShopDatabaseImplement.Implements public OrderViewModel? Update(OrderBindingModel model) { using var context = new FlowerShopDataBase(); - var order = context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(x => x.Id == - model.Id); - if (order == null) + var order = context.Orders.Include(x => x.Flower).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id); + + if (order == null) { return null; } diff --git a/FlowerShopRestApi/Controllers/ImplementerController.cs b/FlowerShopRestApi/Controllers/ImplementerController.cs new file mode 100644 index 0000000..1290011 --- /dev/null +++ b/FlowerShopRestApi/Controllers/ImplementerController.cs @@ -0,0 +1,103 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Enums; +using Microsoft.AspNetCore.Mvc; + +namespace FlowerShopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ImplementerController : ControllerBase + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IImplementerLogic _logic; + public ImplementerController(IOrderLogic order, IImplementerLogic logic, + ILogger logger) + { + _logger = logger; + _order = order; + _logic = logic; + } + [HttpGet] + public ImplementerViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ImplementerSearchModel + { + ImplementerFIO = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка авторизации сотрудника"); + throw; + } + } + [HttpGet] + public List? GetNewOrders() + { + try + { + return _order.ReadList(new OrderSearchModel + { + Status = OrderStatus.Принят + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения новых заказов"); + throw; + } + } + [HttpGet] + public OrderViewModel? GetImplementerOrder(int implementerId) + { + try + { + return _order.ReadElement(new OrderSearchModel + { + ImplementerId = implementerId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения текущего заказа исполнителя"); + + throw; + } + } + + [HttpPost] + public void TakeOrderInWork(OrderBindingModel model) + { + try + { + _order.TakeOrderInWork(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка перевода заказа с №{Id} в работу", + model.Id); + throw; + } + } + [HttpPost] + public void FinishOrder(OrderBindingModel model) + { + try + { + _order.FinishOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа с №{ Id}", model.Id); + throw; + } + } + } +}