76 lines
2.6 KiB
C#
76 lines
2.6 KiB
C#
using GerstITS.Examples.Logic.Customers;
|
|
using GerstITS.Validation;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace GerstITS.Examples.Api.Endpoints;
|
|
|
|
internal static partial class Endpoints
|
|
{
|
|
internal static RouteGroupBuilder MapCustomerEndpoints(this IEndpointRouteBuilder root)
|
|
{
|
|
var group = root.MapGroup("/api/v2.0")
|
|
.WithTags("Customers");
|
|
|
|
Customers.MapGetById(group);
|
|
Customers.MapSearch(group);
|
|
Customers.MapCreate(group);
|
|
Customers.MapUpdate(group);
|
|
Customers.MapDelete(group);
|
|
Customers.MapImport(group);
|
|
|
|
return group;
|
|
}
|
|
|
|
internal static class Customers
|
|
{
|
|
internal static void MapGetById(RouteGroupBuilder group)
|
|
{
|
|
group.MapGet("customers/{id:int}", (int id, ICustomerProvider customerProvider, bool includeInactive = false) =>
|
|
Results.Ok(customerProvider.GetById(id, includeInactive)));
|
|
}
|
|
|
|
internal static void MapSearch(RouteGroupBuilder group)
|
|
{
|
|
group.MapGet("customers", ([AsParameters] CustomerSearchFilter filter, ICustomerProvider customerProvider, IValidator validator) =>
|
|
{
|
|
Validator.Validate(validator, filter);
|
|
|
|
return Results.Ok(customerProvider.Search(filter));
|
|
});
|
|
}
|
|
|
|
internal static void MapCreate(RouteGroupBuilder group)
|
|
{
|
|
group.MapPost("customers", ([FromBody] Customer customer, ICustomerProvider customerProvider, IValidator validator) =>
|
|
{
|
|
Validator.Validate(validator, customer);
|
|
|
|
return Results.Ok(customerProvider.Create(customer));
|
|
});
|
|
}
|
|
|
|
internal static void MapUpdate(RouteGroupBuilder group)
|
|
{
|
|
group.MapPut("customers/{id:int}", (int id, [FromBody] Customer customer, ICustomerProvider customerProvider, IValidator validator) =>
|
|
{
|
|
customer.Id = id;
|
|
Validator.Validate(validator, customer);
|
|
|
|
return Results.Ok(customerProvider.Update(customer));
|
|
});
|
|
}
|
|
|
|
internal static void MapDelete(RouteGroupBuilder group)
|
|
{
|
|
group.MapDelete("customers/{id:int}", (int id, ICustomerProvider customerProvider, bool force = false) =>
|
|
Results.Ok(customerProvider.Delete(id, force)));
|
|
}
|
|
|
|
internal static void MapImport(RouteGroupBuilder group)
|
|
{
|
|
group.MapPost("customers/import", ([FromBody] IEnumerable<Customer> customers, ICustomerProvider customerProvider) =>
|
|
Results.Ok(customerProvider.Import(customers)));
|
|
}
|
|
}
|
|
}
|