Skip to content

Commit

Permalink
Merge pull request #17 from dof-dss/Redis
Browse files Browse the repository at this point in the history
Added GetSchoolByname query so that it limits results by name and als…
  • Loading branch information
MichaelStevenson2207 authored Aug 18, 2021
2 parents 5ee6c03 + 529764e commit 92d4967
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
20 changes: 17 additions & 3 deletions de-institutions-api/Controllers/InstitutionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public InstitutionController(IMediator mediator)
_mediator = mediator;
}

[HttpGet("GetAll/", Name = "Get")]
[HttpGet("GetAll/")]
[Produces("application/json")]
public async Task<ActionResult> GetAll()
{
Expand All @@ -31,7 +31,7 @@ public async Task<ActionResult> GetAll()
return Ok(result.Value);
}

[HttpGet("GetByReferenceNumber", Name = "GetBy")]
[HttpGet("GetByReferenceNumber")]
[Produces("application/json")]
public async Task<ActionResult> GetByReference(string refNumber)
{
Expand All @@ -45,7 +45,7 @@ public async Task<ActionResult> GetByReference(string refNumber)
return Ok(result.Value);
}

[HttpGet("SearchByName", Name = "GetByName")]
[HttpGet("SearchByName")]
[Produces("application/json")]
public async Task<ActionResult> GetByName(string name)
{
Expand All @@ -58,5 +58,19 @@ public async Task<ActionResult> GetByName(string name)

return Ok(result.Value);
}

[HttpGet("SearchSchoolByName")]
[Produces("application/json")]
public async Task<ActionResult> GetSchoolByName(string name)
{
var result = await _mediator.Send(new GetSchoolByNameQuery() { Name = name });

if (result.IsFailure)
{
return NotFound(result.Error);
}

return Ok(result.Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using AutoMapper;
using de_institutions_api_core.DTOs;
using de_institutions_infrastructure.Data;
using de_institutions_infrastructure.Features.Common;
using dss_common.Extensions;
using dss_common.Functional;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace de_institutions_infrastructure.Features.Institution.Queries
{
public class GetSchoolByNameQuery : IRequest<Result<List<InstitutionDto>>>
{
public string Name { get; set; }
}

public class GetSchoolByNameQueryHandler : AbstractBaseHandler<GetSchoolByNameQuery, Result<List<InstitutionDto>>>
{
public GetSchoolByNameQueryHandler(InstituitonContext instituitonContext, IMapper mapper, IValidator<GetSchoolByNameQuery> validator)
: base(instituitonContext, mapper, validator)
{

}

public override async Task<Result<List<InstitutionDto>>> Handle(GetSchoolByNameQuery request, CancellationToken cancellationToken)
{
var validationResult = await Validator.ValidateAsync(request, cancellationToken);

if (!validationResult.IsValid)
return Result.Fail<List<InstitutionDto>>(validationResult.ToString());

var maybeInstitution = InstituitonContext.Institution.AsNoTracking()
.Where(a => a.Name.Contains(request.Name) && a.InstitutionType == "Primary school" ||
a.InstitutionType == "Prepatory Schools"
|| a.InstitutionType == "Secondary (non-grammar) school"
|| a.InstitutionType == "Secondary (grammar) school"
|| a.InstitutionType == "Special school")
.Include(a => a.Address)
.ToMaybe();

if (!maybeInstitution.HasValue)
return Result.Fail<List<InstitutionDto>>("School not found");

var institution = Mapper.Map<List<InstitutionDto>>(maybeInstitution.Value);

return Result.Ok(institution);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using de_institutions_infrastructure.Features.Institution.Queries;
using FluentValidation;

namespace de_institutions_infrastructure.Features.Institution.Validation
{
public class GetSchoolByNameQueryValidator : AbstractValidator<GetSchoolByNameQuery>
{
public GetSchoolByNameQueryValidator()
{
RuleFor(r => r.Name).NotEmpty().WithMessage("Name is not set");
}
}
}

0 comments on commit 92d4967

Please sign in to comment.