PIAPS_CW/RestAPI/Controllers/PurchaseController.cs

160 lines
4.9 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 DataModels.Models;
2024-06-26 08:04:07 +04:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
2024-06-24 23:22:03 +04:00
using Microsoft.AspNetCore.Mvc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
2024-06-24 23:22:03 +04:00
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;
private readonly IBillLogic _billLogic;
2024-06-24 23:22:03 +04:00
public PurchaseController(ILogger<PurchaseController> logger, IPurchaseLogic purchaseLogic, IBillLogic billLogic)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
_logger = logger;
_purchaseLogic = purchaseLogic;
_billLogic = billLogic;
2024-06-24 23:22:03 +04:00
}
2024-06-26 08:04:07 +04:00
[HttpGet]
public List<PurchaseViewModel>? GetList(Guid id, Guid userId, double? costfrom, double? costto, DateTime? datefrom, DateTime? dateto)
2024-06-24 23:22:03 +04:00
{
try
{
if (id == Guid.Empty && userId == Guid.Empty && costfrom == null && costto == null && datefrom == null && dateto == null)
2024-06-26 08:04:07 +04:00
return _purchaseLogic.ReadElements(null);
else
return _purchaseLogic.ReadElements(new PurchaseSearchModel()
{
Id = id,
UserId = userId,
2024-06-26 08:04:07 +04:00
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
{
_logger.LogError(ex, "Ошибка получения списка покупок");
2024-06-26 08:04:07 +04:00
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;
}
}
[HttpGet]
public List<ProductViewModel> GetProducts(Guid purchaseid)
{
try
{
return _purchaseLogic.GetProducts(new PurchaseSearchModel() { Id = purchaseid });
}
catch (Exception ex)
2024-06-26 08:04:07 +04:00
{
_logger.LogError(ex, "Ошибка получения продукта");
throw;
}
2024-06-24 23:22:03 +04:00
}
[HttpPost]
public IResult Create([FromBody] PurchaseBindingModel model)
2024-06-24 23:22:03 +04:00
{
try
{
var purchase = _purchaseLogic.Create(model);
2024-06-26 08:04:07 +04:00
return Results.Ok(purchase);
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
{
_logger.LogError(ex, "Error create purchase");
return Results.Problem(ex.Message);
2024-06-24 23:22:03 +04:00
}
}
[HttpPost]
public IResult CreateBill([FromBody] BillViewModel model)
2024-06-24 23:22:03 +04:00
{
2024-06-26 08:04:07 +04:00
try
{
_billLogic.CreateBill(model);
2024-06-24 23:22:03 +04:00
return Results.Ok();
}
2024-06-26 08:04:07 +04:00
catch (Exception ex)
{
_logger.LogError(ex, "Error create bill");
throw;
2024-06-26 08:04:07 +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
{
try
{
var res = _purchaseLogic.Update(model);
2024-06-26 08:04:07 +04:00
return new PurchaseViewModel()
{
Id = res.Id,
Cost = res.Cost,
DateClosed = res.DateClosed,
UserId = res.UserId,
2024-06-26 08:04:07 +04:00
};
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
}
}
[HttpGet]
public byte[] CreateChart(Guid userId)
{
Document document = new Document();
Section section = document.AddSection();
section.AddParagraph("Столбчатая диаграмма");
using (MemoryStream stream = new MemoryStream())
{
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();
// Сохранение документа в MemoryStream
renderer.PdfDocument.Save(stream, false);
// Получение массива байтов из MemoryStream
return stream.ToArray();
}
}
2024-06-24 23:22:03 +04:00
}
}