From 91b8a438cfbbb697fdf4d1bb383ee31d56358bbe Mon Sep 17 00:00:00 2001 From: James Gunn Date: Fri, 16 Aug 2024 11:27:09 +0100 Subject: [PATCH] Use source generator --- .../Responses/FindTeachersResponse.cs | 1 - .../Controllers/TeacherController.cs | 52 ++++++++ .../Controllers/TeachersController.cs | 9 +- .../Requests/GetTeacherRequestIncludes.cs | 25 ++++ .../V20240416/Responses/GetTeacherResponse.cs | 7 + .../V20240606/Controllers/PersonController.cs | 9 +- .../CreateDateOfBirthChangeResponse.cs | 4 + .../Responses/CreateNameChangeResponse.cs | 4 + .../V20240606/Responses/FindPersonResponse.cs | 18 +-- .../V20240606/Responses/GetPersonResponse.cs | 123 +++++------------- 10 files changed, 136 insertions(+), 116 deletions(-) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeacherController.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Requests/GetTeacherRequestIncludes.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Responses/GetTeacherResponse.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateDateOfBirthChangeResponse.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateNameChangeResponse.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240101/Responses/FindTeachersResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240101/Responses/FindTeachersResponse.cs index 50794297f..f7b3cf593 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240101/Responses/FindTeachersResponse.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240101/Responses/FindTeachersResponse.cs @@ -4,7 +4,6 @@ namespace TeachingRecordSystem.Api.V3.V20240101.Responses; -[AutoMap(typeof(FindPersonByLastNameAndDateOfBirthResult))] public record FindTeachersResponse { public required int Total { get; init; } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeacherController.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeacherController.cs new file mode 100644 index 000000000..62dd6e9a3 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeacherController.cs @@ -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 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(result); + return Ok(response); + + IActionResult MissingOrInvalidTrn() => Forbid(); + } +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeachersController.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeachersController.cs index 142eaaa65..648e2b166 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeachersController.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Controllers/TeachersController.cs @@ -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; @@ -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 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) { @@ -38,7 +39,7 @@ public async Task Get( return NotFound(); } - var response = mapper.Map(result); + var response = mapper.Map(result); return Ok(response); } } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Requests/GetTeacherRequestIncludes.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Requests/GetTeacherRequestIncludes.cs new file mode 100644 index 000000000..1cab8128f --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Requests/GetTeacherRequestIncludes.cs @@ -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 +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Responses/GetTeacherResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Responses/GetTeacherResponse.cs new file mode 100644 index 000000000..f8a17d84f --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240416/Responses/GetTeacherResponse.cs @@ -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; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Controllers/PersonController.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Controllers/PersonController.cs index d3d74a59a..1fd22d822 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Controllers/PersonController.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Controllers/PersonController.cs @@ -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; @@ -41,7 +40,7 @@ public async Task 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 CreateNameChange( @@ -60,7 +59,7 @@ public async Task CreateNameChange( }; var caseNumber = await handler.Handle(command); - var response = new CreateDetailChangeResponseVersion.Responses.CreateNameChangeResponse() { CaseNumber = caseNumber }; + var response = new CreateNameChangeResponse() { CaseNumber = caseNumber }; return Ok(response); } @@ -69,7 +68,7 @@ public async Task 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 CreateDateOfBirthChange( @@ -86,7 +85,7 @@ public async Task CreateDateOfBirthChange( }; var caseNumber = await handler.Handle(command); - var response = new CreateDetailChangeResponseVersion.Responses.CreateNameChangeResponse() { CaseNumber = caseNumber }; + var response = new CreateNameChangeResponse() { CaseNumber = caseNumber }; return Ok(response); } } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateDateOfBirthChangeResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateDateOfBirthChangeResponse.cs new file mode 100644 index 000000000..e7bd4e2d5 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateDateOfBirthChangeResponse.cs @@ -0,0 +1,4 @@ +namespace TeachingRecordSystem.Api.V3.V20240606.Responses; + +[GenerateVersionedDto(typeof(V20240412.Responses.CreateDateOfBirthChangeResponse))] +public partial record CreateDateOfBirthChangeResponse; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateNameChangeResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateNameChangeResponse.cs new file mode 100644 index 000000000..aacaa5c0a --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/CreateNameChangeResponse.cs @@ -0,0 +1,4 @@ +namespace TeachingRecordSystem.Api.V3.V20240606.Responses; + +[GenerateVersionedDto(typeof(V20240412.Responses.CreateNameChangeResponse))] +public partial record CreateNameChangeResponse; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/FindPersonResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/FindPersonResponse.cs index 314c567ee..d852c2a2c 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/FindPersonResponse.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/FindPersonResponse.cs @@ -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 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 Sanctions { get; init; } - public required IReadOnlyCollection PreviousNames { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.FindTeachersResponseResult))] +public partial record FindPersonResponseResult; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/GetPersonResponse.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/GetPersonResponse.cs index ad0ea98fe..a5ad56c23 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/GetPersonResponse.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Responses/GetPersonResponse.cs @@ -1,4 +1,3 @@ -using System.Text.Json.Serialization; using Optional; using TeachingRecordSystem.Api.V3.Core.Operations; using TeachingRecordSystem.Api.V3.V20240101.ApiModels; @@ -6,7 +5,7 @@ namespace TeachingRecordSystem.Api.V3.V20240606.Responses; [AutoMap(typeof(GetPersonResult))] -public record GetPersonResponse +public partial record GetPersonResponse { public required string Trn { get; init; } public required string FirstName { get; init; } @@ -31,121 +30,61 @@ public record GetPersonResponse } [AutoMap(typeof(GetPersonResultQts))] -public record GetPersonResponseQts -{ - public required DateOnly? Awarded { get; init; } - public required string CertificateUrl { get; init; } - public required string? StatusDescription { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseQts))] +public partial record GetPersonResponseQts; [AutoMap(typeof(GetPersonResultEyts))] -public record GetPersonResponseEyts -{ - public required DateOnly? Awarded { get; init; } - public required string CertificateUrl { get; init; } - public required string? StatusDescription { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseEyts))] +public partial record GetPersonResponseEyts; [AutoMap(typeof(GetPersonResultInduction))] -public record GetPersonResponseInduction -{ - public required DateOnly? StartDate { get; init; } - public required DateOnly? EndDate { get; init; } - public required InductionStatus? Status { get; init; } - public required string? StatusDescription { get; init; } - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public required string? CertificateUrl { get; init; } - public required IReadOnlyCollection Periods { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInduction))] +public partial record GetPersonResponseInduction; [AutoMap(typeof(GetPersonResultInductionPeriod))] -public record GetPersonResponseInductionPeriod -{ - public required DateOnly? StartDate { get; init; } - public required DateOnly? EndDate { get; init; } - public required int? Terms { get; init; } - public required GetPersonResponseInductionPeriodAppropriateBody? AppropriateBody { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInductionPeriod))] +public partial record GetPersonResponseInductionPeriod; [AutoMap(typeof(GetPersonResultInductionPeriodAppropriateBody))] -public record GetPersonResponseInductionPeriodAppropriateBody -{ - public required string Name { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInductionPeriodAppropriateBody))] +public partial record GetPersonResponseInductionPeriodAppropriateBody; [AutoMap(typeof(GetPersonResultInitialTeacherTraining))] -public record GetPersonResponseInitialTeacherTraining -{ - public required GetPersonResponseInitialTeacherTrainingQualification? Qualification { get; init; } - public required DateOnly? StartDate { get; init; } - public required DateOnly? EndDate { get; init; } - public required IttProgrammeType? ProgrammeType { get; init; } - public required string? ProgrammeTypeDescription { get; init; } - public required IttOutcome? Result { get; init; } - public required GetPersonResponseInitialTeacherTrainingAgeRange? AgeRange { get; init; } - public required GetPersonResponseInitialTeacherTrainingProvider? Provider { get; init; } - public required IReadOnlyCollection Subjects { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInitialTeacherTraining))] +public partial record GetPersonResponseInitialTeacherTraining; [AutoMap(typeof(GetPersonResultInitialTeacherTrainingQualification))] -public record GetPersonResponseInitialTeacherTrainingQualification -{ - public required string Name { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInitialTeacherTrainingQualification))] +public partial record GetPersonResponseInitialTeacherTrainingQualification; [AutoMap(typeof(GetPersonResultInitialTeacherTrainingAgeRange))] -public record GetPersonResponseInitialTeacherTrainingAgeRange -{ - public required string Description { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInitialTeacherTrainingAgeRange))] +public partial record GetPersonResponseInitialTeacherTrainingAgeRange; [AutoMap(typeof(GetPersonResultInitialTeacherTrainingProvider))] -public record GetPersonResponseInitialTeacherTrainingProvider -{ - public required string Name { get; init; } - public required string Ukprn { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInitialTeacherTrainingProvider))] +public partial record GetPersonResponseInitialTeacherTrainingProvider; [AutoMap(typeof(GetPersonResultInitialTeacherTrainingSubject))] -public record GetPersonResponseInitialTeacherTrainingSubject -{ - public required string Code { get; init; } - public required string Name { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseInitialTeacherTrainingSubject))] +public partial record GetPersonResponseInitialTeacherTrainingSubject; [AutoMap(typeof(GetPersonResultNpqQualification))] -public record GetPersonResponseNpqQualification -{ - public required DateOnly Awarded { get; init; } - public required GetPersonResponseNpqQualificationType Type { get; init; } - public required string CertificateUrl { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseNpqQualification))] +public partial record GetPersonResponseNpqQualification; [AutoMap(typeof(GetPersonResultNpqQualificationType))] -public record GetPersonResponseNpqQualificationType -{ - public required NpqQualificationType Code { get; init; } - public required string Name { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseNpqQualificationType))] +public partial record GetPersonResponseNpqQualificationType; [AutoMap(typeof(GetPersonResultMandatoryQualification))] -public record GetPersonResponseMandatoryQualification -{ - public required DateOnly Awarded { get; init; } - public required string Specialism { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseMandatoryQualification))] +public partial record GetPersonResponseMandatoryQualification; [AutoMap(typeof(GetPersonResultHigherEducationQualification))] -public record GetPersonResponseHigherEducationQualification -{ - public required string? Name { get; init; } - public required DateOnly? Awarded { get; init; } - public required IReadOnlyCollection Subjects { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseHigherEducationQualification))] +public partial record GetPersonResponseHigherEducationQualification; [AutoMap(typeof(GetPersonResultHigherEducationQualificationSubject))] -public record GetPersonResponseHigherEducationQualificationSubject -{ - public required string Code { get; init; } - public required string Name { get; init; } -} +[GenerateVersionedDto(typeof(V20240101.Responses.GetTeacherResponseHigherEducationQualificationSubject))] +public partial record GetPersonResponseHigherEducationQualificationSubject;