PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopRestApi/Controllers/ShopController.cs

133 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace FlowerShopRestApi.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, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public Tuple<ShopViewModel, List<FlowerViewModel>, List<int>>? GetShopFlowers(int shopId)
{
try
{
var shop = _logic.ReadElement(new() { Id = shopId });
if (shop == null)
{
return null;
}
var tuple = Tuple.Create(shop,
shop.ShopFlowers.Select(x => new FlowerViewModel()
{
Id = x.Value.Item1.Id,
Price = x.Value.Item1.Price,
FlowerName = x.Value.Item1.FlowerName,
}).ToList(),
shop.ShopFlowers.Select(x => x.Value.Item2).ToList());
return tuple;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина с цветами");
throw;
}
}
[HttpGet]
public Dictionary<int, (IFlowerModel, int)>? GetListFlower(int shopId)
{
try
{
var shop = _logic.ReadElement(new() { Id = shopId });
if (shop == null)
{
return null;
}
return shop.ShopFlowers;
}
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 MakeSupply(Tuple<ShopSearchModel, FlowerViewModel, int> shopFlowers)
{
try
{
_logic.MakeSupply(shopFlowers.Item1, shopFlowers.Item2, shopFlowers.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления цветов в магазин");
throw;
}
}
}
}