From b31a7909bad8ec8bc60aac9a33d4b2b788222f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Sun, 23 Apr 2023 20:20:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=20RestApi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ShopController.cs | 92 +++++++++++++++++++ .../PrecastConcretePlantRestApi/Program.cs | 5 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 PrecastConcretePlant/PrecastConcretePlantRestApi/Controllers/ShopController.cs diff --git a/PrecastConcretePlant/PrecastConcretePlantRestApi/Controllers/ShopController.cs b/PrecastConcretePlant/PrecastConcretePlantRestApi/Controllers/ShopController.cs new file mode 100644 index 0000000..1017add --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantRestApi/Controllers/ShopController.cs @@ -0,0 +1,92 @@ +using Microsoft.AspNetCore.Mvc; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.BusinessLogicsContracts; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.ViewModels; + +namespace PrecastConcretePlantRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ShopController : Controller + { + private readonly ILogger _logger; + + private readonly IShopLogic _logic; + + public ShopController(IShopLogic logic, ILogger logger) + { + _logger = logger; + _logic = logic; + } + + [HttpGet] + public List? GetShops() + { + try + { + return _logic.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка магазинов"); + throw; + } + } + + [HttpGet] + public Tuple, IEnumerable>? GetShopWithReinforcedies(int id) + { + try + { + + var shop = _logic.ReadElement(new() { Id = id }); + if (shop == null) + { + return null; + } + return Tuple.Create(shop, + shop.ShopReinforcedies.Select(x => new ReinforcedViewModel() + { + Id = x.Value.Item1.Id, + Price = x.Value.Item1.Price, + ReinforcedName = x.Value.Item1.ReinforcedName, + }), + shop.ShopReinforcedies.Select(x => x.Value.Item2)); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения магазина"); + throw; + } + } + + [HttpPost] + public void CRUDShop(Action action) + { + try + { + action.Invoke(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка операции CRUD - {operation} с магазином", action.Method.Name); + throw; + } + } + + [HttpPost] + public void UpdateShop(ShopBindingModel model) => CRUDShop(() => _logic.Update(model)); + [HttpPost] + public void CreateShop(ShopBindingModel model) => CRUDShop(() => _logic.Create(model)); + [HttpPost] + public void DeleteShop(ShopBindingModel model) => CRUDShop(() => _logic.Delete(model)); + + [HttpPost] + public void AddReinforcedInShop(Tuple countReinforcedForShop) + { + CRUDShop(() => _logic.AddReinforced(countReinforcedForShop.Item1, countReinforcedForShop.Item2, countReinforcedForShop.Item3)); + } + } +} + diff --git a/PrecastConcretePlant/PrecastConcretePlantRestApi/Program.cs b/PrecastConcretePlant/PrecastConcretePlantRestApi/Program.cs index 787fe64..5fa6aad 100644 --- a/PrecastConcretePlant/PrecastConcretePlantRestApi/Program.cs +++ b/PrecastConcretePlant/PrecastConcretePlantRestApi/Program.cs @@ -12,10 +12,13 @@ builder.Logging.AddLog4Net("log4net.config"); 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.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c =>