118 lines
2.9 KiB
C#
118 lines
2.9 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 _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 ShopViewModel? GetShop(int shopId)
|
|
{
|
|
try
|
|
{
|
|
var element = _logic.ReadElement(new ShopSearchModel { Id = shopId });
|
|
var newDict = new Dictionary<int, ShopFlowerViewModel>();
|
|
if (element != null)
|
|
{
|
|
foreach (var item in element.ShopFlowers)
|
|
{
|
|
newDict.Add(item.Key, new ShopFlowerViewModel
|
|
{
|
|
FlowerName = item.Value.Item1.FlowerName,
|
|
Count = item.Value.Item2,
|
|
});
|
|
}
|
|
element.ViewFlowers = newDict;
|
|
}
|
|
return element;
|
|
}
|
|
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
|
|
{
|
|
if (!_logic.MakeSupply(shopFlowers.Item1, shopFlowers.Item2, shopFlowers.Item3))
|
|
{
|
|
throw new Exception("Попытка добавить в магазин число элементов, большее чем максимум");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка добавления цветов в магазин");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|