From 056df448a64d6c11d7316af092e1598595c1de0a Mon Sep 17 00:00:00 2001 From: malimova Date: Fri, 3 May 2024 20:48:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ClientController.cs | 69 ++++++++++++++++ .../Controllers/MainController.cs | 82 +++++++++++++++++++ .../Controllers/WeatherForecastController.cs | 33 -------- 3 files changed, 151 insertions(+), 33 deletions(-) create mode 100644 Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs create mode 100644 Confectionery/ConfectioneryRestApi/Controllers/MainController.cs delete mode 100644 Confectionery/ConfectioneryRestApi/Controllers/WeatherForecastController.cs diff --git a/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs b/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..ca55286 --- /dev/null +++ b/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs @@ -0,0 +1,69 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace ConfectioneryRestApi.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/Confectionery/ConfectioneryRestApi/Controllers/MainController.cs b/Confectionery/ConfectioneryRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..b932c1a --- /dev/null +++ b/Confectionery/ConfectioneryRestApi/Controllers/MainController.cs @@ -0,0 +1,82 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace ConfectioneryRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + + private readonly IOrderLogic _order; + + private readonly IPastryLogic _pastry; + + public MainController(ILogger logger, IOrderLogic order, IPastryLogic pastry) + { + _logger = logger; + _order = order; + _pastry = pastry; + } + + [HttpGet] + public List? GetPastryList() + { + try + { + return _pastry.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка продуктов"); + throw; + } + } + + [HttpGet] + public PastryViewModel? GetPastry(int pastryId) + { + try + { + return _pastry.ReadElement(new PastrySearchModel { Id = pastryId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", pastryId); + 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; + } + } + } +} diff --git a/Confectionery/ConfectioneryRestApi/Controllers/WeatherForecastController.cs b/Confectionery/ConfectioneryRestApi/Controllers/WeatherForecastController.cs deleted file mode 100644 index c073e3d..0000000 --- a/Confectionery/ConfectioneryRestApi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace ConfectioneryRestApi.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -}