provider added
This commit is contained in:
@@ -2,5 +2,5 @@ namespace PyroFetes.DTO.Provider.Request;
|
||||
|
||||
public class CreateProviderDto
|
||||
{
|
||||
public int? Price { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
34
PyroFetes/Endpoints/Provider/CreateProviderEndpoint.cs
Normal file
34
PyroFetes/Endpoints/Provider/CreateProviderEndpoint.cs
Normal file
@@ -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<CreateProviderDto, GetProviderDto>
|
||||
{
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
30
PyroFetes/Endpoints/Provider/DeleteProviderEndpoint.cs
Normal file
30
PyroFetes/Endpoints/Provider/DeleteProviderEndpoint.cs
Normal file
@@ -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 <GetProviderRequest, GetProviderDto>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
25
PyroFetes/Endpoints/Provider/GetAllProvidersEndpoint.cs
Normal file
25
PyroFetes/Endpoints/Provider/GetAllProvidersEndpoint.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Provider.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Provider;
|
||||
|
||||
public class GetAllProvidersEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<GetProviderDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get ("/api/providers");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetProviderDto> customer= await pyroFetesDbContext.Providers.Select(x => new GetProviderDto()
|
||||
{
|
||||
Id = x.Id,
|
||||
Price = x.Price,
|
||||
}).ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(customer, ct);
|
||||
}
|
||||
}
|
||||
34
PyroFetes/Endpoints/Provider/GetProviderEndpoint.cs
Normal file
34
PyroFetes/Endpoints/Provider/GetProviderEndpoint.cs
Normal file
@@ -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 <GetProviderRequest, GetProviderDto>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
38
PyroFetes/Endpoints/Provider/UpdateProviderEndpoint.cs
Normal file
38
PyroFetes/Endpoints/Provider/UpdateProviderEndpoint.cs
Normal file
@@ -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 <UpdateProviderDto, GetProviderDto>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user