2023-05-16 15:41:04 +04:00
|
|
|
|
using HardwareShopContracts.BindingModels;
|
2023-05-16 17:54:45 +04:00
|
|
|
|
using HardwareShopContracts.BusinessLogicsContracts;
|
2023-05-16 15:41:04 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 17:54:45 +04:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<GoodViewModel>? GetAllGoods()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _good.ReadList(null);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка всех товаров");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2023-05-16 15:41:04 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 12:28:05 +04:00
|
|
|
|
[HttpGet]
|
2023-05-19 15:26:48 +04:00
|
|
|
|
public Tuple<GoodViewModel, List<Tuple<ComponentViewModel, int>>>? GetGoodUpdate(int id, int userId)
|
2023-05-17 12:28:05 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-19 15:26:48 +04:00
|
|
|
|
var good = _good.ReadElement(new() { Id = id, UserId = userId });
|
2023-05-17 12:28:05 +04:00
|
|
|
|
if (good == null)
|
|
|
|
|
return null;
|
|
|
|
|
var tuple = Tuple.Create(good,
|
|
|
|
|
good.GoodComponents.Select(x => Tuple.Create(new ComponentViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = x.Value.Item1.Id,
|
|
|
|
|
ComponentName = x.Value.Item1.ComponentName,
|
|
|
|
|
Cost = x.Value.Item1.Cost
|
|
|
|
|
}, x.Value.Item2)).ToList());
|
|
|
|
|
return tuple;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения товара");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 15:41:04 +04:00
|
|
|
|
[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
|
|
|
|
|
{
|
2023-05-17 12:28:05 +04:00
|
|
|
|
for (int i = 0; i < model.GoodComponentsCounts.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
model.GoodComponents.Add(model.GoodComponentsComponents[i].Id,
|
|
|
|
|
(model.GoodComponentsComponents[i] as IComponentModel, model.GoodComponentsCounts[i]));
|
|
|
|
|
}
|
2023-05-16 15:41:04 +04:00
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|