PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantRestApi/Controllers/ShopController.cs

155 lines
4.5 KiB
C#
Raw Normal View History

2024-04-21 02:28:27 +04:00
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.BusinessLogicsContracts;
using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
namespace AircraftPlantRestApi.Controllers
{
/// <summary>
/// Контроллер для работы с магазинами
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для магазинов
/// </summary>
private readonly IShopLogic _shopLogic;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="shopLogic"></param>
public ShopController(ILogger<MainController> logger, IShopLogic shopLogic)
{
_logger = logger;
_shopLogic = shopLogic;
}
/// <summary>
/// Получение списка магазинов
/// </summary>
/// <returns></returns>
[HttpGet]
public List<ShopViewModel>? GetShops()
{
try
{
return _shopLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
/// <summary>
/// Получение магазина
/// </summary>
/// <param name="shopId"></param>
/// <returns></returns>
[HttpGet]
public Tuple<ShopViewModel, List<Tuple<string, int>>>? 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;
}
}
/// <summary>
/// Создать магазин
/// </summary>
/// <param name="model"></param>
[HttpPost]
public void CreateShop(ShopBindingModel model)
{
try
{
_shopLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания магазина");
throw;
}
}
/// <summary>
/// Изменить магазин
/// </summary>
/// <param name="model"></param>
[HttpPost]
public void UpdateShop(ShopBindingModel model)
{
try
{
model.ShopPlanes = null!;
_shopLogic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
/// <summary>
/// Удалить магазин
/// </summary>
/// <param name="model"></param>
[HttpPost]
public void DeleteShop(ShopBindingModel model)
{
try
{
_shopLogic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления магазина");
throw;
}
}
/// <summary>
/// Добавить изделие в магазин
/// </summary>
/// <param name="model"></param>
[HttpPost]
public void AddPlaneInShop(Tuple<ShopSearchModel, PlaneViewModel, int> model)
{
try
{
_shopLogic.AddPlaneInShop(model.Item1, model.Item2, model.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления изделия в магазин");
throw;
}
}
}
}