provider added

This commit is contained in:
2025-12-04 15:56:02 +01:00
parent 604dd77fed
commit fc736e8525
8 changed files with 164 additions and 3 deletions

View File

@@ -2,5 +2,5 @@ namespace PyroFetes.DTO.Provider.Request;
public class CreateProviderDto
{
public int? Price { get; set; }
public decimal Price { get; set; }
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}