Update to latest packages and show examples
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using GerstITS.Examples.Logic.Customers;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace GerstITS.Examples.Api.Endpoints;
|
||||
|
||||
internal static partial class Endpoints
|
||||
{
|
||||
internal static RouteGroupBuilder MapCustomerNoteEndpoints(this RouteGroupBuilder group)
|
||||
{
|
||||
CustomerNotes.MapGetByCustomer(group);
|
||||
CustomerNotes.MapAdd(group);
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
internal static class CustomerNotes
|
||||
{
|
||||
internal static void MapGetByCustomer(RouteGroupBuilder group)
|
||||
{
|
||||
group.MapGet("customers/{id:int}/notes", (int id, ICustomerProvider customerProvider) =>
|
||||
Results.Ok(customerProvider.GetNotes(id)));
|
||||
}
|
||||
|
||||
internal static void MapAdd(RouteGroupBuilder group)
|
||||
{
|
||||
group.MapPost("customers/{id:int}/notes", (int id, [FromBody] CustomerNote note, ICustomerProvider customerProvider) =>
|
||||
Results.Ok(customerProvider.AddNote(id, note.Text)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using GerstITS.Validation;
|
||||
|
||||
namespace GerstITS.Examples.Api.Endpoints;
|
||||
|
||||
internal static partial class Endpoints
|
||||
{
|
||||
internal static class Validator
|
||||
{
|
||||
internal static void Validate<T>(IValidator validator, T model)
|
||||
{
|
||||
var result = validator.Validate(model);
|
||||
|
||||
if (!result.IsValid)
|
||||
throw new ValidationException($"{typeof(T).Name} is invalid.{Environment.NewLine}Details: {result.ToErrorMessage()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace GerstITS.Examples.Api.Endpoints;
|
||||
|
||||
internal static partial class Endpoints
|
||||
{
|
||||
internal static RouteGroupBuilder MapEndpoints(this IEndpointRouteBuilder root)
|
||||
{
|
||||
return root.MapCustomerEndpoints()
|
||||
.MapCustomerNoteEndpoints();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user