PIAPS_CW/RestAPI/Controllers/PurchaseController.cs

153 lines
4.6 KiB
C#
Raw Normal View History

2024-06-26 08:04:07 +04:00
using BusinessLogic.BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Exceptions;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
2024-06-24 23:22:03 +04:00
using Microsoft.AspNetCore.Mvc;
namespace RestAPI.Controllers
{
2024-06-26 08:04:07 +04:00
[Route("[controller]/[action]")]
[ApiController]
2024-06-24 23:22:03 +04:00
public class PurchaseController : Controller
{
2024-06-26 08:04:07 +04:00
private readonly ILogger _logger;
private readonly IPurchaseLogic _purchaseLogic;
2024-06-24 23:22:03 +04:00
2024-06-26 08:04:07 +04:00
public PurchaseController(ILogger<PurchaseController> logger, IPurchaseLogic purchaseLogic)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
_logger = logger;
_purchaseLogic = purchaseLogic;
2024-06-24 23:22:03 +04:00
}
2024-06-26 08:04:07 +04:00
[HttpGet]
public List<PurchaseViewModel>? GetList(double? costfrom, double? costto, DateTime? datefrom, DateTime? dateto)
2024-06-24 23:22:03 +04:00
{
try
{
2024-06-26 08:04:07 +04:00
if (costfrom == null && costto == null && datefrom == null && dateto == null)
return _purchaseLogic.ReadElements(null);
else
return _purchaseLogic.ReadElements(new PurchaseSearchModel()
{
CostFrom = costfrom,
CostTo = costto,
DateFrom = datefrom,
DateTo = dateto
});
2024-06-24 23:22:03 +04:00
}
2024-06-26 08:04:07 +04:00
catch (Exception ex)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
2024-06-24 23:22:03 +04:00
}
}
2024-06-26 08:04:07 +04:00
[HttpGet]
public PurchaseViewModel Get(Guid id)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
try
{
return _purchaseLogic.ReadElement(new PurchaseSearchModel() { Id = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта");
throw;
}
2024-06-24 23:22:03 +04:00
}
[HttpPost]
2024-06-26 08:04:07 +04:00
public PurchaseViewModel Create(Guid UserId)
2024-06-24 23:22:03 +04:00
{
try
{
2024-06-26 08:04:07 +04:00
var purchase = _purchaseLogic.Create(new PurchaseBindingModel()
{
Cost = 0,
DatePurchase = DateTime.Now,
UserId = UserId
});
return new PurchaseViewModel()
{
DatePurchase = purchase.DatePurchase,
UserId = purchase.UserId,
Cost = purchase.Cost,
};
2024-06-24 23:22:03 +04:00
}
2024-06-26 08:04:07 +04:00
catch (Exception ex)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
_logger.LogError(ex, "Error update purchase");
return null;
2024-06-24 23:22:03 +04:00
}
}
2024-06-26 08:04:07 +04:00
[HttpPatch]
public PurchaseViewModel Update([FromBody] PurchaseBindingModel model)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
try
{
var res = _purchaseLogic.Update(model);
return new PurchaseViewModel()
{
Id = res.Id,
Cost = res.Cost,
DatePurchase = res.DatePurchase,
PurchaseProducts = res.PurchaseProducts,
UserId = res.UserId,
};
}
2024-06-24 23:22:03 +04:00
2024-06-26 08:04:07 +04:00
catch (Exception ex)
{
_logger.LogError(ex, "Error update purchase");
return null;
}
}
[HttpPatch]
public PurchaseViewModel AddProducts(Guid purchaseId, Guid productId, int count)
2024-06-24 23:22:03 +04:00
{
try
{
2024-06-26 08:04:07 +04:00
var purchase = _purchaseLogic.AddProduct(
new PurchaseSearchModel()
{
Id = purchaseId
},
new ProductSearchModel()
{
Id = productId
},
count);
return new PurchaseViewModel()
{
Id = purchase.Id,
Cost = purchase.Cost,
DatePurchase = purchase.DatePurchase,
PurchaseProducts = purchase.PurchaseProducts,
UserId = purchase.UserId,
};
2024-06-24 23:22:03 +04:00
}
2024-06-26 08:04:07 +04:00
catch (Exception ex)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
_logger.LogError(ex, "Error update purchase");
return null;
2024-06-24 23:22:03 +04:00
}
}
2024-06-26 08:04:07 +04:00
[HttpGet]
public Dictionary<Guid, int> GetProducts(Guid productId)
{
return _purchaseLogic.GetProducts(new PurchaseSearchModel
{
Id = productId,
});
}
2024-06-24 23:22:03 +04:00
}
}