using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.BusinessLogicContracts; using AutomobilePlantContracts.SearchModels; using AutomobilePlantContracts.ViewModels; using Microsoft.AspNetCore.Mvc; namespace AutomobilePlantRestApi.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> carCount = new List>(); foreach (var car in shop.ShopCars) { carCount.Add(new Tuple(car.Value.Item1.CarName, car.Value.Item2)); } shop = new() { Id = shop.Id, Name = shop.Name, Address = shop.Address, OpeningDate = shop.OpeningDate, MaxCountCars = shop.MaxCountCars, CarCount = carCount }; } 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 SupplyCarsToShop(Tuple supplyRequest) { try { _shop.SupplyCars(supplyRequest.Item1, supplyRequest.Item2, supplyRequest.Item3); } catch (Exception ex) { _logger.LogError(ex, "Ошибка пополнения магазина"); throw; } } } }