From 554ba9b72503e97c6f58e438035be3704a484c64 Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 6 Nov 2025 15:54:53 +0100 Subject: [PATCH 1/5] Added all the endpoints needed for Deliverer --- .../Deliverers/CreateDelivererEndpoint.cs | 34 ++++++++++++++++ .../Deliverers/DeleteDelivererEndpoint.cs | 39 +++++++++++++++++++ .../Deliverers/GetAllDelivererEndpoint.cs | 29 ++++++++++++++ .../Deliverers/GetDelivererEndpoint.cs | 39 +++++++++++++++++++ .../Deliverers/UpdateDelivererEndpoint.cs | 39 +++++++++++++++++++ PyroFetes/PyroFetes.csproj | 1 + 6 files changed, 181 insertions(+) create mode 100644 PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs create mode 100644 PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs create mode 100644 PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs create mode 100644 PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs create mode 100644 PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs diff --git a/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs new file mode 100644 index 0000000..6bbe746 --- /dev/null +++ b/PyroFetes/Endpoints/Deliverers/CreateDelivererEndpoint.cs @@ -0,0 +1,34 @@ +using FastEndpoints; +using PyroFetes.DTO.Deliverer.Request; +using PyroFetes.DTO.Deliverer.Response; +using PyroFetes.Models; + +namespace PyroFetes.Endpoints.Deliverers; + +public class CreateDelivererEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("api/deliverers"); + AllowAnonymous(); + + } + + public override async Task HandleAsync(CreateDelivererDto req, CancellationToken ct) + { + Deliverer newDeliverer = new Deliverer() + { + Transporter = req.Transporter, + }; + + database.Deliverers.Add(newDeliverer); + + await database.SaveChangesAsync(ct); + + await Send.OkAsync(new GetDelivererDto() + { + Id = newDeliverer.Id, + Transporter = req.Transporter, + },ct); + } +} diff --git a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs new file mode 100644 index 0000000..5fdbcc9 --- /dev/null +++ b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs @@ -0,0 +1,39 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Deliverer.Request; +using PyroFetes.DTO.Deliverer.Response; +using PyroFetes.Models; + +namespace PyroFetes.Endpoints.Deliverers; + +public class DeleteDelivererRequest +{ + public int DelivererId { get; set; } +} +public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Put("api/deliverers/{id}", x=>new {x.DelivererId}); + AllowAnonymous(); + + } + + public override async Task HandleAsync(DeleteDelivererRequest req, CancellationToken ct) + { + Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.DelivererId, ct); + + if (deliverer == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Remove(deliverer); + + await database.SaveChangesAsync(ct); + + await Send.OkAsync(ct); + } + +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs new file mode 100644 index 0000000..47cafc6 --- /dev/null +++ b/PyroFetes/Endpoints/Deliverers/GetAllDelivererEndpoint.cs @@ -0,0 +1,29 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Scaffolding.Metadata; +using PyroFetes.DTO.Deliverer.Response; +using PyroFetes.Models; + +namespace PyroFetes.Endpoints.Deliverers; + +public class GetAllDelivererEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("api/deliverers"); + AllowAnonymous(); + + } + + public override async Task HandleAsync(CancellationToken ct) + { + List deliverers = await database.Deliverers.Select(x => new GetDelivererDto() + { + Id = x.Id, + Transporter = x.Transporter, + }).ToListAsync(ct); + + await Send.OkAsync(deliverers, ct); + } + +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs new file mode 100644 index 0000000..6995a78 --- /dev/null +++ b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs @@ -0,0 +1,39 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Deliverer.Response; +using PyroFetes.Models; + +namespace PyroFetes.Endpoints.Deliverers; + +public class GetDelivererRequest +{ + public int DelivererId { get; set; } +} + +public class GetDelivererEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("api/deliverers/{id}", x=>new {x.DelivererId}); + AllowAnonymous(); + + } + + public override async Task HandleAsync(GetDelivererRequest req, CancellationToken ct) + { + Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.DelivererId, ct); + + if (deliverer == null) + { + await Send.NotFoundAsync(ct); + return; + } + + await Send.OkAsync(new GetDelivererDto() + { + Id = deliverer.Id, + Transporter = deliverer.Transporter, + }, ct); + } + +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs new file mode 100644 index 0000000..1f65081 --- /dev/null +++ b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs @@ -0,0 +1,39 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Deliverer.Request; +using PyroFetes.DTO.Deliverer.Response; +using PyroFetes.Models; + +namespace PyroFetes.Endpoints.Deliverers; + +public class UpdateDelivererEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Put("api/deliverers/{id}", x=>new {x.Id}); + AllowAnonymous(); + + } + + public override async Task HandleAsync(UpdateDelivererDto req, CancellationToken ct) + { + Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.Id, ct); + + if (deliverer == null) + { + await Send.NotFoundAsync(ct); + return; + } + + deliverer.Transporter = req.Transporter; + + await database.SaveChangesAsync(ct); + + await Send.OkAsync(new GetDelivererDto() + { + Id = deliverer.Id, + Transporter = deliverer.Transporter, + }, ct); + } + +} \ No newline at end of file diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index 60e4770..f481032 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,6 +7,7 @@ + From c0090da9bdbd0018fb8c6d3cf7593562c88d8dab Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 6 Nov 2025 16:08:52 +0100 Subject: [PATCH 2/5] Fix unused imports --- PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs index 5fdbcc9..08c53d5 100644 --- a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs @@ -1,7 +1,5 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; -using PyroFetes.DTO.Deliverer.Request; -using PyroFetes.DTO.Deliverer.Response; using PyroFetes.Models; namespace PyroFetes.Endpoints.Deliverers; From 4bdd8104d8c07b4908395d07ba60f9e418fc5e1a Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 6 Nov 2025 16:11:37 +0100 Subject: [PATCH 3/5] Fix wrong HTTP method --- PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs index 08c53d5..399c91c 100644 --- a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs @@ -12,7 +12,7 @@ public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpointnew {x.DelivererId}); + Delete("api/deliverers/{id}", x=>new {x.DelivererId}); AllowAnonymous(); } From 9ff5c038b1dce5ca798ee7f9b49cc9d632c081b0 Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 6 Nov 2025 16:16:49 +0100 Subject: [PATCH 4/5] HOTFIX --- PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs | 2 +- PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs | 2 +- PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs index 399c91c..2c8e4a8 100644 --- a/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs @@ -12,7 +12,7 @@ public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpointnew {x.DelivererId}); + Delete("api/deliverers/{@id}", x=>new {x.DelivererId}); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs index 6995a78..35a4b69 100644 --- a/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/GetDelivererEndpoint.cs @@ -14,7 +14,7 @@ public class GetDelivererEndpoint(PyroFetesDbContext database) : Endpointnew {x.DelivererId}); + Get("api/deliverers/{@id}", x=>new {x.DelivererId}); AllowAnonymous(); } diff --git a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs index 1f65081..b8c7a5e 100644 --- a/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs +++ b/PyroFetes/Endpoints/Deliverers/UpdateDelivererEndpoint.cs @@ -10,7 +10,7 @@ public class UpdateDelivererEndpoint(PyroFetesDbContext database) : Endpointnew {x.Id}); + Put("api/deliverers/{@id}", x=>new {x.Id}); AllowAnonymous(); } From 6bc8281a374323dd552d03a4aacf995c499eb6c3 Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 6 Nov 2025 16:17:35 +0100 Subject: [PATCH 5/5] AddedAutoMapper --- PyroFetes/PyroFetes.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/PyroFetes/PyroFetes.csproj b/PyroFetes/PyroFetes.csproj index ec6d069..10361d5 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,6 +7,7 @@ +