Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ApplicantService.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using static Diligent.WebAPI.Data.Entities.Applicant;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class ApplicantService : IApplicantService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly ILogger<ApplicantService> _logger;
  9. public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger)
  10. {
  11. _context = context;
  12. _mapper = mapper;
  13. _logger = logger;
  14. }
  15. public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
  16. {
  17. _logger.LogInformation("Start getting filtered applicants");
  18. _logger.LogInformation("Getting data from DB and filter");
  19. var filteredApplicants = (await _context.Applicants
  20. .Include(c => c.Ads)
  21. .Include(x => x.TechnologyApplicants)
  22. .ThenInclude(x => x.Technology).ToListAsync())
  23. .FilterApplicants(applicantFilterDto);
  24. int totalNumberOfItems = filteredApplicants.Count;
  25. _logger.LogInformation($"Got {totalNumberOfItems} applicants");
  26. filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination
  27. {
  28. CurrentPage = applicantFilterDto.CurrentPage,
  29. PageSize = applicantFilterDto.PageSize
  30. });
  31. _logger.LogInformation($"Return list of applicants");
  32. return new QueryResultDto<ApplicantViewDto>
  33. {
  34. Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants),
  35. Total = totalNumberOfItems
  36. };
  37. }
  38. public async Task<ApplicantViewDto> GetById(int id)
  39. {
  40. _logger.LogInformation($"Start searching Applicant with id = {id}");
  41. var applicant = await _context.Applicants
  42. .Include(x => x.Ads)
  43. .ThenInclude(x => x.Technologies)
  44. .Include(x => x.TechnologyApplicants)
  45. .ThenInclude(x => x.Technology)
  46. .Include(x => x.Comments)
  47. .ThenInclude(t => t.User)
  48. .FirstOrDefaultAsync(x => x.ApplicantId == id);
  49. if (applicant is null)
  50. {
  51. _logger.LogError($"Applicant with id = {id} not found");
  52. throw new EntityNotFoundException("Applicant not found");
  53. }
  54. _logger.LogInformation($"Mapping Applicant with id = {id}");
  55. var result = _mapper.Map<ApplicantViewDto>(applicant);
  56. _logger.LogInformation($"Applicant with id = {id} mapped successfully");
  57. return result;
  58. }
  59. public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
  60. {
  61. var applicant = await _context.Applicants
  62. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
  63. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.Scheduler)
  64. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  65. if (applicant is null)
  66. throw new EntityNotFoundException("Applicant not found");
  67. return _mapper.Map<ApplicantViewDto>(applicant);
  68. }
  69. public async Task DeleteApplicant(int id)
  70. {
  71. _logger.LogInformation($"Start searching Applicant with id = {id}");
  72. var applicant = await _context.Applicants.FindAsync(id);
  73. if (applicant is null)
  74. {
  75. _logger.LogError($"Applicant with id = {id} not found");
  76. throw new EntityNotFoundException("Applicant not found");
  77. }
  78. _logger.LogInformation($"Removing Applicant with id = {id}");
  79. _context.Applicants.Remove(applicant);
  80. var result = _context.SaveChangesAsync();
  81. _logger.LogInformation($"Applicant with id = {id} is removed successfully");
  82. await result;
  83. }
  84. public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto)
  85. {
  86. _logger.LogInformation("Start getting filtered applicants");
  87. _logger.LogInformation("Getting data from DB and filter");
  88. var adsApplicants = (await _context.Ads
  89. .Include(a => a.Applicants)
  90. .ThenInclude(a => a.TechnologyApplicants)
  91. .ThenInclude(a => a.Technology)
  92. .ToListAsync())
  93. .FilterAdApplicants(applicantFilterDto);
  94. _logger.LogInformation($"Got {adsApplicants.Count} ads");
  95. var result = _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
  96. return result;
  97. }
  98. public async Task ApplyForAAd(ApplyForAdRequestDto request)
  99. {
  100. _logger.LogInformation("Start applying for ad");
  101. _logger.LogInformation("Find ad by id");
  102. var ad = await _context.Ads.Where(x => x.Id == request.AdId).FirstOrDefaultAsync();
  103. if (ad == null)
  104. {
  105. _logger.LogError($"Ad with {request.AdId} not found");
  106. throw new EntityNotFoundException("Ad not found in database");
  107. }
  108. _logger.LogInformation($"Find sent technologies from FE in database");
  109. var technologies = await _context.Technologies.Where(x => request.TechnologiesIds.Contains(x.TechnologyId)).ToListAsync();
  110. _logger.LogInformation($"Create applicant instance with sent data");
  111. Applicant applicant = new Applicant
  112. {
  113. FirstName = request.FirstName,
  114. LastName = request.LastName,
  115. Position = ad.Title,
  116. DateOfApplication = DateTime.UtcNow,
  117. CV = request.PdfFile,
  118. Email = request.Email,
  119. PhoneNumber = request.PhoneNumber,
  120. GithubLink = request.GithubLink,
  121. LinkedlnLink = request.LinkedinLink,
  122. BitBucketLink = request.BitBucketLink,
  123. Experience = request.Experience,
  124. //TypeOfEmployment = (EmploymentTypes)Enum.Parse(typeof(EmploymentTypes), ad.EmploymentType, true),
  125. TypeOfEmployment = ad.EmploymentType == EmploymentTypes.Intership ? TypesOfEmployment.Intership : TypesOfEmployment.Posao,
  126. Comments = new(),
  127. Ads = new List<Ad> { ad },
  128. SelectionProcesses = new(),
  129. TechnologyApplicants = new(),
  130. ApplicationChannel = "Putem sajta"
  131. };
  132. _logger.LogInformation($"Saving applicant in database");
  133. await _context.AddAsync(applicant);
  134. await _context.SaveChangesAsync();
  135. _logger.LogInformation($"Applicant saved in database");
  136. _logger.LogInformation($"Saving TechnologyApplicants in database");
  137. for (int i = 0; i < technologies.Count; i++)
  138. {
  139. await _context.ApplicantTechnologies.AddAsync(new TechnologyApplicant { Applicant = applicant, ApplicantId = applicant.ApplicantId, Technology = technologies[i], TechnologyId = technologies[i].TechnologyId });
  140. }
  141. await _context.SaveChangesAsync();
  142. _logger.LogInformation($"TechnologyApplicants saved in database");
  143. }
  144. public async Task<List<ApplicantOptionsDTO>> GetOptions()
  145. {
  146. var res = await _context.Applicants.ToListAsync();
  147. return _mapper.Map<List<ApplicantOptionsDTO>>(res);
  148. }
  149. public async Task<ServiceResponseDTO<object>> InitializeProcess(ApplicantProcessRequestDTO model)
  150. {
  151. var applicant = await _context.Applicants.Include(n => n.SelectionProcesses).Where(n=> n.ApplicantId ==model.ApplicantId).FirstOrDefaultAsync();
  152. if (applicant == null)
  153. return new ServiceResponseDTO<object>
  154. {
  155. IsError = true,
  156. ErrorMessage = "Applicant does not exist."
  157. };
  158. applicant.SelectionProcesses.Add(new SelectionProcess
  159. {
  160. Name = StringGenerator.GenerateRandomPassword(),
  161. SchedulerId = model.SchedulerId,
  162. SelectionLevelId = 1,
  163. Status = model.Appointment != null ? "Zakazan" : "Čeka na zakazivanje",
  164. Date = model.Appointment
  165. });
  166. await _context.SaveChangesAsync();
  167. return new ServiceResponseDTO<object>
  168. {
  169. Data = true
  170. };
  171. }
  172. //public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  173. //{
  174. // var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  175. // await _context.Applicants.AddAsync(applicant);
  176. // await _context.SaveChangesAsync();
  177. //}
  178. //public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  179. //{
  180. // var applicant = await _context.Applicants.FindAsync(id);
  181. // if (applicant is null)
  182. // throw new EntityNotFoundException("Applicant not found");
  183. // _mapper.Map(applicantUpdateDto, applicant);
  184. // _context.Entry(applicant).State = EntityState.Modified;
  185. // await _context.SaveChangesAsync();
  186. //}
  187. }
  188. }