using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace AircraftPlantRestApi.Controllers
{
///
/// Контроллер для работы с магазинами
///
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Бизнес-логика для магазинов
///
private readonly IShopLogic _shopLogic;
///
/// Конструктор
///
///
///
public ShopController(ILogger logger, IShopLogic shopLogic)
{
_logger = logger;
_shopLogic = shopLogic;
}
///
/// Получение списка магазинов
///
///
[HttpGet]
public List? GetShops()
{
try
{
return _shopLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
///
/// Получение магазина
///
///
///
[HttpGet]
public Tuple>>? GetShop(int shopId)
{
try
{
var shop = _shopLogic.ReadElement(new ShopSearchModel { Id = shopId });
if (shop == null)
{
return null;
}
return Tuple.Create(shop, shop.ShopPlanes.Select(x => Tuple.Create(x.Value.Item1.PlaneName, x.Value.Item2)).ToList());
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина по id={Id}", shopId);
throw;
}
}
///
/// Создать магазин
///
///
[HttpPost]
public void CreateShop(ShopBindingModel model)
{
try
{
_shopLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания магазина");
throw;
}
}
///
/// Изменить магазин
///
///
[HttpPost]
public void UpdateShop(ShopBindingModel model)
{
try
{
model.ShopPlanes = null!;
_shopLogic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
///
/// Удалить магазин
///
///
[HttpPost]
public void DeleteShop(ShopBindingModel model)
{
try
{
_shopLogic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления магазина");
throw;
}
}
///
/// Добавить изделие в магазин
///
///
[HttpPost]
public void AddPlaneInShop(Tuple model)
{
try
{
_shopLogic.AddPlaneInShop(model.Item1, model.Item2, model.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления изделия в магазин");
throw;
}
}
}
}