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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. private readonly IUserService _userService;
  10. private readonly IFileService _fileService;
  11. public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger,
  12. IUserService userService,IFileService fileService)
  13. {
  14. _context = context;
  15. _mapper = mapper;
  16. _logger = logger;
  17. _userService = userService;
  18. _fileService = fileService;
  19. }
  20. public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger)
  21. {
  22. _context = context;
  23. _mapper = mapper;
  24. _logger = logger;
  25. }
  26. public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
  27. {
  28. _logger.LogInformation("Start getting filtered applicants");
  29. _logger.LogInformation("Getting data from DB and filter");
  30. var filteredApplicants = (await _context.Applicants
  31. .Include(c => c.Ads)
  32. .Include(x => x.TechnologyApplicants)
  33. .ThenInclude(x => x.Technology).ToListAsync())
  34. .FilterApplicants(applicantFilterDto);
  35. int totalNumberOfItems = filteredApplicants.Count;
  36. _logger.LogInformation($"Got {totalNumberOfItems} applicants");
  37. filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination
  38. {
  39. CurrentPage = applicantFilterDto.CurrentPage,
  40. PageSize = applicantFilterDto.PageSize
  41. });
  42. _logger.LogInformation($"Return list of applicants");
  43. return new QueryResultDto<ApplicantViewDto>
  44. {
  45. Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants),
  46. Total = totalNumberOfItems
  47. };
  48. }
  49. public async Task<ApplicantViewDto> GetById(int id)
  50. {
  51. _logger.LogInformation($"Start searching Applicant with id = {id}");
  52. var applicant = await _context.Applicants
  53. .Include(x => x.Ads)
  54. .ThenInclude(x => x.Technologies)
  55. .Include(x => x.TechnologyApplicants)
  56. .ThenInclude(x => x.Technology)
  57. .Include(x => x.Comments)
  58. .ThenInclude(t => t.User)
  59. .FirstOrDefaultAsync(x => x.ApplicantId == id);
  60. if (applicant is null)
  61. {
  62. _logger.LogError($"Applicant with id = {id} not found");
  63. throw new EntityNotFoundException("Applicant not found");
  64. }
  65. _logger.LogInformation($"Mapping Applicant with id = {id}");
  66. var result = _mapper.Map<ApplicantViewDto>(applicant);
  67. result.CV = await _fileService.GetCV("638077305621281656.pdf");
  68. _logger.LogInformation($"Applicant with id = {id} mapped successfully");
  69. return result;
  70. }
  71. public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
  72. {
  73. var applicant = await _context.Applicants
  74. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
  75. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.Scheduler)
  76. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  77. if (applicant is null)
  78. throw new EntityNotFoundException("Applicant not found");
  79. return _mapper.Map<ApplicantViewDto>(applicant);
  80. }
  81. public async Task DeleteApplicant(int id)
  82. {
  83. _logger.LogInformation($"Start searching Applicant with id = {id}");
  84. var applicant = await _context.Applicants.FindAsync(id);
  85. if (applicant is null)
  86. {
  87. _logger.LogError($"Applicant with id = {id} not found");
  88. throw new EntityNotFoundException("Applicant not found");
  89. }
  90. _logger.LogInformation($"Removing Applicant with id = {id}");
  91. _context.Applicants.Remove(applicant);
  92. var result = _context.SaveChangesAsync();
  93. _logger.LogInformation($"Applicant with id = {id} is removed successfully");
  94. await result;
  95. }
  96. public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto)
  97. {
  98. _logger.LogInformation("Start getting filtered applicants");
  99. _logger.LogInformation("Getting data from DB and filter");
  100. var adsApplicants = (await _context.Ads
  101. .Include(a => a.Applicants)
  102. .ThenInclude(a => a.TechnologyApplicants)
  103. .ThenInclude(a => a.Technology)
  104. .ToListAsync())
  105. .FilterAdApplicants(applicantFilterDto);
  106. _logger.LogInformation($"Got {adsApplicants.Count} ads");
  107. var result = _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
  108. return result;
  109. }
  110. public async Task ApplyForAd(ApplyForAdRequestDto request)
  111. {
  112. string fileName = string.Format(@"{0}.pdf", DateTime.Now.Ticks);
  113. await _fileService.UploadCV(fileName, request.PdfFile);
  114. _logger.LogInformation("Start applying for ad");
  115. _logger.LogInformation("Find ad by id");
  116. var ad = await _context.Ads.Where(x => x.Id == request.AdId).FirstOrDefaultAsync();
  117. if (ad == null)
  118. {
  119. _logger.LogError($"Ad with {request.AdId} not found");
  120. throw new EntityNotFoundException("Ad not found in database");
  121. }
  122. _logger.LogInformation($"Find sent technologies from FE in database");
  123. var technologies = await _context.Technologies.Where(x => request.TechnologiesIds.Contains(x.TechnologyId)).ToListAsync();
  124. _logger.LogInformation($"Create applicant instance with sent data");
  125. Applicant applicant = new()
  126. {
  127. FirstName = request.FirstName,
  128. LastName = request.LastName,
  129. Position = ad.Title,
  130. DateOfApplication = DateTime.Now,
  131. CV = fileName,
  132. Email = request.Email,
  133. PhoneNumber = request.PhoneNumber,
  134. GithubLink = request.GithubLink,
  135. LinkedlnLink = request.LinkedinLink,
  136. BitBucketLink = request.BitBucketLink,
  137. Experience = request.Experience,
  138. //TypeOfEmployment = (EmploymentTypes)Enum.Parse(typeof(EmploymentTypes), ad.EmploymentType, true),
  139. TypeOfEmployment = ad.EmploymentType == EmploymentTypes.Intership ? TypesOfEmployment.Intership : TypesOfEmployment.Posao,
  140. Comments = new(),
  141. Ads = new List<Ad> { ad },
  142. SelectionProcesses = new(),
  143. TechnologyApplicants = new(),
  144. ApplicationChannel = "Putem sajta"
  145. };
  146. _logger.LogInformation($"Saving applicant in database");
  147. await _context.Applicants.AddAsync(applicant);
  148. var res = await _context.SaveChangesAsync();
  149. _logger.LogInformation($"Applicant saved in database");
  150. _logger.LogInformation($"Saving TechnologyApplicants in database");
  151. for (int i = 0; i < technologies.Count; i++)
  152. {
  153. await _context.ApplicantTechnologies.AddAsync(new TechnologyApplicant { Applicant = applicant, ApplicantId = applicant.ApplicantId, Technology = technologies[i], TechnologyId = technologies[i].TechnologyId });
  154. }
  155. await _context.SaveChangesAsync();
  156. _logger.LogInformation($"TechnologyApplicants saved in database");
  157. }
  158. public async Task ImportApplicant(List<ApplicantImportDto> requests)
  159. {
  160. _logger.LogInformation($"Create applicant instance with sent data");
  161. var res = new List<Applicant>();
  162. var user = await _userService.GetFirst();
  163. foreach (var request in requests) {
  164. Applicant applicant = new Applicant
  165. {
  166. FirstName = request.FirstName ?? "",
  167. LastName = request.LastName ?? "",
  168. CV = request.CV ?? "",
  169. Email = request.Email ?? "",
  170. PhoneNumber = request.PhoneNumber ?? "",
  171. GithubLink = request.GithubLink ?? "",
  172. LinkedlnLink = request.LinkedlnLink ?? "",
  173. BitBucketLink = request.BitBucketLink ?? "",
  174. Position = "",
  175. DateOfApplication = request.DateOfApplication,
  176. TypeOfEmployment = request.TypeOfEmployment == "Praksa" ? TypesOfEmployment.Intership : TypesOfEmployment.Posao,
  177. Experience = request.Experience,
  178. ApplicationChannel = request.ApplicationChannel ?? "Putem sajta",
  179. // MORA DA SE UVEDE KO JE DAO KOMENTAR DA LI DA STAVIMO DA JE DANIJELA SVIMA STAVILA KOMENTARE ILI ??
  180. Comments = new List<Comment>
  181. {
  182. new Comment
  183. {
  184. User = user,
  185. Content = request.Comment
  186. }
  187. },
  188. Ads = new List<Ad> { request.Ad },
  189. SelectionProcesses = new(),
  190. TechnologyApplicants = new()
  191. };
  192. res.Add(applicant);
  193. }
  194. await _context.AddRangeAsync(res);
  195. await _context.SaveChangesAsync();
  196. _logger.LogInformation($"Saving applicant in database");
  197. _logger.LogInformation($"Applicant saved in database");
  198. }
  199. public async Task<List<ApplicantOptionsDTO>> GetOptions()
  200. {
  201. var res = await _context.Applicants.ToListAsync();
  202. return _mapper.Map<List<ApplicantOptionsDTO>>(res);
  203. }
  204. public async Task<ServiceResponseDTO<object>> InitializeProcess(ApplicantProcessRequestDTO model)
  205. {
  206. var applicant = await _context.Applicants.Include(n => n.SelectionProcesses).Where(n=> n.ApplicantId ==model.ApplicantId).FirstOrDefaultAsync();
  207. if (applicant == null)
  208. return new ServiceResponseDTO<object>
  209. {
  210. IsError = true,
  211. ErrorMessage = "Applicant does not exist."
  212. };
  213. applicant.SelectionProcesses.Add(new SelectionProcess
  214. {
  215. Name = StringGenerator.GenerateRandomPassword(),
  216. SchedulerId = model.SchedulerId,
  217. SelectionLevelId = 1,
  218. Status = model.Appointment != null ? "Zakazan" : "Čeka na zakazivanje",
  219. Date = model.Appointment
  220. });
  221. await _context.SaveChangesAsync();
  222. return new ServiceResponseDTO<object>
  223. {
  224. Data = true
  225. };
  226. }
  227. }
  228. }