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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. namespace Diligent.WebAPI.Business.Services
  2. {
  3. public class ApplicantService : IApplicantService
  4. {
  5. private readonly DatabaseContext _context;
  6. private readonly IMapper _mapper;
  7. private readonly ILogger<ApplicantService> _logger;
  8. public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger)
  9. {
  10. _context = context;
  11. _mapper = mapper;
  12. _logger = logger;
  13. }
  14. public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
  15. {
  16. _logger.LogInformation("Start getting filtered applicants");
  17. _logger.LogInformation("Getting data from DB and filter");
  18. var filteredApplicants = (await _context.Applicants
  19. .Include(c => c.Ads)
  20. .Include(x => x.TechnologyApplicants)
  21. .ThenInclude(x => x.Technology).ToListAsync())
  22. .FilterApplicants(applicantFilterDto);
  23. int totalNumberOfItems = filteredApplicants.Count;
  24. _logger.LogInformation($"Got {totalNumberOfItems} applicants");
  25. filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination
  26. {
  27. CurrentPage = applicantFilterDto.CurrentPage,
  28. PageSize = applicantFilterDto.PageSize
  29. });
  30. _logger.LogInformation($"Return list of applicants");
  31. return new QueryResultDto<ApplicantViewDto>
  32. {
  33. Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants),
  34. Total = totalNumberOfItems
  35. };
  36. }
  37. public async Task<ApplicantViewDto> GetById(int id)
  38. {
  39. _logger.LogInformation($"Start searching Applicant with id = {id}");
  40. var applicant = await _context.Applicants
  41. .Include(x => x.Ads)
  42. .ThenInclude(x => x.Technologies)
  43. .Include(x => x.TechnologyApplicants)
  44. .ThenInclude(x => x.Technology)
  45. .Include(x => x.Comments)
  46. .ThenInclude(t => t.User)
  47. .FirstOrDefaultAsync(x => x.ApplicantId == id);
  48. if (applicant is null)
  49. {
  50. _logger.LogError($"Applicant with id = {id} not found");
  51. throw new EntityNotFoundException("Applicant not found");
  52. }
  53. _logger.LogInformation($"Mapping Applicant with id = {id}");
  54. var result = _mapper.Map<ApplicantViewDto>(applicant);
  55. _logger.LogInformation($"Applicant with id = {id} mapped successfully");
  56. return result;
  57. }
  58. public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
  59. {
  60. var applicant = await _context.Applicants
  61. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
  62. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.Scheduler)
  63. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  64. if (applicant is null)
  65. throw new EntityNotFoundException("Applicant not found");
  66. return _mapper.Map<ApplicantViewDto>(applicant);
  67. }
  68. public async Task DeleteApplicant(int id)
  69. {
  70. _logger.LogInformation($"Start searching Applicant with id = {id}");
  71. var applicant = await _context.Applicants.FindAsync(id);
  72. if (applicant is null)
  73. {
  74. _logger.LogError($"Applicant with id = {id} not found");
  75. throw new EntityNotFoundException("Applicant not found");
  76. }
  77. _logger.LogInformation($"Removing Applicant with id = {id}");
  78. _context.Applicants.Remove(applicant);
  79. var result = _context.SaveChangesAsync();
  80. _logger.LogInformation($"Applicant with id = {id} is removed successfully");
  81. await result;
  82. }
  83. public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto)
  84. {
  85. _logger.LogInformation("Start getting filtered applicants");
  86. _logger.LogInformation("Getting data from DB and filter");
  87. var adsApplicants = (await _context.Ads
  88. .Include(a => a.Applicants)
  89. .ThenInclude(a => a.TechnologyApplicants)
  90. .ThenInclude(a => a.Technology)
  91. .ToListAsync())
  92. .FilterAdApplicants(applicantFilterDto);
  93. _logger.LogInformation($"Got {adsApplicants.Count} ads");
  94. var result = _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
  95. return result;
  96. }
  97. public async Task<List<ApplicantOptionsDTO>> GetOptions()
  98. {
  99. var res = await _context.Applicants.ToListAsync();
  100. return _mapper.Map<List<ApplicantOptionsDTO>>(res);
  101. }
  102. public async Task<ServiceResponseDTO<object>> InitializeProcess(ApplicantProcessRequestDTO model)
  103. {
  104. var applicant = await _context.Applicants.Include(n => n.SelectionProcesses).Where(n=> n.ApplicantId ==model.ApplicantId).FirstOrDefaultAsync();
  105. if (applicant == null)
  106. return new ServiceResponseDTO<object>
  107. {
  108. IsError = true,
  109. ErrorMessage = "Applicant does not exist."
  110. };
  111. applicant.SelectionProcesses.Add(new SelectionProcess
  112. {
  113. Name = StringGenerator.GenerateRandomPassword(),
  114. SchedulerId = model.SchedulerId,
  115. SelectionLevelId = 1,
  116. Status = model.Appointment != null ? "Zakazan" : "Čeka na zakazivanje",
  117. Date = model.Appointment
  118. });
  119. await _context.SaveChangesAsync();
  120. return new ServiceResponseDTO<object>
  121. {
  122. Data = true
  123. };
  124. }
  125. //public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  126. //{
  127. // var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  128. // await _context.Applicants.AddAsync(applicant);
  129. // await _context.SaveChangesAsync();
  130. //}
  131. //public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  132. //{
  133. // var applicant = await _context.Applicants.FindAsync(id);
  134. // if (applicant is null)
  135. // throw new EntityNotFoundException("Applicant not found");
  136. // _mapper.Map(applicantUpdateDto, applicant);
  137. // _context.Entry(applicant).State = EntityState.Modified;
  138. // await _context.SaveChangesAsync();
  139. //}
  140. }
  141. }