113 lines
2.9 KiB
C#
113 lines
2.9 KiB
C#
using HardwareShopContracts.BindingModels;
|
|
using HardwareShopContracts.BusinessLogicsContracts;
|
|
using HardwareShopContracts.ViewModels;
|
|
using HardwareShopDataModels.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace HardwareShopRestApi.Controllers
|
|
{
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class GoodController : Controller
|
|
{
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IGoodLogic _good;
|
|
|
|
public GoodController(ILogger<GoodController> logger, IGoodLogic good)
|
|
{
|
|
_logger = logger;
|
|
_good = good;
|
|
}
|
|
|
|
[HttpGet]
|
|
public List<GoodViewModel>? GetAllGoods()
|
|
{
|
|
try
|
|
{
|
|
return _good.ReadList(null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка всех товаров");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public List<GoodViewModel>? GetGoods(int userId)
|
|
{
|
|
try
|
|
{
|
|
return _good.ReadList(new() { UserId = userId });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка товаров");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public GoodViewModel? GetGood(int id)
|
|
{
|
|
try
|
|
{
|
|
return _good.ReadElement(new() { Id = id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения товара");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void CreateGood(GoodBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
for (int i = 0; i < model.GoodComponentsCounts.Count; i++)
|
|
{
|
|
model.GoodComponents.Add(model.GoodComponentsComponents[i].Id,
|
|
(model.GoodComponentsComponents[i] as IComponentModel, model.GoodComponentsCounts[i]));
|
|
}
|
|
_good.Create(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка создания товара");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void UpdateData(GoodBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_good.Update(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка обновления данных товара");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public void DeleteGood(GoodBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_good.Delete(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка удаления товара");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|