using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System; using LawFirmContracts.BusinessLogicContracts; using LawFirmContracts.ViewModels; using LawFirmContracts.SearchModels; using LawFirmContracts.BindingModels; using LawFirmDataModels.Models; namespace LawFirmRestApi.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class ShopController : Controller { private readonly ILogger _logger; private readonly IShopLogic _shop; public ShopController(ILogger logger, IShopLogic shopLogic) { _logger = logger; _shop = shopLogic; } [HttpGet] public List? GetShopList() { try { return _shop.ReadList(null); } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения списка магазинов"); throw; } } [HttpGet] public ShopViewModel? GetShop(int shopId) { try { var shop = _shop.ReadElement(new ShopSearchModel { Id = shopId }); if (shop != null) { List> docCount = new List>(); foreach (var doc in shop.ShopDocuments) { docCount.Add(new Tuple(doc.Value.Item1.DocumentName, doc.Value.Item2)); } shop = new() { Id = shop.Id, Name = shop.Name, Adress = shop.Adress, OpeningDate = shop.OpeningDate, MaxCountDocuments = shop.MaxCountDocuments, DocumentCount = docCount }; } return shop; } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения магазина по id={Id}",shopId); throw; } } [HttpPost] public void CreateShop(ShopBindingModel model) { try { _shop.Create(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания магазина"); throw; } } [HttpPost] public void UpdateShop(ShopBindingModel model) { try { _shop.Update(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка обновления магазина"); throw; } } [HttpPost] public void DeleteShop(ShopBindingModel model) { try { _shop.Delete(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка удаления магазина"); throw; } } [HttpPost] public void SupplyDocumentsToShop(Tuple supplyRequest) { try { _shop.SupplyDocuments(supplyRequest.Item1, supplyRequest.Item2, supplyRequest.Item3); } catch (Exception ex) { _logger.LogError(ex, "Ошибка пополнения магазина"); throw; } } } }