diff --git a/de-institutions-api/Controllers/InstitutionController.cs b/de-institutions-api/Controllers/InstitutionController.cs index 858619b..8fe76fc 100644 --- a/de-institutions-api/Controllers/InstitutionController.cs +++ b/de-institutions-api/Controllers/InstitutionController.cs @@ -17,7 +17,7 @@ public InstitutionController(IMediator mediator) _mediator = mediator; } - [HttpGet("GetAll/", Name = "Get")] + [HttpGet("GetAll/")] [Produces("application/json")] public async Task GetAll() { @@ -31,7 +31,7 @@ public async Task GetAll() return Ok(result.Value); } - [HttpGet("GetByReferenceNumber", Name = "GetBy")] + [HttpGet("GetByReferenceNumber")] [Produces("application/json")] public async Task GetByReference(string refNumber) { @@ -45,7 +45,7 @@ public async Task GetByReference(string refNumber) return Ok(result.Value); } - [HttpGet("SearchByName", Name = "GetByName")] + [HttpGet("SearchByName")] [Produces("application/json")] public async Task GetByName(string name) { @@ -58,5 +58,19 @@ public async Task GetByName(string name) return Ok(result.Value); } + + [HttpGet("SearchSchoolByName")] + [Produces("application/json")] + public async Task GetSchoolByName(string name) + { + var result = await _mediator.Send(new GetSchoolByNameQuery() { Name = name }); + + if (result.IsFailure) + { + return NotFound(result.Error); + } + + return Ok(result.Value); + } } } \ No newline at end of file diff --git a/de-institutions-infrastructure/Features/Institution/Queries/GetSchoolByNameQuery.cs b/de-institutions-infrastructure/Features/Institution/Queries/GetSchoolByNameQuery.cs new file mode 100644 index 0000000..f76cd97 --- /dev/null +++ b/de-institutions-infrastructure/Features/Institution/Queries/GetSchoolByNameQuery.cs @@ -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>> + { + public string Name { get; set; } + } + + public class GetSchoolByNameQueryHandler : AbstractBaseHandler>> + { + public GetSchoolByNameQueryHandler(InstituitonContext instituitonContext, IMapper mapper, IValidator validator) + : base(instituitonContext, mapper, validator) + { + + } + + public override async Task>> Handle(GetSchoolByNameQuery request, CancellationToken cancellationToken) + { + var validationResult = await Validator.ValidateAsync(request, cancellationToken); + + if (!validationResult.IsValid) + return Result.Fail>(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>("School not found"); + + var institution = Mapper.Map>(maybeInstitution.Value); + + return Result.Ok(institution); + } + } +} \ No newline at end of file diff --git a/de-institutions-infrastructure/Features/Institution/Validation/GetSchoolByNameQueryValidator.cs b/de-institutions-infrastructure/Features/Institution/Validation/GetSchoolByNameQueryValidator.cs new file mode 100644 index 0000000..1af9404 --- /dev/null +++ b/de-institutions-infrastructure/Features/Institution/Validation/GetSchoolByNameQueryValidator.cs @@ -0,0 +1,13 @@ +using de_institutions_infrastructure.Features.Institution.Queries; +using FluentValidation; + +namespace de_institutions_infrastructure.Features.Institution.Validation +{ + public class GetSchoolByNameQueryValidator : AbstractValidator + { + public GetSchoolByNameQueryValidator() + { + RuleFor(r => r.Name).NotEmpty().WithMessage("Name is not set"); + } + } +} \ No newline at end of file