-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from dof-dss/Redis
Added GetSchoolByname query so that it limits results by name and als…
- Loading branch information
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
de-institutions-infrastructure/Features/Institution/Queries/GetSchoolByNameQuery.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,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); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...titutions-infrastructure/Features/Institution/Validation/GetSchoolByNameQueryValidator.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,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"); | ||
} | ||
} | ||
} |