-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added New Endpoint GetMPBySchoolURN (#627)
* Added New Endpoint GetMPBySchoolURN
- Loading branch information
1 parent
5976f41
commit 69dc8fc
Showing
25 changed files
with
760 additions
and
281 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Dfe.Academies.Application/Common/Exceptions/CustomProblemDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Net; | ||
|
||
namespace Dfe.Academies.Application.Common.Exceptions | ||
{ | ||
public class CustomProblemDetails : ProblemDetails | ||
{ | ||
public CustomProblemDetails(HttpStatusCode statusCode, string? detail = null) | ||
{ | ||
Status = (int)statusCode; | ||
Detail = detail; | ||
|
||
Title = statusCode switch | ||
{ | ||
HttpStatusCode.NotFound => "Not Found", | ||
HttpStatusCode.Unauthorized => "Unauthorized", | ||
HttpStatusCode.Forbidden => "Forbidden", | ||
HttpStatusCode.BadRequest => "Bad Request", | ||
HttpStatusCode.InternalServerError => "Internal Server Error", | ||
_ => "An error occurred" | ||
}; | ||
|
||
Type = statusCode switch | ||
{ | ||
HttpStatusCode.NotFound => "https://tools.ietf.org/html/rfc9110#section-15.5.5", | ||
HttpStatusCode.Unauthorized => "https://tools.ietf.org/html/rfc7235#section-3.1", | ||
HttpStatusCode.Forbidden => "https://tools.ietf.org/html/rfc7231#section-6.5.3", | ||
HttpStatusCode.BadRequest => "https://tools.ietf.org/html/rfc7231#section-6.5.1", | ||
HttpStatusCode.InternalServerError => "https://tools.ietf.org/html/rfc7231#section-6.6.1", | ||
_ => "https://tools.ietf.org/html/rfc7231#section-6.6.1" | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Dfe.Academies.Application.Common.Models | ||
{ | ||
public class Result<T> | ||
{ | ||
public T? Value { get; } | ||
public bool IsSuccess { get; } | ||
public string? Error { get; } | ||
|
||
private Result(T value, bool isSuccess, string? error) | ||
{ | ||
Value = value; | ||
IsSuccess = isSuccess; | ||
Error = error; | ||
} | ||
|
||
public static Result<T> Success(T value) => new Result<T>(value, true, null); | ||
public static Result<T> Failure(string error) => new Result<T>(default!, false, error); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...tion/Establishment/Queries/GetMemberOfParliamentBySchool/GetMemberOfParliamentBySchool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using AutoMapper; | ||
using Dfe.Academies.Application.Common.Models; | ||
using Dfe.Academies.Domain.Interfaces.Repositories; | ||
using DfE.CoreLibs.Caching.Helpers; | ||
using DfE.CoreLibs.Caching.Interfaces; | ||
using MediatR; | ||
|
||
namespace Dfe.Academies.Application.Establishment.Queries.GetMemberOfParliamentBySchool | ||
{ | ||
public record GetMemberOfParliamentBySchoolQuery(int Urn) : IRequest<Result<MemberOfParliament?>>; | ||
|
||
public class GetMemberOfParliamentBySchoolQueryHandler( | ||
IEstablishmentRepository establishmentRepository, | ||
IConstituencyRepository constituencyRepository, | ||
IMapper mapper, | ||
ICacheService<IMemoryCacheType> cacheService) | ||
: IRequestHandler<GetMemberOfParliamentBySchoolQuery, Result<MemberOfParliament?>> | ||
{ | ||
public async Task<Result<MemberOfParliament?>> Handle(GetMemberOfParliamentBySchoolQuery request, CancellationToken cancellationToken) | ||
{ | ||
var cacheKey = $"MPbySchool_{CacheKeyHelper.GenerateHashedCacheKey(request.Urn.ToString())}"; | ||
|
||
return await cacheService.GetOrAddAsync(cacheKey, async () => | ||
{ | ||
var establishment = await establishmentRepository.GetEstablishmentByUrn(request.Urn.ToString(), cancellationToken); | ||
if (establishment == null) | ||
{ | ||
return Result<MemberOfParliament?>.Failure("School not found."); | ||
} | ||
|
||
var constituency = await constituencyRepository.GetMemberOfParliamentByConstituencyAsync(establishment.ParliamentaryConstituency!, cancellationToken); | ||
if (constituency == null) | ||
{ | ||
return Result<MemberOfParliament?>.Failure("Constituency not found for the given establishment."); | ||
} | ||
|
||
var mp = mapper.Map<MemberOfParliament?>(constituency); | ||
|
||
return Result<MemberOfParliament?>.Success(mp); | ||
|
||
}, nameof(GetMemberOfParliamentBySchoolQueryHandler)); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...blishment/Queries/GetMemberOfParliamentBySchool/GetMemberOfParliamentBySchoolValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using FluentValidation; | ||
|
||
namespace Dfe.Academies.Application.Establishment.Queries.GetMemberOfParliamentBySchool | ||
{ | ||
public class GetMemberOfParliamentBySchoolQueryValidator : AbstractValidator<GetMemberOfParliamentBySchoolQuery> | ||
{ | ||
public GetMemberOfParliamentBySchoolQueryValidator() | ||
{ | ||
RuleFor(query => query.Urn) | ||
.GreaterThan(0).WithMessage("URN must be greater than 0.") | ||
.NotEmpty().WithMessage("URN is required."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.