Skip to content

Commit

Permalink
Use source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Aug 16, 2024
1 parent 76f5128 commit 4efdbf3
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TeachingRecordSystem.Api.V3.V20240101.Responses;

[AutoMap(typeof(FindPersonByLastNameAndDateOfBirthResult))]
public record FindTeachersResponse
{
public required int Total { get; init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using TeachingRecordSystem.Api.Infrastructure.ModelBinding;
using TeachingRecordSystem.Api.Infrastructure.Security;
using TeachingRecordSystem.Api.V3.Core.Operations;
using TeachingRecordSystem.Api.V3.V20240416.Requests;
using TeachingRecordSystem.Api.V3.V20240416.Responses;

namespace TeachingRecordSystem.Api.V3.V20240416.Controllers;

[Route("teacher")]
public class TeacherController(IMapper mapper) : ControllerBase
{
[Authorize(AuthorizationPolicies.IdentityUserWithTrn)]
[HttpGet]
[SwaggerOperation(
OperationId = "GetCurrentTeacher",
Summary = "Get the current teacher's details",
Description = "Gets the details for the authenticated teacher.")]
[ProducesResponseType(typeof(GetTeacherResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
public async Task<IActionResult> Get(
[FromQuery, ModelBinder(typeof(FlagsEnumStringListModelBinder)), SwaggerParameter("The additional properties to include in the response.")] GetTeacherRequestIncludes? include,
[FromServices] GetPersonHandler handler)
{
var trn = User.FindFirstValue("trn");

if (trn is null)
{
return MissingOrInvalidTrn();
}

var command = new GetPersonCommand(
trn,
include is not null ? (GetPersonCommandIncludes)include : GetPersonCommandIncludes.None,
DateOfBirth: null);

var result = await handler.Handle(command);

if (result is null)
{
return MissingOrInvalidTrn();
}

var response = mapper.Map<GetTeacherResponse>(result);
return Ok(response);

IActionResult MissingOrInvalidTrn() => Forbid();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using TeachingRecordSystem.Api.Infrastructure.ModelBinding;
using TeachingRecordSystem.Api.Infrastructure.Security;
using TeachingRecordSystem.Api.V3.Core.Operations;
using GetTeacherDtoVersion = TeachingRecordSystem.Api.V3.V20240101;
using TeachingRecordSystem.Api.V3.V20240416.Requests;
using TeachingRecordSystem.Api.V3.V20240416.Responses;

namespace TeachingRecordSystem.Api.V3.V20240416.Controllers;

Expand All @@ -16,13 +17,13 @@ public class TeachersController(IMapper mapper) : ControllerBase
OperationId = "GetTeacherByTrn",
Summary = "Get teacher details by TRN",
Description = "Gets the details of the teacher corresponding to the given TRN.")]
[ProducesResponseType(typeof(GetTeacherDtoVersion.Responses.GetTeacherResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(GetTeacherResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
[Authorize(Policy = AuthorizationPolicies.GetPerson)]
public async Task<IActionResult> Get(
[FromRoute] string trn,
[FromQuery, ModelBinder(typeof(FlagsEnumStringListModelBinder)), SwaggerParameter("The additional properties to include in the response.")] GetTeacherDtoVersion.Requests.GetTeacherRequestIncludes? include,
[FromQuery, ModelBinder(typeof(FlagsEnumStringListModelBinder)), SwaggerParameter("The additional properties to include in the response.")] GetTeacherRequestIncludes? include,
[FromQuery, SwaggerParameter("Adds an additional check that the record has the specified dateOfBirth, if provided.")] DateOnly? dateOfBirth,
[FromServices] GetPersonHandler handler)
{
Expand All @@ -38,7 +39,32 @@ public async Task<IActionResult> Get(
return NotFound();
}

var response = mapper.Map<GetTeacherDtoVersion.Responses.GetTeacherResponse>(result);
var response = mapper.Map<GetTeacherResponse>(result);
return Ok(response);
}

[HttpGet("")]
[SwaggerOperation(
OperationId = "FindTeachers",
Summary = "Find teachers",
Description = "Finds teachers with a TRN matching the specified criteria.")]
[ProducesResponseType(typeof(FindTeachersResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[Authorize(Policy = AuthorizationPolicies.GetPerson)]
public async Task<IActionResult> FindTeachers(
FindTeachersRequest request,
[FromServices] FindPersonByLastNameAndDateOfBirthHandler handler)
{
var command = new FindPersonByLastNameAndDateOfBirthCommand(request.LastName!, request.DateOfBirth!.Value);
var result = await handler.Handle(command);

var response = new FindTeachersResponse()
{
Total = result.Total,
Query = request,
Results = result.Items.Select(mapper.Map<FindTeachersResponseResult>).AsReadOnly()
};

return Ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TeachingRecordSystem.Api.V3.V20240416.Requests;

[GenerateVersionedDto(typeof(V20240101.Requests.FindTeachersRequest))]
public partial record FindTeachersRequest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.ComponentModel;

namespace TeachingRecordSystem.Api.V3.V20240416.Requests;

[Flags]
[Description("Comma-separated list of data to include in response.")]
public enum GetTeacherRequestIncludes
{
None = 0,

Induction = 1 << 0,
InitialTeacherTraining = 1 << 1,
NpqQualifications = 1 << 2,
MandatoryQualifications = 1 << 3,
PendingDetailChanges = 1 << 4,
HigherEducationQualifications = 1 << 5,
Sanctions = 1 << 6,
Alerts = 1 << 7,
PreviousNames = 1 << 8,

[ExcludeFromSchema]
_AllowIdSignInWithProhibitions = 1 << 9,

All = Induction | InitialTeacherTraining | NpqQualifications | MandatoryQualifications | PendingDetailChanges | HigherEducationQualifications | Sanctions | Alerts | PreviousNames
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TeachingRecordSystem.Api.V3.V20240416.Requests;

namespace TeachingRecordSystem.Api.V3.V20240416.Responses;

[GenerateVersionedDto(typeof(V20240101.Responses.FindTeachersResponse), excludeMembers: "Query")]
public partial record FindTeachersResponse
{
public required FindTeachersRequest Query { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using TeachingRecordSystem.Api.V3.Core.Operations;

namespace TeachingRecordSystem.Api.V3.V20240416.Responses;

[AutoMap(typeof(GetPersonResult))]
[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponse))]
public partial record GetTeacherResponse;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using TeachingRecordSystem.Api.V3.Core.Operations;
using TeachingRecordSystem.Api.V3.V20240606.Requests;
using TeachingRecordSystem.Api.V3.V20240606.Responses;
using CreateDetailChangeResponseVersion = TeachingRecordSystem.Api.V3.V20240412;

namespace TeachingRecordSystem.Api.V3.V20240606.Controllers;

Expand Down Expand Up @@ -41,7 +40,7 @@ public async Task<IActionResult> Get(
OperationId = "CreateNameChange",
Summary = "Create name change request",
Description = "Creates a name change request for the authenticated teacher.")]
[ProducesResponseType(typeof(CreateDetailChangeResponseVersion.Responses.CreateNameChangeResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(CreateNameChangeResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[Authorize(AuthorizationPolicies.IdentityUserWithTrn)]
public async Task<IActionResult> CreateNameChange(
Expand All @@ -60,7 +59,7 @@ public async Task<IActionResult> CreateNameChange(
};

var caseNumber = await handler.Handle(command);
var response = new CreateDetailChangeResponseVersion.Responses.CreateNameChangeResponse() { CaseNumber = caseNumber };
var response = new CreateNameChangeResponse() { CaseNumber = caseNumber };
return Ok(response);
}

Expand All @@ -69,7 +68,7 @@ public async Task<IActionResult> CreateNameChange(
OperationId = "CreateDobChange",
Summary = "Create DOB change request",
Description = "Creates a date of birth change request for the authenticated teacher.")]
[ProducesResponseType(typeof(CreateDetailChangeResponseVersion.Responses.CreateDateOfBirthChangeResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(CreateDateOfBirthChangeResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[Authorize(AuthorizationPolicies.IdentityUserWithTrn)]
public async Task<IActionResult> CreateDateOfBirthChange(
Expand All @@ -86,7 +85,7 @@ public async Task<IActionResult> CreateDateOfBirthChange(
};

var caseNumber = await handler.Handle(command);
var response = new CreateDetailChangeResponseVersion.Responses.CreateNameChangeResponse() { CaseNumber = caseNumber };
var response = new CreateNameChangeResponse() { CaseNumber = caseNumber };
return Ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TeachingRecordSystem.Api.V3.V20240606.Responses;

[GenerateVersionedDto(typeof(V20240412.Responses.CreateDateOfBirthChangeResponse))]
public partial record CreateDateOfBirthChangeResponse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TeachingRecordSystem.Api.V3.V20240606.Responses;

[GenerateVersionedDto(typeof(V20240412.Responses.CreateNameChangeResponse))]
public partial record CreateNameChangeResponse;
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
using TeachingRecordSystem.Api.V3.Core.Operations;
using TeachingRecordSystem.Api.V3.V20240101.ApiModels;
using TeachingRecordSystem.Api.V3.V20240606.Requests;

namespace TeachingRecordSystem.Api.V3.V20240606.Responses;

[AutoMap(typeof(FindPersonByLastNameAndDateOfBirthResult))]
public record FindPersonResponse
[GenerateVersionedDto(typeof(V20240101.Responses.FindTeachersResponse), excludeMembers: ["Query", "Results"])]
public partial record FindPersonResponse
{
public required int Total { get; init; }
public required FindPersonRequest Query { get; init; }
public required IReadOnlyCollection<FindPersonResponseResult> Results { get; init; }
}

[AutoMap(typeof(FindPersonByLastNameAndDateOfBirthResultItem))]
public record FindPersonResponseResult
{
public required string Trn { get; init; }
public required DateOnly DateOfBirth { get; init; }
public required string FirstName { get; init; }
public required string MiddleName { get; init; }
public required string LastName { get; init; }
public required IReadOnlyCollection<SanctionInfo> Sanctions { get; init; }
public required IReadOnlyCollection<NameInfo> PreviousNames { get; init; }
}
[GenerateVersionedDto(typeof(V20240101.Responses.FindTeachersResponseResult))]
public partial record FindPersonResponseResult;
Loading

0 comments on commit 4efdbf3

Please sign in to comment.