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..2c8e4a8 --- /dev/null +++ b/PyroFetes/Endpoints/Deliverers/DeleteDelivererEndpoint.cs @@ -0,0 +1,37 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +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() + { + Delete("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..35a4b69 --- /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..b8c7a5e --- /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 ec6d069..10361d5 100644 --- a/PyroFetes/PyroFetes.csproj +++ b/PyroFetes/PyroFetes.csproj @@ -7,6 +7,7 @@ +