using PlumbingRepairContracts.BindingModels; using PlumbingRepairContracts.BusinessLogicsContracts; using PlumbingRepairContracts.SearchModels; using PlumbingRepairContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using PlumbingRepairDataModels.Models; namespace PlumbingRepairRestApi.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? GetShopList() { try { return _logic.ReadList(null); } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения списка магазинов"); throw; } } [HttpGet] public Tuple, List>? GetShopWorks(int shopId) { try { var shop = _logic.ReadElement(new() { Id = shopId }); if (shop == null) { return null; } var tuple = Tuple.Create(shop, shop.ShopWorks.Select(x => new WorkViewModel() { Id = x.Value.Item1.Id, Price = x.Value.Item1.Price, WorkName = x.Value.Item1.WorkName, }).ToList(), shop.ShopWorks.Select(x => x.Value.Item2).ToList()); return tuple; } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения магазина с работами"); throw; } } [HttpGet] public Dictionary? GetListWork(int shopId) { try { var shop = _logic.ReadElement(new() { Id = shopId }); if (shop == null) { return null; } return shop.ShopWorks; } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения работ магазина"); throw; } } [HttpPost] public void CreateShop(ShopBindingModel model) { try { _logic.Create(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания магазина"); throw; } } [HttpPost] public void UpdateShop(ShopBindingModel model) { try { _logic.Update(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка обновления магазина"); throw; } } [HttpPost] public void DeleteShop(ShopBindingModel model) { try { _logic.Delete(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка удаления магазина"); throw; } } [HttpPost] public void StoreReplenishment(Tuple shopWorks) { try { _logic.StoreReplenishment(shopWorks.Item1, shopWorks.Item2, shopWorks.Item3); } catch (Exception ex) { _logger.LogError(ex, "Ошибка добавления заготовок в магазин"); throw; } } } }