diff --git a/PyroFetes/DTO/Provider/Request/CreateProviderDto.cs b/PyroFetes/DTO/Provider/Request/CreateProviderDto.cs index 2f548d9..ea0df6a 100644 --- a/PyroFetes/DTO/Provider/Request/CreateProviderDto.cs +++ b/PyroFetes/DTO/Provider/Request/CreateProviderDto.cs @@ -2,5 +2,5 @@ namespace PyroFetes.DTO.Provider.Request; public class CreateProviderDto { - public int? Price { get; set; } + public decimal Price { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Provider/Request/UpdateProviderDto.cs b/PyroFetes/DTO/Provider/Request/UpdateProviderDto.cs index 8b7c0a6..216dc68 100644 --- a/PyroFetes/DTO/Provider/Request/UpdateProviderDto.cs +++ b/PyroFetes/DTO/Provider/Request/UpdateProviderDto.cs @@ -3,5 +3,5 @@ namespace PyroFetes.DTO.Provider.Request; public class UpdateProviderDto { public int Id { get; set; } - public int? Price { get; set; } + public decimal Price { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Provider/Response/GetProviderDto.cs b/PyroFetes/DTO/Provider/Response/GetProviderDto.cs index 1a5c3ca..99eeab0 100644 --- a/PyroFetes/DTO/Provider/Response/GetProviderDto.cs +++ b/PyroFetes/DTO/Provider/Response/GetProviderDto.cs @@ -3,5 +3,5 @@ namespace PyroFetes.DTO.Provider.Response; public class GetProviderDto { public int Id { get; set; } - public int? Price { get; set; } + public decimal Price { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Provider/CreateProviderEndpoint.cs b/PyroFetes/Endpoints/Provider/CreateProviderEndpoint.cs new file mode 100644 index 0000000..f7f3e99 --- /dev/null +++ b/PyroFetes/Endpoints/Provider/CreateProviderEndpoint.cs @@ -0,0 +1,34 @@ +using FastEndpoints; +using PyroFetes.DTO.Provider.Request; +using PyroFetes.DTO.Provider.Response; + +namespace PyroFetes.Endpoints.Provider; + +public class CreateProviderEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + public override void Configure() + { + Post("/api/providers"); + AllowAnonymous(); + } + + + public override async Task HandleAsync(CreateProviderDto req, CancellationToken ct) + { + var provider = new Models.ServiceProvider() + { + Price = req.Price + }; + pyroFetesDbContext.Add(provider); + await pyroFetesDbContext.SaveChangesAsync(ct); + + GetProviderDto response = new GetProviderDto() + { + Id = provider.Id, + Price = provider.Price + }; + + await Send.OkAsync(response, ct); + + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Provider/DeleteProviderEndpoint.cs b/PyroFetes/Endpoints/Provider/DeleteProviderEndpoint.cs new file mode 100644 index 0000000..7668675 --- /dev/null +++ b/PyroFetes/Endpoints/Provider/DeleteProviderEndpoint.cs @@ -0,0 +1,30 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Provider.Request; +using PyroFetes.DTO.Provider.Response; + +namespace PyroFetes.Endpoints.Provider; + +public class DeleteProviderEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + public override void Configure() + { + Delete ("/api/providers/{@Id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(GetProviderRequest req, CancellationToken ct) + { + Models.ServiceProvider? databaseProvider = await pyroFetesDbContext.Providers.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseProvider == null) + { + await Send.NotFoundAsync(ct); + return; + } + pyroFetesDbContext.Providers.Remove(databaseProvider); + await pyroFetesDbContext.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Provider/GetAllProvidersEndpoint.cs b/PyroFetes/Endpoints/Provider/GetAllProvidersEndpoint.cs new file mode 100644 index 0000000..0ab3848 --- /dev/null +++ b/PyroFetes/Endpoints/Provider/GetAllProvidersEndpoint.cs @@ -0,0 +1,25 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Provider.Response; + +namespace PyroFetes.Endpoints.Provider; + +public class GetAllProvidersEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get ("/api/providers"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CancellationToken ct) + { + List customer= await pyroFetesDbContext.Providers.Select(x => new GetProviderDto() + { + Id = x.Id, + Price = x.Price, + }).ToListAsync(ct); + + await Send.OkAsync(customer, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Provider/GetProviderEndpoint.cs b/PyroFetes/Endpoints/Provider/GetProviderEndpoint.cs new file mode 100644 index 0000000..ecad2f8 --- /dev/null +++ b/PyroFetes/Endpoints/Provider/GetProviderEndpoint.cs @@ -0,0 +1,34 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Provider.Request; +using PyroFetes.DTO.Provider.Response; + +namespace PyroFetes.Endpoints.Provider; + +public class GetProviderEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + public override void Configure() + { + Get ("/api/providers/{@Id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(GetProviderRequest req, CancellationToken ct) + { + Models.ServiceProvider? databaseProvider = await pyroFetesDbContext.Providers.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseProvider == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetProviderDto dto = new() + { + Id = databaseProvider.Id, + Price = databaseProvider.Price, + }; + + await Send.OkAsync(dto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Provider/UpdateProviderEndpoint.cs b/PyroFetes/Endpoints/Provider/UpdateProviderEndpoint.cs new file mode 100644 index 0000000..9b425a9 --- /dev/null +++ b/PyroFetes/Endpoints/Provider/UpdateProviderEndpoint.cs @@ -0,0 +1,38 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Provider.Request; +using PyroFetes.DTO.Provider.Response; + +namespace PyroFetes.Endpoints.Provider; + +public class UpdateProviderEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint +{ + public override void Configure() + { + Put ("/api/providers/{@Id}", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(UpdateProviderDto req, CancellationToken ct) + { + Models.ServiceProvider? databaseProvider = await pyroFetesDbContext.Providers.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct); + + if (databaseProvider == null) + { + await Send.NotFoundAsync(ct); + return; + } + else + { + databaseProvider.Price = req.Price; + } + await pyroFetesDbContext.SaveChangesAsync(ct); + + GetProviderDto dto = new() + { + Id = databaseProvider.Id, + }; + + await Send.OkAsync(dto, ct); + } +} \ No newline at end of file