Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ApplicantService.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. Gender = request.Gender == "Muski" ? Genders.M : Genders.Z,
  154. ProfessionalQualification = request.ProfessionalQualification
  155. };
  156. _logger.LogInformation($"Saving applicant in database");
  157. await _context.Applicants.AddAsync(applicant);
  158. var res = await _context.SaveChangesAsync();
  159. _logger.LogInformation($"Applicant saved in database");
  160. _logger.LogInformation($"Saving TechnologyApplicants in database");
  161. for (int i = 0; i < technologies.Count; i++)
  162. {
  163. await _context.ApplicantTechnologies.AddAsync(new TechnologyApplicant { Applicant = applicant, ApplicantId = applicant.ApplicantId, Technology = technologies[i], TechnologyId = technologies[i].TechnologyId });
  164. }
  165. await _context.SaveChangesAsync();
  166. _logger.LogInformation($"TechnologyApplicants saved in database");
  167. }
  168. public async Task ImportApplicant(List<ApplicantImportDto> requests)
  169. {
  170. _logger.LogInformation($"Create applicant instance with sent data");
  171. var res = new List<Applicant>();
  172. _logger.LogInformation($"Get first user from database");
  173. var user = await _userService.GetFirst();
  174. _logger.LogInformation($"User succesufully fetched from database");
  175. foreach (var request in requests) {
  176. Applicant applicant = new Applicant
  177. {
  178. FirstName = request.FirstName ?? "",
  179. LastName = request.LastName ?? "",
  180. CV = request.CV ?? "",
  181. Email = request.Email ?? "",
  182. PhoneNumber = request.PhoneNumber ?? "",
  183. GithubLink = request.GithubLink ?? "",
  184. LinkedlnLink = request.LinkedlnLink ?? "",
  185. BitBucketLink = request.BitBucketLink ?? "",
  186. Position = "",
  187. DateOfApplication = request.DateOfApplication,
  188. TypeOfEmployment = request.TypeOfEmployment == "Praksa" ? TypesOfEmployment.Intership : TypesOfEmployment.Posao,
  189. Experience = request.Experience,
  190. ApplicationChannel = request.ApplicationChannel ?? "Putem sajta",
  191. // MORA DA SE UVEDE KO JE DAO KOMENTAR DA LI DA STAVIMO DA JE DANIJELA SVIMA STAVILA KOMENTARE ILI ??
  192. Comments = new List<Comment>
  193. {
  194. new Comment
  195. {
  196. User = user,
  197. Content = request.Comment
  198. }
  199. },
  200. Ads = new List<Ad> { request.Ad },
  201. SelectionProcesses = new(),
  202. TechnologyApplicants = new(),
  203. Gender = Genders.M,
  204. ProfessionalQualification = "Elektrotehnicki fakultet",
  205. };
  206. res.Add(applicant);
  207. }
  208. _logger.LogInformation($"Saving applicants in database");
  209. await _context.AddRangeAsync(res);
  210. await _context.SaveChangesAsync();
  211. _logger.LogInformation($"Applicants saved in database");
  212. }
  213. public async Task<List<ApplicantOptionsDTO>> GetOptions()
  214. {
  215. _logger.LogInformation($"Start getting all applicants from database");
  216. var res = await _context.Applicants.ToListAsync();
  217. _logger.LogInformation($"Got {res.Count} applicants");
  218. return _mapper.Map<List<ApplicantOptionsDTO>>(res);
  219. }
  220. public async Task<ServiceResponseDTO<object>> InitializeProcess(ApplicantProcessRequestDTO model)
  221. {
  222. _logger.LogInformation($"Start searching Applicant with id = {model.ApplicantId}");
  223. var applicant = await _context.Applicants.Include(n => n.SelectionProcesses).Where(n=> n.ApplicantId ==model.ApplicantId).FirstOrDefaultAsync();
  224. if (applicant == null)
  225. {
  226. _logger.LogError($"Applicant with id = {model.ApplicantId} not found");
  227. return new ServiceResponseDTO<object>
  228. {
  229. IsError = true,
  230. ErrorMessage = "Applicant does not exist."
  231. };
  232. }
  233. applicant.SelectionProcesses.Add(new SelectionProcess
  234. {
  235. Name = StringGenerator.GenerateRandomPassword(),
  236. SchedulerId = model.SchedulerId,
  237. SelectionLevelId = 1,
  238. Status = model.Appointment != null ? "Zakazan" : "Čeka na zakazivanje",
  239. Date = model.Appointment
  240. });
  241. _logger.LogInformation($"Saving selection processes in database");
  242. await _context.SaveChangesAsync();
  243. _logger.LogInformation($"Selecetion processes saved in database");
  244. return new ServiceResponseDTO<object>
  245. {
  246. Data = true
  247. };
  248. }
  249. }
  250. }