| { | { | ||||
| public static class ApplicantExtensions | 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) | .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; | 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 == null) return query; | ||||
| if (minDateOfApplication > maxDateOfApplication) 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) | if (technologies is null) | ||||
| { | { | ||||
| return query.AsQueryable(); | |||||
| return query; | |||||
| } | } | ||||
| List<Applicant> filteredApplicants = new(); | List<Applicant> filteredApplicants = new(); | ||||
| } | } | ||||
| } | } | ||||
| return filteredApplicants.AsQueryable(); | |||||
| return filteredApplicants; | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| { | { | ||||
| public static class PaginationExtension | 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) | return query.Skip((pagination.CurrentPage - 1) * pagination.PageSize) | ||||
| .Take(pagination.PageSize); | |||||
| .Take(pagination.PageSize).ToList(); | |||||
| } | } | ||||
| } | } | ||||
| using Diligent.WebAPI.Business.Extensions; | using Diligent.WebAPI.Business.Extensions; | ||||
| using Microsoft.AspNetCore.Http; | using Microsoft.AspNetCore.Http; | ||||
| using static Diligent.WebAPI.Data.Entities.Applicant; | |||||
| namespace Diligent.WebAPI.Business.Services | namespace Diligent.WebAPI.Business.Services | ||||
| { | { | ||||
| public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto) | public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto) | ||||
| { | { | ||||
| var allApplicants = _context.Applicants | |||||
| var filteredApplicants = (await _context.Applicants | |||||
| .Include(c => c.Ads) | .Include(c => c.Ads) | ||||
| .Include(x => x.TechnologyApplicants) | .Include(x => x.TechnologyApplicants) | ||||
| .ThenInclude(x => x.Technology); | |||||
| var filteredApplicants = await allApplicants | |||||
| .ThenInclude(x => x.Technology).ToListAsync()) | |||||
| .FilterApplicants(applicantFilterDto); | .FilterApplicants(applicantFilterDto); | ||||
| int totalNumberOfItems = filteredApplicants.Count; | |||||
| filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination | filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination | ||||
| { | { | ||||
| CurrentPage = applicantFilterDto.CurrentPage, | CurrentPage = applicantFilterDto.CurrentPage, | ||||
| return new QueryResultDto<ApplicantViewDto> | return new QueryResultDto<ApplicantViewDto> | ||||
| { | { | ||||
| Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants), | Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants), | ||||
| Total = allApplicants.ToList().Count | |||||
| Total = totalNumberOfItems | |||||
| }; | }; | ||||
| } | } | ||||
| await _context.SaveChangesAsync(); | 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) | .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); | return _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants); | ||||
| } | } |
| public interface IApplicantService | public interface IApplicantService | ||||
| { | { | ||||
| Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto); | Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto); | ||||
| Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(); | |||||
| Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto); | |||||
| Task<ApplicantViewDto> GetById(int id); | Task<ApplicantViewDto> GetById(int id); | ||||
| Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id); | Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id); | ||||
| Task CreateApplicant(ApplicantCreateDto applicantCreateDto); | Task CreateApplicant(ApplicantCreateDto applicantCreateDto); |
| Ok(await _applicantService.GetById(id)); | Ok(await _applicantService.GetById(id)); | ||||
| [Authorize] | |||||
| //[Authorize] | |||||
| [HttpGet("adsApplicants")] | [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] | [Authorize] | ||||
| [HttpDelete] | [HttpDelete] |
| 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 | namespace Diligent.WebAPI.Tests | ||||
| { | { | ||||
| public static class MockData | public static class MockData | ||||
| { | { | ||||
| public static ApplicantFilterDto GetApplicantFilters() | |||||
| { | |||||
| return new ApplicantFilterDto | |||||
| { | |||||
| CurrentPage = 1, | |||||
| PageSize = 4 | |||||
| }; | |||||
| } | |||||
| public static List<Applicant> GetListOfApplicants() | public static List<Applicant> GetListOfApplicants() | ||||
| { | { | ||||
| var applicant = new Applicant | var applicant = new Applicant |
| using Diligent.WebAPI.Business.Services; | using Diligent.WebAPI.Business.Services; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Ad; | using Diligent.WebAPI.Contracts.DTOs.Ad; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Applicant; | using Diligent.WebAPI.Contracts.DTOs.Applicant; | ||||
| using Diligent.WebAPI.Contracts.DTOs.SelectionProcess; | |||||
| using Diligent.WebAPI.Contracts.Exceptions; | using Diligent.WebAPI.Contracts.Exceptions; | ||||
| using Diligent.WebAPI.Data.Entities; | using Diligent.WebAPI.Data.Entities; | ||||
| using Microsoft.AspNetCore.Http; | using Microsoft.AspNetCore.Http; | ||||
| private readonly HttpContext _httpContext; | private readonly HttpContext _httpContext; | ||||
| private readonly List<Applicant> _applicants; | private readonly List<Applicant> _applicants; | ||||
| private readonly List<Ad> _ads; | private readonly List<Ad> _ads; | ||||
| private readonly List<SelectionProcess> _selectionProcesses; | |||||
| public ApplicantServiceTests() | public ApplicantServiceTests() | ||||
| { | { | ||||
| _applicants = MockData.GetListOfApplicants(); | _applicants = MockData.GetListOfApplicants(); | ||||
| await applicantService.DeleteApplicant(1); | await applicantService.DeleteApplicant(1); | ||||
| var filterDto = new ApplicantFilterDto | |||||
| { | |||||
| CurrentPage = 1, | |||||
| PageSize = 4 | |||||
| }; | |||||
| var filterDto = MockData.GetApplicantFilters(); | |||||
| var applicants = await applicantService.GetFilteredApplicants(filterDto); | var applicants = await applicantService.GetFilteredApplicants(filterDto); | ||||
| var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads); | var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads); | ||||
| ApplicantService applicantService = new(databaseContext, _mapper); | 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)); | result.Should().BeEquivalentTo(_mapper.Map<List<AdApplicantsViewDto>>(_ads)); | ||||
| } | } |