Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ApplicantService.cs 4.1KB

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