PIbd-21_RazubaevSM_Plumbing.../PlumbingRepair/PlumbingRepairRestApi/Controllers/ShopController.cs

133 lines
4.0 KiB
C#
Raw Normal View History

2024-05-11 20:51:02 +04:00
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using PlumbingRepairDataModels.Models;
namespace PlumbingRepairRestApi.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, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public Tuple<ShopViewModel, List<WorkViewModel>, List<int>>? GetShopWorks(int shopId)
{
try
{
var shop = _logic.ReadElement(new() { Id = shopId });
if (shop == null)
{
return null;
}
var tuple = Tuple.Create(shop,
shop.ShopWorks.Select(x => new WorkViewModel()
{
Id = x.Value.Item1.Id,
Price = x.Value.Item1.Price,
WorkName = x.Value.Item1.WorkName,
}).ToList(),
shop.ShopWorks.Select(x => x.Value.Item2).ToList());
return tuple;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина с работами");
throw;
}
}
[HttpGet]
public Dictionary<int, (IWorkModel, int)>? GetListWork(int shopId)
{
try
{
var shop = _logic.ReadElement(new() { Id = shopId });
if (shop == null)
{
return null;
}
return shop.ShopWorks;
}
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 StoreReplenishment(Tuple<ShopSearchModel, WorkViewModel, int> shopWorks)
{
try
{
_logic.StoreReplenishment(shopWorks.Item1, shopWorks.Item2, shopWorks.Item3);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления заготовок в магазин");
throw;
}
}
}
}