PIbd-21_Rodionov_I.A._IceCr.../IceCreamShop/IceCreamShopRestApi/Controllers/ShopController.cs

112 lines
2.9 KiB
C#

using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.BusinessLogicsContracts;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace IceCreamShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
private readonly ILogger _logger;
private readonly IShopLogic _logic;
public ShopController(IShopLogic logic, ILogger<ShopController> logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public List<ShopViewModel>? GetShopList()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving shop list error");
throw;
}
}
[HttpGet]
public ShopViewModel? GetShop(int shopId)
{
try
{
var shop = _logic.ReadElement(new ShopSearchModel { Id = shopId });
if (shop != null)
{
shop.ShortIceCreamsInfo = shop.ShopIceCreams.Select(x => Tuple.Create(x.Value.Item1.IceCreamName, x.Value.Item1.Price, x.Value.Item2)).ToList();
}
return shop;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error receiving shop by id = {Id}", shopId);
throw;
}
}
[HttpPost]
public void CreateShop(ShopBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop creation error");
throw;
}
}
[HttpPost]
public void UpdateShop(ShopBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop update error");
throw;
}
}
[HttpPost]
public void DeleteShop(ShopBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop deletion error");
throw;
}
}
[HttpPost]
public void MakeShipment(Tuple<ShopSearchModel, IceCreamViewModel, int> shopWorks)
{
try
{
_logic.MakeShipment(shopWorks.Item1, shopWorks.Item2, shopWorks.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Make shipment error");
throw;
}
}
}
}