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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. public ApplicantService(DatabaseContext context, IMapper mapper)
  10. {
  11. _context = context;
  12. _mapper = mapper;
  13. }
  14. public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
  15. {
  16. var allApplicants = _context.Applicants
  17. .Include(c => c.Ads)
  18. .Include(x => x.TechnologyApplicants)
  19. .ThenInclude(x => x.Technology);
  20. var filteredApplicants = await allApplicants
  21. .FilterApplicants(applicantFilterDto);
  22. filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination
  23. {
  24. CurrentPage = applicantFilterDto.CurrentPage,
  25. PageSize = applicantFilterDto.PageSize
  26. });
  27. return new QueryResultDto<ApplicantViewDto>
  28. {
  29. Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants),
  30. Total = allApplicants.ToList().Count
  31. };
  32. }
  33. public async Task<ApplicantViewDto> GetById(int id)
  34. {
  35. var applicant = await _context.Applicants
  36. .Include(x => x.Ads)
  37. .ThenInclude(x => x.Technologies)
  38. .Include(x => x.TechnologyApplicants)
  39. .ThenInclude(x => x.Technology)
  40. .Include(x => x.Comments)
  41. .ThenInclude(t => t.User)
  42. .FirstOrDefaultAsync(x => x.ApplicantId == id);
  43. if (applicant is null)
  44. throw new EntityNotFoundException("Applicant not found");
  45. return _mapper.Map<ApplicantViewDto>(applicant);
  46. }
  47. public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  48. {
  49. var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  50. await _context.Applicants.AddAsync(applicant);
  51. await _context.SaveChangesAsync();
  52. }
  53. public async Task DeleteApplicant(int id)
  54. {
  55. var applicant = await _context.Applicants.FindAsync(id);
  56. if (applicant is null)
  57. throw new EntityNotFoundException("Applicant not found");
  58. _context.Applicants.Remove(applicant);
  59. await _context.SaveChangesAsync();
  60. }
  61. public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  62. {
  63. var applicant = await _context.Applicants.FindAsync(id);
  64. if (applicant is null)
  65. throw new EntityNotFoundException("Applicant not found");
  66. _mapper.Map(applicantUpdateDto, applicant);
  67. _context.Entry(applicant).State = EntityState.Modified;
  68. await _context.SaveChangesAsync();
  69. }
  70. public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants()
  71. {
  72. var adsApplicants = await _context.Ads
  73. .Include(a => a.Applicants)
  74. .ThenInclude(a => a.TechnologyApplicants).ThenInclude(a => a.Technology).ToListAsync();
  75. return _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
  76. }
  77. public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
  78. {
  79. var applicant = await _context.Applicants
  80. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
  81. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.Scheduler)
  82. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  83. if (applicant is null)
  84. throw new EntityNotFoundException("Applicant not found");
  85. return _mapper.Map<ApplicantViewDto>(applicant);
  86. }
  87. }
  88. }