added CreateQuotationEndpoint.cs

This commit is contained in:
2025-12-13 15:08:18 +01:00
parent 0802ec9e33
commit be7a3193ab
7 changed files with 83 additions and 4 deletions

View File

@@ -0,0 +1,10 @@
using PyroFetes.DTO.QuotationProduct.Request;
namespace PyroFetes.DTO.Quotation.Request;
public class CreateQuotationDto
{
public string? Message { get; set; }
public string? ConditionsSale { get; set; }
public List<CreateProductQuotationDto>? Products { get; set; }
}

View File

@@ -7,5 +7,5 @@ public class GetQuotationDto
public int Id { get; set; }
public string? Message { get; set; }
public string? ConditionsSale { get; set; }
public List<GetQuotationProductDto>? GetQuotationProductDto { get; set; }
public List<GetQuotationProductDto>? Products { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace PyroFetes.DTO.QuotationProduct.Request;
// Pour création global
public class CreateProductQuotationDto
{
public int ProductId { get; set; }
public int Quantity { get; set; }
}

View File

@@ -23,7 +23,7 @@ public class CreatePurchaseOrder(
public override async Task HandleAsync(CreatePurchaseOrderDto req, CancellationToken ct)
{
var purchaseOrder = new PurchaseOrder
PurchaseOrder purchaseOrder = new PurchaseOrder
{
PurchaseConditions = req.PurchaseConditions ?? "Conditions non précisées",
PurchaseProducts = new List<PurchaseProduct>()

View File

@@ -0,0 +1,50 @@
using FastEndpoints;
using PyroFetes.DTO.Quotation.Request;
using PyroFetes.DTO.Quotation.Response;
using PyroFetes.Models;
using PyroFetes.Repositories;
namespace PyroFetes.Endpoints.Quotations;
public class CreateQuotationEndpoint(
QuotationsRepository quotationsRepository,
ProductsRepository productsRepository,
AutoMapper.IMapper mapper) : Endpoint<CreateQuotationDto, GetQuotationDto>
{
public override void Configure()
{
Post("/quotations");
AllowAnonymous();
}
public override async Task HandleAsync(CreateQuotationDto req, CancellationToken ct)
{
Quotation quotation = new Quotation
{
Message = req.Message,
ConditionsSale = req.ConditionsSale ?? "Conditions non précisées",
CustomerId = 1, // A changer
QuotationProducts = new List<QuotationProduct>()
};
foreach (var line in req.Products)
{
var product = await productsRepository.GetByIdAsync(line.ProductId, ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
return;
}
quotation.QuotationProducts.Add(new QuotationProduct
{
ProductId = product.Id,
Quantity = line.Quantity,
});
}
await quotationsRepository.AddAsync(quotation, ct);
await Send.OkAsync(mapper.Map<GetQuotationDto>(quotation), ct);
}
}

View File

@@ -47,6 +47,7 @@ public class DtoToEntityMappings : Profile
CreateMap<PatchQuotationConditionsSaleDto, Quotation>();
CreateMap<PatchQuotationMessageDto, Quotation>();
CreateMap<CreateProductQuotationDto, Quotation>();
CreateMap<CreateQuotationProductDto, QuotationProduct>();
CreateMap<UpdateQuotationProductDto, QuotationProduct>();

View File

@@ -46,9 +46,19 @@ public class EntityToDtoMappings : Profile
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.Product.Name))
.ForMember(dest => dest.ProductReferences, opt => opt.MapFrom(src => src.Product.Reference));
CreateMap<Quotation, GetQuotationDto>();
// CreateMap<Quotation, GetQuotationDto>();
//
// CreateMap<QuotationProduct, GetQuotationProductDto>();
CreateMap<QuotationProduct, GetQuotationProductDto>();
CreateMap<Quotation, GetQuotationDto>()
.ForMember(dest => dest.Products,
opt => opt.MapFrom(src => src.QuotationProducts));
CreateMap<QuotationProduct, GetQuotationProductDto>()
.ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.ProductId))
.ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quantity))
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.Product.Name))
.ForMember(dest => dest.ProductReferences, opt => opt.MapFrom(src => src.Product.Reference));
CreateMap<Setting, GetSettingDto>();