PIbd-23_Minhasapov_R.H._Aut.../AutomobilePlant/AutomobilePlantRestApi/Controllers/ShopController.cs

129 lines
3.6 KiB
C#
Raw Normal View History

using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.BusinessLogicContracts;
using AutomobilePlantContracts.SearchModels;
using AutomobilePlantContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace AutomobilePlantRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
private readonly ILogger _logger;
private readonly IShopLogic _shop;
public ShopController(ILogger<ShopController> logger, IShopLogic shopLogic)
{
_logger = logger;
_shop = shopLogic;
}
[HttpGet]
public List<ShopViewModel>? GetShopList()
{
try
{
return _shop.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public ShopViewModel? GetShop(int shopId)
{
try
{
var shop = _shop.ReadElement(new ShopSearchModel
{
Id = shopId
});
if (shop != null)
{
List<Tuple<string, int>> carCount = new List<Tuple<string, int>>();
foreach (var car in shop.ShopCars)
{
carCount.Add(new Tuple<string, int>(car.Value.Item1.CarName, car.Value.Item2));
}
shop = new()
{
Id = shop.Id,
Name = shop.Name,
Address = shop.Address,
OpeningDate = shop.OpeningDate,
MaxCountCars = shop.MaxCountCars,
CarCount = carCount
};
}
return shop;
}
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 UpdateShop(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 SupplyCarsToShop(Tuple<ShopSearchModel, CarBindingModel, int> supplyRequest)
{
try
{
_shop.SupplyCars(supplyRequest.Item1, supplyRequest.Item2, supplyRequest.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка пополнения магазина");
throw;
}
}
}
}