Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ApplicantService.cs 13KB

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