using FlowerShopContracts.BindingModels; using FlowerShopContracts.BusinessLogicsContracts; using FlowerShopContracts.SearchModels; using FlowerShopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; namespace FlowerShopRestApi.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).Select(shop => { shop.ShopBouquets = new(); return shop; }).ToList(); } catch (Exception ex) { _logger.LogError(ex, "Error during recieving list of shops"); throw; } } [HttpGet] public ShopViewModel? GetShop(int shopId) { try { var shop = _shop.ReadElement(new ShopSearchModel { Id = shopId }); if (shop != null) { List> bouquetCount = new List>(); foreach (var bouquet in shop.ShopBouquets) { bouquetCount.Add(Tuple.Create(bouquet.Value.Item1.BouquetName, bouquet.Value.Item2)); } shop = new() { Id = shop.Id, Name = shop.Name, Address = shop.Address, OpeningDate = shop.OpeningDate, MaxCountBouquets = shop.MaxCountBouquets, BouquetCount = bouquetCount, ShopBouquets = shop.ShopBouquets }; } return shop; } catch (Exception ex) { _logger.LogError(ex, "Error during recieving shop with id={Id}", shopId); throw; } } [HttpPost] public void CreateShop(ShopBindingModel model) { try { _shop.Create(model); } catch (Exception ex) { _logger.LogError(ex, "Error during creating shop"); throw; } } [HttpPost] public void UpdateShop(ShopBindingModel model) { try { _shop.Update(model); } catch (Exception ex) { _logger.LogError(ex, "Error during updating shop"); throw; } } [HttpPost] public void DeleteShop(ShopBindingModel model) { try { _shop.Delete(model); } catch (Exception ex) { _logger.LogError(ex, "Error during deleting shop"); throw; } } [HttpPost] public void SupplyBouquetsToShop(Tuple supplyRequest) { try { _shop.SupplyBouquets(supplyRequest.Item1, supplyRequest.Item2, supplyRequest.Item3); } catch (Exception ex) { _logger.LogError(ex, "Error during supplying shop"); throw; } } } }