Computer_Hardware_Store/HardwareShop/HardwareShopRestApi/Controllers/PurchaseController.cs

123 lines
2.7 KiB
C#
Raw Normal View History

2023-05-15 19:26:52 +04:00
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels;
2023-05-16 17:54:45 +04:00
using HardwareShopDatabaseImplement.Models.Storekeeper;
2023-05-16 19:49:58 +04:00
using HardwareShopDataModels.Enums;
2023-05-16 17:54:45 +04:00
using HardwareShopDataModels.Models;
using HardwareShopRestApi.Controllers;
2023-05-15 19:26:52 +04:00
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
2023-05-15 19:26:52 +04:00
namespace HardwareShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class PurchaseController : Controller
2023-05-15 19:26:52 +04:00
{
private readonly ILogger _logger;
private readonly IPurchaseLogic _purchaseLogic;
2023-05-15 19:26:52 +04:00
public PurchaseController(IPurchaseLogic purchaseLogic, ILogger<UserController> logger)
2023-05-15 19:26:52 +04:00
{
_logger = logger;
_purchaseLogic = purchaseLogic;
2023-05-16 17:54:45 +04:00
}
[HttpGet]
public List<PurchaseViewModel>? GetPurchases(int userId)
{
try
{
return _purchaseLogic.ReadList(new() { UserId = userId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка товаров");
throw;
}
}
[HttpGet]
public PurchaseViewModel? GetPurchase(int purchaseId)
2023-05-16 17:54:45 +04:00
{
try
{
return _purchaseLogic.ReadElement(new() { Id = purchaseId });
2023-05-16 17:54:45 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения товара");
throw;
}
}
[HttpPost]
public void CreatePurchase(PurchaseBindingModel model)
{
try
{
for (int i = 0; i < model.PurchaseGoodsCounts.Count; i++)
{
2023-05-16 19:49:58 +04:00
model.PurchaseGoods.Add(model.ListPurchaseGoods[i].Id,(model.ListPurchaseGoods[i] as IGoodModel, model.PurchaseGoodsCounts[i]));
2023-05-16 17:54:45 +04:00
}
_purchaseLogic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания товара");
throw;
}
}
[HttpPost]
public void UpdatePurchase(PurchaseBindingModel model)
{
try
{
2023-05-16 19:49:58 +04:00
var oldModel = _purchaseLogic.ReadElement(new() {
Id = model.Id,
});
model.Sum = oldModel.Sum;
_purchaseLogic.DeliveryPurchase(model);
2023-05-16 17:54:45 +04:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных товара");
throw;
}
}
[HttpPost]
public void Update(PurchaseBindingModel model)
{
try
{
_purchaseLogic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных товара");
throw;
}
}
2023-05-16 17:54:45 +04:00
[HttpPost]
public void DeletePurchase(PurchaseBindingModel model)
{
try
{
_purchaseLogic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления товара");
throw;
}
2023-05-15 19:26:52 +04:00
}
}
2023-05-15 19:26:52 +04:00
}