55 lines
2.3 KiB
C#
55 lines
2.3 KiB
C#
using Indotalent.ConfigBackEnd.Extensions;
|
|
using Indotalent.Features.Organization.Branch.Cqrs;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
namespace Indotalent.Features.Organization.Branch;
|
|
|
|
public static class BranchEndpoint
|
|
{
|
|
public static void MapBranchEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var group = app.MapGroup("/branch").WithTags("Branches")
|
|
.RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
|
|
.RequireAuthenticatedUser());
|
|
|
|
group.MapGet("/", async (IMediator mediator) =>
|
|
{
|
|
var result = await mediator.Send(new GetBranchListQuery());
|
|
return result.ToApiResponse("Data branch retrieved successfully");
|
|
})
|
|
.WithName("GetBranchList");
|
|
|
|
group.MapGet("/{id}", async (string id, IMediator mediator) =>
|
|
{
|
|
var result = await mediator.Send(new GetBranchByIdQuery(id));
|
|
return result.ToApiResponse(result is not null
|
|
? "Branch detail retrieved successfully"
|
|
: $"Branch with ID {id} not found");
|
|
})
|
|
.WithName("GetBranchById");
|
|
|
|
group.MapPost("/", async (CreateBranchRequest request, IMediator mediator) =>
|
|
{
|
|
var result = await mediator.Send(new CreateBranchCommand(request));
|
|
return result.ToApiResponse("Branch has been created successfully", StatusCodes.Status201Created);
|
|
})
|
|
.WithName("CreateBranch");
|
|
|
|
group.MapPost("/update", async (UpdateBranchRequest request, IMediator mediator) =>
|
|
{
|
|
var result = await mediator.Send(new UpdateBranchCommand(request));
|
|
if (!result.Success) return ((object?)null).ToApiResponse("Update failed.");
|
|
return result.ToApiResponse("Branch has been updated successfully");
|
|
})
|
|
.WithName("UpdateBranch");
|
|
|
|
group.MapPost("/delete/{id}", async (string id, IMediator mediator) =>
|
|
{
|
|
var result = await mediator.Send(new DeleteBranchByIdCommand(new DeleteBranchByIdRequest(id)));
|
|
if (!result) return ((object?)null).ToApiResponse("Delete failed.");
|
|
return true.ToApiResponse("Branch has been deleted successfully");
|
|
})
|
|
.WithName("DeleteBranchById");
|
|
}
|
|
} |