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

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