PIbd-23_Polevoy_S.V._Flower.../FlowerShop/FlowerShopRestApi/Controllers/ShopController.cs

128 lines
3.6 KiB
C#

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<ShopController> logger, IShopLogic shopLogic)
{
_logger = logger;
_shop = shopLogic;
}
[HttpGet]
public List<ShopViewModel>? 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<Tuple<string, int>> bouquetCount = new List<Tuple<string, int>>();
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<ShopSearchModel, BouquetBindingModel, int> supplyRequest)
{
try
{
_shop.SupplyBouquets(supplyRequest.Item1, supplyRequest.Item2, supplyRequest.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during supplying shop");
throw;
}
}
}
}