diff --git a/PyroFetes/DTO/Quotation/Request/CreateQuotationDto.cs b/PyroFetes/DTO/Quotation/Request/CreateQuotationDto.cs new file mode 100644 index 0000000..e775c9f --- /dev/null +++ b/PyroFetes/DTO/Quotation/Request/CreateQuotationDto.cs @@ -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? Products { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/DTO/Quotation/Response/GetQuotationDto.cs b/PyroFetes/DTO/Quotation/Response/GetQuotationDto.cs index 589197c..f77195f 100644 --- a/PyroFetes/DTO/Quotation/Response/GetQuotationDto.cs +++ b/PyroFetes/DTO/Quotation/Response/GetQuotationDto.cs @@ -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 { get; set; } + public List? Products { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/QuotationProduct/Request/CreateProductQuotationDto.cs b/PyroFetes/DTO/QuotationProduct/Request/CreateProductQuotationDto.cs new file mode 100644 index 0000000..51b0771 --- /dev/null +++ b/PyroFetes/DTO/QuotationProduct/Request/CreateProductQuotationDto.cs @@ -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; } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/PurchaseOrders/CreatePurchaseOrder.cs b/PyroFetes/Endpoints/PurchaseOrders/CreatePurchaseOrder.cs index 0fc433a..b3ca875 100644 --- a/PyroFetes/Endpoints/PurchaseOrders/CreatePurchaseOrder.cs +++ b/PyroFetes/Endpoints/PurchaseOrders/CreatePurchaseOrder.cs @@ -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() diff --git a/PyroFetes/Endpoints/Quotations/CreateQuotationEndpoint.cs b/PyroFetes/Endpoints/Quotations/CreateQuotationEndpoint.cs new file mode 100644 index 0000000..a7003c8 --- /dev/null +++ b/PyroFetes/Endpoints/Quotations/CreateQuotationEndpoint.cs @@ -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 +{ + 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() + }; + + 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(quotation), ct); + } +} \ No newline at end of file diff --git a/PyroFetes/MappingProfiles/DtoToEntityMappings.cs b/PyroFetes/MappingProfiles/DtoToEntityMappings.cs index ed46819..132c135 100644 --- a/PyroFetes/MappingProfiles/DtoToEntityMappings.cs +++ b/PyroFetes/MappingProfiles/DtoToEntityMappings.cs @@ -47,6 +47,7 @@ public class DtoToEntityMappings : Profile CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap(); diff --git a/PyroFetes/MappingProfiles/EntityToDtoMappings.cs b/PyroFetes/MappingProfiles/EntityToDtoMappings.cs index b1c6433..617891f 100644 --- a/PyroFetes/MappingProfiles/EntityToDtoMappings.cs +++ b/PyroFetes/MappingProfiles/EntityToDtoMappings.cs @@ -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(); + // CreateMap(); + // + // CreateMap(); - CreateMap(); + CreateMap() + .ForMember(dest => dest.Products, + opt => opt.MapFrom(src => src.QuotationProducts)); + + CreateMap() + .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();