| @@ -4,47 +4,59 @@ namespace Diligent.WebAPI.Business.Extensions | |||
| { | |||
| public static class ApplicantExtensions | |||
| { | |||
| public static async Task<IQueryable<Applicant>> FilterApplicants(this IQueryable<Applicant> query,ApplicantFilterDto applicantFilterDto) | |||
| public static List<Applicant> FilterApplicants(this List<Applicant> query,ApplicantFilterDto applicantFilterDto) | |||
| { | |||
| IQueryable<Applicant> resultQuery; | |||
| resultQuery = query.FilterByExperience(applicantFilterDto.MinExperience, applicantFilterDto.MaxExperience) | |||
| return query.FilterByExperience(applicantFilterDto.MinExperience, applicantFilterDto.MaxExperience) | |||
| .FilterByEmploymentType(applicantFilterDto.EmploymentType) | |||
| .FilterByDateOfApplication(applicantFilterDto.MinDateOfApplication, applicantFilterDto.MaxDateOfApplication); | |||
| return FilterByTechnologies(await resultQuery.ToListAsync(), applicantFilterDto.Technologies); | |||
| .FilterByDateOfApplication(applicantFilterDto.MinDateOfApplication, applicantFilterDto.MaxDateOfApplication) | |||
| .FilterByTechnologies(applicantFilterDto.Technologies).ToList(); | |||
| } | |||
| public static IQueryable<Applicant> ApplyPagging(this IQueryable<Applicant> query, Pagination pagination) | |||
| public static List<Ad> FilterAdApplicants(this List<Ad> query, ApplicantFilterDto applicantFilterDto) | |||
| { | |||
| return query.Skip((pagination.CurrentPage - 1) * pagination.PageSize) | |||
| .Take(pagination.PageSize); | |||
| List<Ad> filteredAds = new(); | |||
| List<List<Applicant>> applicants = new(); | |||
| for (int i = 0; i < query.Count; i++) | |||
| { | |||
| for (int j = 0; j < query[i].Applicants.Count; j++) | |||
| { | |||
| var app = query[i].Applicants.FilterApplicants(applicantFilterDto); | |||
| applicants.Add(app); | |||
| } | |||
| var k = query[i]; | |||
| k.Applicants = applicants[i]; | |||
| filteredAds.Add(k); | |||
| } | |||
| return filteredAds; | |||
| } | |||
| private static IQueryable<Applicant> FilterByExperience(this IQueryable<Applicant> query, int minExperience, int maxExperience) | |||
| private static List<Applicant> FilterByExperience(this List<Applicant> query, int minExperience, int maxExperience) | |||
| { | |||
| if (minExperience == 0 && maxExperience == 0 || minExperience > maxExperience) return query; | |||
| return query.Where(x => x.Experience >= minExperience && x.Experience < maxExperience); | |||
| if ((minExperience == 0 && maxExperience == 0) || minExperience > maxExperience) return query; | |||
| return query.Where(x => x.Experience >= minExperience && x.Experience < maxExperience).ToList(); | |||
| } | |||
| private static IQueryable<Applicant> FilterByEmploymentType(this IQueryable<Applicant> query, string? employmentType) | |||
| private static List<Applicant> FilterByEmploymentType(this List<Applicant> query, string? employmentType) | |||
| { | |||
| if (employmentType == null) return query; | |||
| return query.Where(x => x.TypeOfEmployment == Enum.Parse<TypesOfEmployment>(employmentType)); | |||
| return query.Where(x => x.TypeOfEmployment == Enum.Parse<TypesOfEmployment>(employmentType)).ToList(); | |||
| } | |||
| private static IQueryable<Applicant> FilterByDateOfApplication(this IQueryable<Applicant> query, DateTime? minDateOfApplication, DateTime? maxDateOfApplication) | |||
| private static List<Applicant> FilterByDateOfApplication(this List<Applicant> query, DateTime? minDateOfApplication, DateTime? maxDateOfApplication) | |||
| { | |||
| if (minDateOfApplication == null) return query; | |||
| if (minDateOfApplication > maxDateOfApplication) return query; | |||
| if (maxDateOfApplication == null) return query.Where(x => x.DateOfApplication >= minDateOfApplication && x.DateOfApplication <= DateTime.Now); | |||
| return query.Where(x => x.DateOfApplication >= minDateOfApplication && x.DateOfApplication < maxDateOfApplication); | |||
| if (maxDateOfApplication == null) return query.Where(x => x.DateOfApplication >= minDateOfApplication && x.DateOfApplication <= DateTime.Now).ToList(); | |||
| return query.Where(x => x.DateOfApplication >= minDateOfApplication && x.DateOfApplication < maxDateOfApplication).ToList(); | |||
| } | |||
| private static IQueryable<Applicant> FilterByTechnologies(this List<Applicant> query, string[]? technologies) | |||
| private static List<Applicant> FilterByTechnologies(this List<Applicant> query, string[]? technologies) | |||
| { | |||
| if (technologies is null) | |||
| { | |||
| return query.AsQueryable(); | |||
| return query; | |||
| } | |||
| List<Applicant> filteredApplicants = new(); | |||
| @@ -70,7 +82,7 @@ namespace Diligent.WebAPI.Business.Extensions | |||
| } | |||
| } | |||
| return filteredApplicants.AsQueryable(); | |||
| return filteredApplicants; | |||
| } | |||
| } | |||
| } | |||
| @@ -8,10 +8,10 @@ namespace Diligent.WebAPI.Business.Extensions | |||
| { | |||
| public static class PaginationExtension | |||
| { | |||
| public static IQueryable<T> ApplyPagging<T>(this IQueryable<T> query, Pagination pagination) | |||
| public static List<T> ApplyPagging<T>(this List<T> query, Pagination pagination) | |||
| { | |||
| return query.Skip((pagination.CurrentPage - 1) * pagination.PageSize) | |||
| .Take(pagination.PageSize); | |||
| .Take(pagination.PageSize).ToList(); | |||
| } | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| using Diligent.WebAPI.Business.Extensions; | |||
| using Microsoft.AspNetCore.Http; | |||
| using static Diligent.WebAPI.Data.Entities.Applicant; | |||
| namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| @@ -16,14 +17,14 @@ namespace Diligent.WebAPI.Business.Services | |||
| public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto) | |||
| { | |||
| var allApplicants = _context.Applicants | |||
| var filteredApplicants = (await _context.Applicants | |||
| .Include(c => c.Ads) | |||
| .Include(x => x.TechnologyApplicants) | |||
| .ThenInclude(x => x.Technology); | |||
| var filteredApplicants = await allApplicants | |||
| .ThenInclude(x => x.Technology).ToListAsync()) | |||
| .FilterApplicants(applicantFilterDto); | |||
| int totalNumberOfItems = filteredApplicants.Count; | |||
| filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination | |||
| { | |||
| CurrentPage = applicantFilterDto.CurrentPage, | |||
| @@ -33,7 +34,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| return new QueryResultDto<ApplicantViewDto> | |||
| { | |||
| Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants), | |||
| Total = allApplicants.ToList().Count | |||
| Total = totalNumberOfItems | |||
| }; | |||
| } | |||
| @@ -84,11 +85,14 @@ namespace Diligent.WebAPI.Business.Services | |||
| await _context.SaveChangesAsync(); | |||
| } | |||
| public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants() | |||
| public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto) | |||
| { | |||
| var adsApplicants = await _context.Ads | |||
| var adsApplicants = (await _context.Ads | |||
| .Include(a => a.Applicants) | |||
| .ThenInclude(a => a.TechnologyApplicants).ThenInclude(a => a.Technology).ToListAsync(); | |||
| .ThenInclude(a => a.TechnologyApplicants) | |||
| .ThenInclude(a => a.Technology) | |||
| .ToListAsync()) | |||
| .FilterAdApplicants(applicantFilterDto); | |||
| return _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants); | |||
| } | |||
| @@ -4,7 +4,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| public interface IApplicantService | |||
| { | |||
| Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto); | |||
| Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(); | |||
| Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto); | |||
| Task<ApplicantViewDto> GetById(int id); | |||
| Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id); | |||
| Task CreateApplicant(ApplicantCreateDto applicantCreateDto); | |||
| @@ -24,10 +24,10 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| Ok(await _applicantService.GetById(id)); | |||
| [Authorize] | |||
| //[Authorize] | |||
| [HttpGet("adsApplicants")] | |||
| public async Task<IActionResult> GetAllAdsApplicants() => | |||
| Ok(await _applicantService.GetAllAdsApplicants()); | |||
| public async Task<IActionResult> GetAllAdsApplicants([FromQuery]ApplicantFilterDto applicantFilterDto) => | |||
| Ok(await _applicantService.GetAllAdsApplicants(applicantFilterDto)); | |||
| [Authorize] | |||
| [HttpDelete] | |||
| @@ -1,9 +1,19 @@ | |||
| using Diligent.WebAPI.Data.Entities; | |||
| using Diligent.WebAPI.Contracts.DTOs.Applicant; | |||
| using Diligent.WebAPI.Data.Entities; | |||
| using static Diligent.WebAPI.Data.Entities.Applicant; | |||
| namespace Diligent.WebAPI.Tests | |||
| { | |||
| public static class MockData | |||
| { | |||
| public static ApplicantFilterDto GetApplicantFilters() | |||
| { | |||
| return new ApplicantFilterDto | |||
| { | |||
| CurrentPage = 1, | |||
| PageSize = 4 | |||
| }; | |||
| } | |||
| public static List<Applicant> GetListOfApplicants() | |||
| { | |||
| var applicant = new Applicant | |||
| @@ -4,7 +4,6 @@ using Diligent.WebAPI.Business.MappingProfiles; | |||
| using Diligent.WebAPI.Business.Services; | |||
| using Diligent.WebAPI.Contracts.DTOs.Ad; | |||
| using Diligent.WebAPI.Contracts.DTOs.Applicant; | |||
| using Diligent.WebAPI.Contracts.DTOs.SelectionProcess; | |||
| using Diligent.WebAPI.Contracts.Exceptions; | |||
| using Diligent.WebAPI.Data.Entities; | |||
| using Microsoft.AspNetCore.Http; | |||
| @@ -18,7 +17,6 @@ namespace Diligent.WebAPI.Tests.Services | |||
| private readonly HttpContext _httpContext; | |||
| private readonly List<Applicant> _applicants; | |||
| private readonly List<Ad> _ads; | |||
| private readonly List<SelectionProcess> _selectionProcesses; | |||
| public ApplicantServiceTests() | |||
| { | |||
| _applicants = MockData.GetListOfApplicants(); | |||
| @@ -114,11 +112,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| await applicantService.DeleteApplicant(1); | |||
| var filterDto = new ApplicantFilterDto | |||
| { | |||
| CurrentPage = 1, | |||
| PageSize = 4 | |||
| }; | |||
| var filterDto = MockData.GetApplicantFilters(); | |||
| var applicants = await applicantService.GetFilteredApplicants(filterDto); | |||
| @@ -167,7 +161,9 @@ namespace Diligent.WebAPI.Tests.Services | |||
| var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| var result = await applicantService.GetAllAdsApplicants(); | |||
| var filterDto = MockData.GetApplicantFilters(); | |||
| var result = await applicantService.GetAllAdsApplicants(filterDto); | |||
| result.Should().BeEquivalentTo(_mapper.Map<List<AdApplicantsViewDto>>(_ads)); | |||
| } | |||