Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ApplicantService.cs 12KB

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