Refactor all code
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.DeliveryNote.Request;
|
||||
using PyroFetes.DTO.DeliveryNote.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Deliverers;
|
||||
@@ -12,8 +11,7 @@ public class CreateDeliveryNoteEndpoint(
|
||||
DeliveryNotesRepository deliveryNotesRepository,
|
||||
DeliverersRepository deliverersRepository,
|
||||
ProductsRepository productsRepository,
|
||||
ProductDeliveriesRepository productDeliveriesRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreateDeliveryNoteDto, GetDeliveryNoteDto>
|
||||
ProductDeliveriesRepository productDeliveriesRepository) : Endpoint<CreateDeliveryNoteDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -23,46 +21,44 @@ public class CreateDeliveryNoteEndpoint(
|
||||
|
||||
public override async Task HandleAsync(CreateDeliveryNoteDto req, CancellationToken ct)
|
||||
{
|
||||
Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.DelivererId), ct);
|
||||
|
||||
if (deliverer == null)
|
||||
Deliverer? deliverer = await deliverersRepository.SingleOrDefaultAsync(new GetDelivererByIdSpec(req.DelivererId), ct);
|
||||
if (deliverer is null)
|
||||
{
|
||||
await Send.StringAsync("No deliverer found", 404, cancellation: ct);
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//Creating the Delivery Note
|
||||
DeliveryNote newDeliveryNote = new DeliveryNote()
|
||||
DeliveryNote newDeliveryNote = new()
|
||||
{
|
||||
TrackingNumber = req.TrackingNumber,
|
||||
EstimateDeliveryDate = req.EstimateDeliveryDate,
|
||||
ExpeditionDate = req.ExpeditionDate,
|
||||
DelivererId = req.DelivererId,
|
||||
ExpeditionDate = req.ExpeditionDate,
|
||||
DelivererId = deliverer.Id,
|
||||
Deliverer = deliverer,
|
||||
|
||||
};
|
||||
|
||||
|
||||
await deliveryNotesRepository.AddAsync(newDeliveryNote, ct);
|
||||
|
||||
foreach (var productQuantity in req.ProductQuantities!)
|
||||
if (req.ProductQuantities is not null)
|
||||
{
|
||||
Product? product =
|
||||
await productsRepository.FirstOrDefaultAsync(new GetProductByIdSpec(productQuantity.Key), ct);
|
||||
if (product != null)
|
||||
foreach (KeyValuePair<int, int> productQuantity in req.ProductQuantities)
|
||||
{
|
||||
ProductDelivery productDelivery = new ProductDelivery()
|
||||
Product? product = await productsRepository.SingleOrDefaultAsync(new GetProductByIdSpec(productQuantity.Key), ct);
|
||||
if (product is null) continue;
|
||||
ProductDelivery productDelivery = new()
|
||||
{
|
||||
DeliveryNote = newDeliveryNote,
|
||||
Quantity = productQuantity.Value,
|
||||
Product = product,
|
||||
ProductId = product.Id,
|
||||
DeliveryNoteId = newDeliveryNote.Id
|
||||
};
|
||||
|
||||
|
||||
await productDeliveriesRepository.AddAsync(productDelivery, ct);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetDeliveryNoteDto>(newDeliveryNote), ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.Endpoints.Quotations;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.DeliveryNotes;
|
||||
using PyroFetes.Specifications.Quotations;
|
||||
|
||||
namespace PyroFetes.Endpoints.DeliveryNotes;
|
||||
|
||||
@@ -17,22 +15,21 @@ public class DeleteDeliveryNoteEndpoint(
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/deliveryNotes/{@Id}", x => new {x.Id});
|
||||
Delete("/deliveryNotes/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteDeliveryNoteRequest req, CancellationToken ct)
|
||||
{
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository.FirstOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id), ct);
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository.SingleOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id), ct);
|
||||
|
||||
if (deliveryNote == null)
|
||||
if (deliveryNote is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await deliveryNotesRepository.DeleteAsync(deliveryNote, ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.DeliveryNote.Response;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.DeliveryNotes;
|
||||
|
||||
namespace PyroFetes.Endpoints.DeliveryNotes;
|
||||
|
||||
@@ -14,7 +15,6 @@ public class GetAllDeliveryNoteEndpoint(DeliveryNotesRepository deliveryNotesRep
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
await Send.OkAsync(await deliveryNotesRepository.ProjectToListAsync<GetDeliveryNoteDto>(ct), ct);
|
||||
await Send.OkAsync(await deliveryNotesRepository.ProjectToListAsync<GetDeliveryNoteDto>(new GetAllDeliveryNoteSpec() ,ct), ct);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,26 +10,27 @@ public class GetDeliveryNoteRequest
|
||||
{
|
||||
public int DeliveryNoteId { get; set; }
|
||||
}
|
||||
|
||||
public class GetDeliveryNoteEndpoint(
|
||||
DeliveryNotesRepository deliveryNotesRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<GetDeliveryNoteRequest, GetDeliveryNoteDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/deliveryNotes/{@id}", x=> new {x.DeliveryNoteId});
|
||||
Get("/deliveryNotes/{@Id}", x => new { x.DeliveryNoteId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetDeliveryNoteRequest req, CancellationToken ct)
|
||||
{
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository.FirstOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.DeliveryNoteId), ct);
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository.SingleOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.DeliveryNoteId), ct);
|
||||
|
||||
if (deliveryNote == null)
|
||||
if (deliveryNote is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetDeliveryNoteDto>(deliveryNote), ct);
|
||||
}
|
||||
}
|
||||
@@ -5,39 +5,37 @@ using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Services.Pdf;
|
||||
using PyroFetes.Specifications.DeliveryNotes;
|
||||
using PyroFetes.Specifications.Quotations;
|
||||
|
||||
namespace PyroFetes.Endpoints.DeliveryNotes;
|
||||
|
||||
public class GetDeliveryNotePdfEndpoint(
|
||||
DeliveryNotesRepository deliveryNotesRepository,
|
||||
IDeliveryNotePdfService deliveryNotePdfService)
|
||||
: Endpoint<GetDeliveryNotePdfDto, byte[]>
|
||||
public class GetDeliveryNotePdfEndpoint(DeliveryNotesRepository deliveryNotesRepository, IDeliveryNotePdfService deliveryNotePdfService, SettingsRepository settingsRepository) : Endpoint<GetDeliveryNotePdfDto, byte[]>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/deliveryNotes/{@Id}/pdf", x => new {x.Id});
|
||||
Get("/deliveryNotes/{@Id}/pdf", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
Description(b => b.Produces<byte[]>(200, MediaTypeNames.Application.Pdf));
|
||||
}
|
||||
|
||||
|
||||
public override async Task HandleAsync(GetDeliveryNotePdfDto req, CancellationToken ct)
|
||||
{
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository
|
||||
.FirstOrDefaultAsync(new GetDeliveryNoteByIdWithProductsSpec(req.Id), ct);
|
||||
.SingleOrDefaultAsync(new GetDeliveryNoteByIdWithProductsSpec(req.Id), ct);
|
||||
|
||||
if (deliveryNote == null)
|
||||
Setting? setting = await settingsRepository.FirstOrDefaultAsync(ct);
|
||||
|
||||
if (deliveryNote is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var bytes = deliveryNotePdfService.Generate(deliveryNote, deliveryNote.ProductDeliveries!);
|
||||
|
||||
byte[] bytes = deliveryNotePdfService.Generate(deliveryNote, deliveryNote.ProductDeliveries!, setting!);
|
||||
|
||||
await Send.BytesAsync(
|
||||
bytes: bytes,
|
||||
contentType: "application/pdf",
|
||||
fileName: $"bon-de-livraison-{deliveryNote.Id}.pdf",
|
||||
fileName: $"bon-de-livraison-{deliveryNote.Id}{DateOnly.FromDateTime(DateTime.Now)}.pdf",
|
||||
cancellation: ct);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +1,41 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.DeliveryNote.Request;
|
||||
using PyroFetes.DTO.DeliveryNote.Response;
|
||||
using PyroFetes.DTO.PurchaseProduct.Request;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Deliverers;
|
||||
using PyroFetes.Specifications.DeliveryNotes;
|
||||
|
||||
namespace PyroFetes.Endpoints.DeliveryNotes;
|
||||
|
||||
public class PatchRealDeliveryDateEndpoint(
|
||||
DeliveryNotesRepository deliveryNotesRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchDeliveryNoteRealDeliveryDateDto, GetDeliveryNoteDto>
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchDeliveryNoteRealDeliveryDateDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/deliveryNotes/{@id}", x=> new {x.Id});
|
||||
Patch("/deliveryNotes/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(PatchDeliveryNoteRealDeliveryDateDto req, CancellationToken ct)
|
||||
{
|
||||
DeliveryNote? deliveryNoteToPath =
|
||||
await deliveryNotesRepository.FirstOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id),ct);
|
||||
DeliveryNote? deliveryNoteToPath = await deliveryNotesRepository.SingleOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id), ct);
|
||||
|
||||
if (deliveryNoteToPath == null)
|
||||
if (deliveryNoteToPath is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (deliveryNoteToPath.RealDeliveryDate != null)
|
||||
|
||||
if (deliveryNoteToPath.RealDeliveryDate is not null)
|
||||
{
|
||||
await Send.StringAsync("Impossible de modifier la date.", 400);
|
||||
await Send.StringAsync("Impossible de modifier la date.", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
deliveryNoteToPath.RealDeliveryDate = req.RealDeliveryDate;
|
||||
|
||||
|
||||
mapper.Map(req, deliveryNoteToPath);
|
||||
|
||||
await deliveryNotesRepository.UpdateAsync(deliveryNoteToPath, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetDeliveryNoteDto>(deliveryNoteToPath), ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,32 @@
|
||||
using FastEndpoints;
|
||||
using PyroFetes.DTO.DeliveryNote.Request;
|
||||
using PyroFetes.DTO.DeliveryNote.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.DeliveryNotes;
|
||||
|
||||
namespace PyroFetes.Endpoints.DeliveryNotes;
|
||||
|
||||
public class UpdateDeliveryNoteEndpoint(
|
||||
DeliveryNotesRepository deliveryNotesRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<UpdateDeliveryNoteDto, GetDeliveryNoteDto>
|
||||
public class UpdateDeliveryNoteEndpoint(DeliveryNotesRepository deliveryNotesRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateDeliveryNoteDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/deliveryNotes/{@Id}", x => new {x.Id});
|
||||
Put("/deliveryNotes/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateDeliveryNoteDto req, CancellationToken ct)
|
||||
{
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository.FirstOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id), ct);
|
||||
|
||||
if (deliveryNote == null)
|
||||
DeliveryNote? deliveryNote = await deliveryNotesRepository.SingleOrDefaultAsync(new GetDeliveryNoteByIdSpec(req.Id), ct);
|
||||
|
||||
if (deliveryNote is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
deliveryNote.TrackingNumber = req.TrackingNumber;
|
||||
deliveryNote.EstimateDeliveryDate = req.EstimateDeliveryDate;
|
||||
deliveryNote.ExpeditionDate = req.ExpeditionDate;
|
||||
deliveryNote.RealDeliveryDate = req.RealDeliveryDate;
|
||||
deliveryNote.DelivererId = req.DelivererId;
|
||||
|
||||
await deliveryNotesRepository.UpdateAsync(deliveryNote, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetDeliveryNoteDto>(deliveryNote), ct);
|
||||
|
||||
mapper.Map(req, deliveryNote);
|
||||
|
||||
await deliveryNotesRepository.SaveChangesAsync(ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user