Provider typê added + delete customer updated

This commit is contained in:
2025-12-04 16:31:52 +01:00
parent fc736e8525
commit a554693e9c
6 changed files with 166 additions and 7 deletions

View File

@@ -1,13 +1,11 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Customer.Request;
using PyroFetes.DTO.Customer.Response;
using PyroFetes.DTO.CustomerType.Request;
using PyroFetes.DTO.CustomerType.Response;
namespace PyroFetes.Endpoints.CustomerType;
public class DeleteCustomerTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetCustomerRequest, GetCustomerDto>
public class DeleteCustomerTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetCustomerTypeRequest, GetCustomerTypeDto>
{
public override void Configure()
{
@@ -15,16 +13,16 @@ public class DeleteCustomerTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) :
AllowAnonymous();
}
public override async Task HandleAsync(GetCustomerRequest req, CancellationToken ct)
public override async Task HandleAsync(GetCustomerTypeRequest req, CancellationToken ct)
{
Models.Customer? databaseCustomer = await pyroFetesDbContext.Customers.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
Models.CustomerType? databaseCustomerType = await pyroFetesDbContext.CustomerTypes.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseCustomer == null)
if (databaseCustomerType == null)
{
await Send.NotFoundAsync(ct);
return;
}
pyroFetesDbContext.Customers.Remove(databaseCustomer);
pyroFetesDbContext.CustomerTypes.Remove(databaseCustomerType);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);

View File

@@ -0,0 +1,34 @@
using FastEndpoints;
using PyroFetes.DTO.ProviderType.Request;
using PyroFetes.DTO.ProviderType.Response;
namespace PyroFetes.Endpoints.ProviderType;
public class CreateProviderTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateProviderTypeDto, GetProviderTypeDto>
{
public override void Configure()
{
Post("/api/providertypes");
AllowAnonymous();
}
public override async Task HandleAsync(CreateProviderTypeDto req, CancellationToken ct)
{
var providerType = new Models.ProviderType
{
Label = req.Label
};
pyroFetesDbContext.Add(providerType);
await pyroFetesDbContext.SaveChangesAsync(ct);
GetProviderTypeDto response = new GetProviderTypeDto()
{
Id = providerType.Id,
Label = providerType.Label
};
await Send.OkAsync(response, ct);
}
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.ProviderType.Request;
using PyroFetes.DTO.ProviderType.Response;
namespace PyroFetes.Endpoints.ProviderType;
public class DeleteProviderTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetProviderTypeRequest, GetProviderTypeDto>
{
public override void Configure()
{
Delete ("/api/providertypes/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetProviderTypeRequest req, CancellationToken ct)
{
Models.ProviderType? databaseProviderType = await pyroFetesDbContext.ProviderTypes.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseProviderType == null)
{
await Send.NotFoundAsync(ct);
return;
}
pyroFetesDbContext.ProviderTypes.Remove(databaseProviderType);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,25 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.ProviderType.Response;
namespace PyroFetes.Endpoints.ProviderType;
public class GetAllProviderTypesEndpoint(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<GetProviderTypeDto>>
{
public override void Configure()
{
Get ("/api/providertype");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
List<GetProviderTypeDto> providerType= await pyroFetesDbContext.ProviderTypes.Select(x => new GetProviderTypeDto()
{
Id = x.Id,
Label = x.Label,
}).ToListAsync(ct);
await Send.OkAsync(providerType, ct);
}
}

View File

@@ -0,0 +1,34 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.ProviderType.Request;
using PyroFetes.DTO.ProviderType.Response;
namespace PyroFetes.Endpoints.ProviderType;
public class GetProviderTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetProviderTypeRequest, GetProviderTypeDto>
{
public override void Configure()
{
Get ("/api/providertype/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetProviderTypeRequest req, CancellationToken ct)
{
Models.ProviderType? databaseProviderType = await pyroFetesDbContext.ProviderTypes.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseProviderType == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetProviderTypeDto dto = new()
{
Id = databaseProviderType.Id,
Label = databaseProviderType.Label,
};
await Send.OkAsync(dto, ct);
}
}

View File

@@ -0,0 +1,38 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.ProviderType.Request;
using PyroFetes.DTO.ProviderType.Response;
namespace PyroFetes.Endpoints.ProviderType;
public class UpdateProviderTypeEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <UpdateProviderTypeDto, GetProviderTypeDto>
{
public override void Configure()
{
Put ("/api/providertypes/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateProviderTypeDto req, CancellationToken ct)
{
Models.ProviderType? databaseProviderType = await pyroFetesDbContext.ProviderTypes.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseProviderType == null)
{
await Send.NotFoundAsync(ct);
return;
}
else
{
databaseProviderType.Label = req.Label;
}
await pyroFetesDbContext.SaveChangesAsync(ct);
GetProviderTypeDto dto = new()
{
Id = databaseProviderType.Id,
};
await Send.OkAsync(dto, ct);
}
}