2024-06-17 23:29:35 +04:00

122 lines
3.3 KiB
C#

using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.BusinessLogicsContracts;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace CarRepairShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
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, "Receiving shop list error");
throw;
}
}
[HttpGet]
public ShopViewModel? GetShop(int shopId)
{
try
{
var element = _logic.ReadElement(new ShopSearchModel { Id = shopId });
var newDict = new Dictionary<int, ShopRepairViewModel>();
if (element != null)
{
foreach (var item in element.ShopRepairs)
{
newDict.Add(item.Key, new ShopRepairViewModel
{
RepairName = item.Value.Item1.RepairName,
Count = item.Value.Item2,
});
}
element.ViewRepairs = newDict;
}
return element;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error receiving shop by id = {Id}", shopId);
throw;
}
}
[HttpPost]
public void CreateShop(ShopBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop creation error");
throw;
}
}
[HttpPost]
public void UpdateShop(ShopBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop update error");
throw;
}
}
[HttpPost]
public void DeleteShop(ShopBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop deletion error");
throw;
}
}
[HttpPost]
public void MakeShipment(Tuple<ShopSearchModel, RepairViewModel, int> shopWorks)
{
try
{
_logic.MakeShipment(shopWorks.Item1, shopWorks.Item2, shopWorks.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Make shipment error");
throw;
}
}
}
}