You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicantService.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Diligent.WebAPI.Business.Extensions;
  2. using Microsoft.AspNetCore.Http;
  3. namespace Diligent.WebAPI.Business.Services
  4. {
  5. public class ApplicantService : IApplicantService
  6. {
  7. private readonly DatabaseContext _context;
  8. private readonly IMapper _mapper;
  9. private readonly IHttpContextAccessor _httpContextAccessor;
  10. public ApplicantService(DatabaseContext context, IMapper mapper, IHttpContextAccessor httpContextAccessor)
  11. {
  12. _context = context;
  13. _mapper = mapper;
  14. _httpContextAccessor = httpContextAccessor;
  15. }
  16. public async Task<List<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
  17. {
  18. int applicantsToSkip = (applicantFilterDto.CurrentPage - 1) * applicantFilterDto.PageSize;
  19. var context = _httpContextAccessor.HttpContext;
  20. var allApplicants = _context.Applicants
  21. .Include(c => c.Ads)
  22. .Include(x => x.TechnologyApplicants)
  23. .ThenInclude(x => x.Technology);
  24. context.Response.AddHeader("Pagination", allApplicants.ToList().Count);
  25. var filteredApplicants = await allApplicants
  26. .FilterByExperience(applicantFilterDto.MinExperience, applicantFilterDto.MaxExperience)
  27. .FilterByEmploymentType(applicantFilterDto.EmploymentType)
  28. .FilterByDateOfApplication(applicantFilterDto.MinDateOfApplication, applicantFilterDto.MaxDateOfApplication)
  29. .Skip(applicantsToSkip)
  30. .Take(applicantFilterDto.PageSize)
  31. .ToListAsync();
  32. filteredApplicants = filteredApplicants.FilterByTechnologies(applicantFilterDto.Technologies);
  33. return _mapper.Map<List<ApplicantViewDto>>(filteredApplicants);
  34. }
  35. public async Task<ApplicantViewDto> GetById(int id)
  36. {
  37. var applicant = await _context.Applicants
  38. .Include(x => x.Ads)
  39. .ThenInclude(x => x.Technologies)
  40. .Include(x => x.TechnologyApplicants)
  41. .ThenInclude(x => x.Technology)
  42. .Include(x => x.Comments)
  43. .ThenInclude(t => t.User)
  44. .FirstOrDefaultAsync(x => x.ApplicantId == id);
  45. if (applicant is null)
  46. throw new EntityNotFoundException("Applicant not found");
  47. return _mapper.Map<ApplicantViewDto>(applicant);
  48. }
  49. public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  50. {
  51. var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  52. await _context.Applicants.AddAsync(applicant);
  53. await _context.SaveChangesAsync();
  54. }
  55. public async Task DeleteApplicant(int id)
  56. {
  57. var applicant = await _context.Applicants.FindAsync(id);
  58. if (applicant is null)
  59. throw new EntityNotFoundException("Applicant not found");
  60. _context.Applicants.Remove(applicant);
  61. await _context.SaveChangesAsync();
  62. }
  63. public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  64. {
  65. var applicant = await _context.Applicants.FindAsync(id);
  66. if (applicant is null)
  67. throw new EntityNotFoundException("Applicant not found");
  68. _mapper.Map(applicantUpdateDto, applicant);
  69. _context.Entry(applicant).State = EntityState.Modified;
  70. await _context.SaveChangesAsync();
  71. }
  72. public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants()
  73. {
  74. var adsApplicants = await _context.Ads
  75. .Include(a => a.Applicants)
  76. .ThenInclude(a => a.TechnologyApplicants).ThenInclude(a => a.Technology).ToListAsync();
  77. return _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
  78. }
  79. public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
  80. {
  81. var applicant = await _context.Applicants
  82. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
  83. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.Scheduler)
  84. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  85. if (applicant is null)
  86. throw new EntityNotFoundException("Applicant not found");
  87. return _mapper.Map<ApplicantViewDto>(applicant);
  88. }
  89. }
  90. }