112 lines
3.1 KiB
C#
112 lines
3.1 KiB
C#
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.BusinessLogicsContracts;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.ViewModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SushiBarDataModels.Models;
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
|
|
|
namespace SushiBarRestApi.Controllers
|
|
{
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class ShopController : Controller
|
|
{
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IShopLogic _shop;
|
|
|
|
public ShopController(ILogger<MainController> logger, IShopLogic shop)
|
|
{
|
|
_logger = logger;
|
|
_shop = shop;
|
|
}
|
|
|
|
[HttpGet]
|
|
public List<ShopViewModel>? GetShops()
|
|
{
|
|
try
|
|
{
|
|
return _shop.ReadList(null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка магазинов");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public Tuple<ShopViewModel, List<Tuple<string, int>>>? GetShop(int shopId)
|
|
{
|
|
try
|
|
{
|
|
var elem = _shop.ReadElement(new ShopSearchModel { Id = shopId });
|
|
if (elem == null)
|
|
return null;
|
|
return Tuple.Create(elem, elem.ListSushi.Select(x => Tuple.Create(x.Value.Item1.SushiName, x.Value.Item2)).ToList());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения магазина по id={Id}", shopId);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateShop(ShopBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_shop.Create(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка создания магазина");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void UpdateData(ShopBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_shop.Update(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка обновления данных");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void DeleteShop(ShopBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_shop.Delete(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка удаления магазина");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void AddSushiInShop(Tuple<ShopSearchModel, SushiViewModel, int> model)
|
|
{
|
|
try
|
|
{
|
|
_shop.AddSushiInShop(model.Item1, model.Item2, model.Item3);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка добавления суши в магазин");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
} |