2023-06-13 14:33:24 +03:00

142 lines
4.1 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.Mvc;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
namespace PrecastConcretePlantRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController
{
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<ReinforcedViewModel>, List<int>>? GetShopWithReinforceds(int shopId)
{
try
{
var shop = _logic.ReadElement(new() { Id = shopId });
if (shop == null)
{
return null;
}
var tuple = Tuple.Create(shop,
shop.ShopReinforceds.Select(x => new ReinforcedViewModel()
{
Id = x.Value.Item1.Id,
Price = x.Value.Item1.Price,
ReinforcedName = x.Value.Item1.ReinforcedName,
}).ToList(),
shop.ShopReinforceds.Select(x => x.Value.Item2).ToList());
return tuple;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина с ЖБИ");
throw;
}
}
[HttpGet]
public Dictionary<int, (IReinforcedModel, int)>? GetShopReinforceds(int shopId)
{
try
{
var shop = _logic.ReadElement(new() { Id = shopId });
if (shop == null)
{
return null;
}
return shop.ShopReinforceds;
}
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 AddReinforcedInShop(Tuple<ShopSearchModel, ReinforcedViewModel, int> shopReinforcedWithCount)
{
try
{
_logic.AddReinforcedInShop(shopReinforcedWithCount.Item1, shopReinforcedWithCount.Item2, shopReinforcedWithCount.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления ЖБИ в магазин");
throw;
}
}
}
}