137 lines
4.1 KiB
C#
137 lines
4.1 KiB
C#
using BusinessLogic.BusinessLogic;
|
|
using Contracts.BindingModels;
|
|
using Contracts.BusinessLogicContracts;
|
|
using Contracts.Exceptions;
|
|
using Contracts.SearchModels;
|
|
using Contracts.ViewModels;
|
|
using DatabaseImplement.Models;
|
|
using DataModels.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace RestAPI.Controllers
|
|
{
|
|
[Route("[controller]/[action]")]
|
|
[ApiController]
|
|
public class PurchaseController : Controller
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IPurchaseLogic _purchaseLogic;
|
|
private readonly IBillLogic _billLogic;
|
|
|
|
public PurchaseController(ILogger<PurchaseController> logger, IPurchaseLogic purchaseLogic, IBillLogic billLogic)
|
|
{
|
|
_logger = logger;
|
|
_purchaseLogic = purchaseLogic;
|
|
_billLogic = billLogic;
|
|
}
|
|
[HttpGet]
|
|
public List<PurchaseViewModel>? GetList(Guid id, Guid userId, double? costfrom, double? costto, DateTime? datefrom, DateTime? dateto)
|
|
{
|
|
try
|
|
{
|
|
if (id == Guid.Empty && userId == Guid.Empty && costfrom == null && costto == null && datefrom == null && dateto == null)
|
|
return _purchaseLogic.ReadElements(null);
|
|
|
|
else
|
|
return _purchaseLogic.ReadElements(new PurchaseSearchModel()
|
|
{
|
|
Id = id,
|
|
UserId = userId,
|
|
CostFrom = costfrom,
|
|
CostTo = costto,
|
|
DateFrom = datefrom,
|
|
DateTo = dateto
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка покупок");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public PurchaseViewModel Get(Guid id)
|
|
{
|
|
try
|
|
{
|
|
return _purchaseLogic.ReadElement(new PurchaseSearchModel() { Id = id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения покупки");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
public List<ProductViewModel> GetProducts(Guid purchaseid)
|
|
{
|
|
try
|
|
{
|
|
return _purchaseLogic.GetProducts(new PurchaseSearchModel() { Id = purchaseid });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения продукта");
|
|
throw;
|
|
}
|
|
}
|
|
[HttpPost]
|
|
public IResult Create([FromBody] PurchaseBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
var purchase = _purchaseLogic.Create(model);
|
|
|
|
return Results.Ok(purchase);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error create purchase");
|
|
return Results.Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IResult CreateBill([FromBody] BillViewModel model)
|
|
{
|
|
try
|
|
{
|
|
_billLogic.CreateBill(model);
|
|
|
|
return Results.Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error create bill");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[HttpPatch]
|
|
public PurchaseViewModel Update([FromBody] PurchaseBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
var res = _purchaseLogic.Update(model);
|
|
return new PurchaseViewModel()
|
|
{
|
|
Id = res.Id,
|
|
Cost = res.Cost,
|
|
DateClosed = res.DateClosed,
|
|
UserId = res.UserId,
|
|
};
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error update purchase");
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|