PIbd-23-Salin-O.A.-IceCream.../IceCreamShop/IceCreamShopRestApi/Controllers/ShopController.cs

109 lines
2.4 KiB
C#
Raw Normal View History

2024-04-14 12:31:53 +04:00
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.BusinessLogicsContracts;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace IceCreamShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : ControllerBase
{
private readonly ILogger _logger;
private readonly IShopLogic _logic;
public ShopController(IShopLogic logic, ILogger<ShopController>
logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
2024-04-14 16:46:26 +04:00
public Tuple<ShopViewModel, List<Tuple<string, int>>>? GetShop(int shopId)
2024-04-14 12:31:53 +04:00
{
try
{
2024-04-14 16:46:26 +04:00
var elem = _logic.ReadElement(new ShopSearchModel { Id = shopId });
if (elem == null)
return null;
return Tuple.Create(elem, elem.ShopIceCreams.Select(x => Tuple.Create(x.Value.Item1.IceCreamName, x.Value.Item2)).ToList());
2024-04-14 12:31:53 +04:00
}
catch (Exception ex)
{
2024-04-14 16:46:26 +04:00
_logger.LogError(ex, "Ошибка получения магазина по id={Id}", shopId);
2024-04-14 12:31:53 +04:00
throw;
}
}
[HttpGet]
public List<ShopViewModel> GetShops()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
[HttpPost]
public bool CreateShop(ShopBindingModel model)
{
try
{
return _logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать магазин");
throw;
}
}
[HttpPost]
public bool UpdateShop(ShopBindingModel model)
{
try
{
return _logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить магазин");
throw;
}
}
[HttpPost]
public bool DeleteShop(ShopBindingModel model)
{
try
{
return _logic.Delete(model);
}
catch (Exception ex)
{
2024-04-14 16:06:36 +04:00
_logger.LogError(ex, "Ошибка удаления магазина");
2024-04-14 12:31:53 +04:00
throw;
}
}
[HttpPost]
2024-04-14 16:06:36 +04:00
public bool MakeSupply(Tuple<ShopSearchModel, IceCreamViewModel, int> model)
2024-04-14 12:31:53 +04:00
{
try
{
2024-04-14 14:57:27 +04:00
return _logic.MakeSupply(model.Item1, model.Item2, model.Item3);
2024-04-14 12:31:53 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось сделать поставку в магазин");
throw;
}
}
}
}