116 lines
2.6 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 Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SewingDressesContracts.BusinessLogicsContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.BindingModels;
using SewingDressesDataModels.Models;
using DocumentFormat.OpenXml;
namespace SewingDressesRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
private readonly ILogger _logger;
private readonly IShopLogic _logic;
public ShopController(ILogger<ShopController> logger, IShopLogic logic)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public List<ShopViewModel>? GetShopList()
{
try
{
var shops = _logic.ReadList(null);
for (int i = 0; i < shops.Count; i++)
{
shops[i].DressesCount = shops[i].ShopDresses.Values.ToList().Select(x => (x.Item1.DressName, x.Item2).ToTuple()).ToList();
}
return shops;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public Tuple<ShopViewModel, List<Tuple<string, int>>>? GetShop(int id)
{
try
{
var elem = _logic.ReadElement(new ShopSearchModel
{
Id = id
});
if (elem == null)
return null;
return Tuple.Create(elem, elem.ShopDresses.Select(x => Tuple.Create(x.Value.Item1.DressName, x.Value.Item2)).ToList());
}
catch (Exception ex)
{
_logger.LogError(ex, "Не получилось взять магазин Id:{id}", id);
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, DressBindingModel, int> shop_add)
{
try
{
_logic.MakeSupply(shop_add.Item1, shop_add.Item2, shop_add.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка пополнения магазина");
throw;
}
}
}
}