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;
|
2024-07-26 02:46:29 +04:00
|
|
|
|
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;
|
2024-07-30 18:41:27 +04:00
|
|
|
|
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;
|
2024-07-26 02:46:29 +04:00
|
|
|
|
private readonly IBillLogic _billLogic;
|
2024-06-24 23:22:03 +04:00
|
|
|
|
|
2024-07-26 02:46:29 +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;
|
2024-07-26 02:46:29 +04:00
|
|
|
|
_billLogic = billLogic;
|
2024-06-24 23:22:03 +04:00
|
|
|
|
}
|
2024-06-26 08:04:07 +04:00
|
|
|
|
[HttpGet]
|
2024-07-26 02:46:29 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
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()
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
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
|
|
|
|
{
|
2024-07-26 02:46:29 +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)
|
2024-07-26 02:46:29 +04:00
|
|
|
|
{
|
|
|
|
|
_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]
|
2024-07-26 02:46:29 +04:00
|
|
|
|
public IResult Create([FromBody] PurchaseBindingModel model)
|
2024-06-24 23:22:03 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
var purchase = _purchaseLogic.Create(model);
|
2024-06-26 08:04:07 +04:00
|
|
|
|
|
2024-07-26 02:46:29 +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
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
_logger.LogError(ex, "Error create purchase");
|
|
|
|
|
return Results.Problem(ex.Message);
|
2024-06-24 23:22:03 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-26 02:46:29 +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
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
_billLogic.CreateBill(model);
|
2024-06-24 23:22:03 +04:00
|
|
|
|
|
2024-07-26 02:46:29 +04:00
|
|
|
|
return Results.Ok();
|
|
|
|
|
}
|
2024-06-26 08:04:07 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
_logger.LogError(ex, "Error create bill");
|
|
|
|
|
throw;
|
2024-06-26 08:04:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-26 02:46:29 +04:00
|
|
|
|
|
2024-06-26 08:04:07 +04:00
|
|
|
|
[HttpPatch]
|
2024-07-26 02:46:29 +04:00
|
|
|
|
public PurchaseViewModel Update([FromBody] PurchaseBindingModel model)
|
2024-06-24 23:22:03 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-26 02:46:29 +04:00
|
|
|
|
var res = _purchaseLogic.Update(model);
|
2024-06-26 08:04:07 +04:00
|
|
|
|
return new PurchaseViewModel()
|
2024-07-26 02:46:29 +04:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-30 18:41:27 +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
|
|
|
|
}
|
|
|
|
|
}
|