using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Features.Thirdparty.Patient.Cqrs; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; namespace Indotalent.Features.Thirdparty.Patient; public static class PatientEndpoint { public static void MapPatientEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/patient").WithTags("Patients") .RequireAuthorization(policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser()); group.MapGet("/", async (IMediator mediator) => { var result = await mediator.Send(new GetPatientListQuery()); return result.ToApiResponse("Patient list retrieved successfully"); }) .WithName("GetPatientList") .WithTags("Patients"); group.MapGet("/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new GetPatientByIdQuery(id)); return result.ToApiResponse(result is not null ? "Patient detail retrieved successfully" : $"Patient with ID {id} not found"); }) .WithName("GetPatientById") .WithTags("Patients"); group.MapGet("/lookup", async (IMediator mediator) => { var result = await mediator.Send(new LookupPatientQuery()); return result.ToApiResponse("Patient lookup data retrieved successfully"); }) .WithName("GetPatientLookup") .WithTags("Patients"); group.MapPost("/", async (CreatePatientRequest request, IMediator mediator) => { var result = await mediator.Send(new CreatePatientCommand(request)); return result.ToApiResponse("Patient has been created successfully", StatusCodes.Status201Created); }) .WithName("CreatePatient") .WithTags("Patients"); group.MapPost("/update", async (UpdatePatientRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdatePatientCommand(request)); if (!result.Success) { return ((object?)null).ToApiResponse("Update failed. The patient data could not be found."); } return result.ToApiResponse("Patient has been updated successfully"); }) .WithName("UpdatePatient") .WithTags("Patients"); group.MapPost("/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeletePatientByIdCommand(new DeletePatientByIdRequest(id))); if (!result) { return ((object?)null).ToApiResponse("Delete failed. The patient data could not be found."); } return true.ToApiResponse("Patient has been deleted successfully"); }) .WithName("DeletePatientById") .WithTags("Patients"); group.MapPost("/patient-contact", async (CreatePatientContactRequest request, IMediator mediator) => { var result = await mediator.Send(new CreatePatientContactCommand(request)); return result.ToApiResponse("Patient contact has been added successfully"); }) .WithName("CreatePatientContactChild") .WithTags("Patients"); group.MapPost("/patient-contact/update", async (UpdatePatientContactRequest request, IMediator mediator) => { var result = await mediator.Send(new UpdatePatientContactCommand(request)); if (!result.Success) { return ((object?)null).ToApiResponse("Update failed. Patient contact not found."); } return result.ToApiResponse("Patient contact has been updated successfully"); }) .WithName("UpdatePatientContactChild") .WithTags("Patients"); group.MapPost("/patient-contact/delete/{id}", async (string id, IMediator mediator) => { var result = await mediator.Send(new DeletePatientContactCommand(id)); if (!result) { return ((object?)null).ToApiResponse("Delete failed. Patient contact not found."); } return true.ToApiResponse("Patient contact has been removed successfully"); }) .WithName("DeletePatientContactChild") .WithTags("Patients"); } }