From 9d7b0751c89ed853be2f03a5e1267fd08bedfc6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Fri, 19 Apr 2024 14:12:20 +0400 Subject: [PATCH] rest api --- .../CarpentryWorkshopRestApi.csproj | 4 - .../Controllers/ClientController.cs | 69 ++++++++++++++++ .../Controllers/MainController.cs | 82 +++++++++++++++++++ 3 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/ClientController.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/MainController.cs diff --git a/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj b/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj index 6cbf777..1dd3a99 100644 --- a/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj @@ -11,10 +11,6 @@ - - - - diff --git a/CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/ClientController.cs b/CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..a915668 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/ClientController.cs @@ -0,0 +1,69 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace CarpentryWorkshopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + + private readonly IClientLogic _logic; + + public ClientController(IClientLogic logic, ILogger logger) + { + _logger = logger; + _logic = logic; + } + + [HttpGet] + public ClientViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ClientSearchModel + { + Email = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + + [HttpPost] + public void Register(ClientBindingModel model) + { + try + { + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + + [HttpPost] + public void UpdateData(ClientBindingModel model) + { + try + { + _logic.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/MainController.cs b/CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..da35b65 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopRestApi/Controllers/MainController.cs @@ -0,0 +1,82 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace CarpentryWorkshopRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + + private readonly IOrderLogic _order; + + private readonly IWoodLogic _wood; + + public MainController(ILogger logger, IOrderLogic order, IWoodLogic wood) + { + _logger = logger; + _order = order; + _wood = wood; + } + + [HttpGet] + public List? GetWoodList() + { + try + { + return _wood.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка изделий"); + throw; + } + } + + [HttpGet] + public WoodViewModel? GetWood(int woodId) + { + try + { + return _wood.ReadElement(new WoodSearchModel { Id = woodId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", woodId); + throw; + } + } + + [HttpGet] + public List? GetOrders(int clientId) + { + try + { + return _order.ReadList(new OrderSearchModel { ClientId = clientId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", clientId); + throw; + } + } + + [HttpPost] + public void CreateOrder(OrderBindingModel model) + { + try + { + _order.CreateOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + } +}