Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PatternService.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using Diligent.WebAPI.Contracts.DTOs.Pattern;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class PatternService : IPatternService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly ISelectionLevelService _selectionLevelService;
  9. private readonly ILogger<PatternService> _logger;
  10. private readonly IEmailer _emailer;
  11. private readonly ISelectionProcessService _selectionProcessService;
  12. public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService, ILogger<PatternService> logger, IEmailer emailer, ISelectionProcessService selectionProcessService)
  13. {
  14. _context = context;
  15. _mapper = mapper;
  16. _selectionLevelService = selectionLevelService;
  17. _logger = logger;
  18. _emailer = emailer;
  19. _selectionProcessService = selectionProcessService;
  20. }
  21. public async Task<List<PatternResponseDto>> GetAllAsync()
  22. {
  23. _logger.LogInformation("Start getting all Patterns");
  24. _logger.LogInformation("Getting data from DB");
  25. var fromDb = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync();
  26. _logger.LogInformation($"Received {fromDb.Count} patterns from db.");
  27. _logger.LogInformation($"Mapping received patterns to PatternResponseDto");
  28. var result = _mapper.Map<List<PatternResponseDto>>(fromDb);
  29. _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns");
  30. return result;
  31. }
  32. public async Task<PatternResponseDto> GetByIdAsync(int id)
  33. {
  34. _logger.LogInformation($"Start searching Pattern with id = {id}");
  35. var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync();
  36. if (pattern is null)
  37. {
  38. _logger.LogError($"Pattern with id = {id} not found");
  39. throw new EntityNotFoundException("Pattern not found");
  40. }
  41. _logger.LogInformation($"Mapping Pattern with id = {id}");
  42. PatternResponseDto result = _mapper.Map<PatternResponseDto>(pattern);
  43. _logger.LogInformation($"Pattern with id = {id} mapped successfully");
  44. return result;
  45. }
  46. public async Task<List<PatternResponseDto>> GetFilteredPatternsAsync(FilterPatternDto filterPatternDto)
  47. {
  48. _logger.LogInformation($"Start getting all filtered Patterns");
  49. _logger.LogInformation("Getting data from DB");
  50. var filteredPatterns = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync();
  51. _logger.LogInformation($"Received {filteredPatterns.Count} patterns from db.");
  52. _logger.LogInformation($"Mapping received patterns to PatternResponseDto");
  53. List<PatternResponseDto> result = _mapper.Map<List<PatternResponseDto>>(filteredPatterns.FilterApplicants(filterPatternDto));
  54. _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns");
  55. return result;
  56. }
  57. public async Task<List<PatternApplicantViewDto>> GetCorrespondingPatternApplicants(int id)
  58. {
  59. _logger.LogInformation($"Start getting corresponding pattern applicants");
  60. _logger.LogInformation($"Getting pattern from database by id");
  61. var pattern = await _context.Patterns.Include(x => x.SelectionLevel).ThenInclude(y => y.SelectionProcesses).ThenInclude(z => z.Applicant).Where(x => x.Id == id).FirstOrDefaultAsync();
  62. if(pattern == null)
  63. {
  64. _logger.LogError($"Pattern with id = {id} not found");
  65. throw new EntityNotFoundException("Pattern not found");
  66. }
  67. _logger.LogInformation($"Select applicants from selection processes with status \"Čeka na zakazivanje\"");
  68. var selectionProcessesApplicants = pattern.SelectionLevel.SelectionProcesses.Where(x => x.Status == "Čeka na zakazivanje").Select(x => x.Applicant).ToList();
  69. _logger.LogInformation($"Mapping Pattern applicants");
  70. var applicants = _mapper.Map<List<PatternApplicantViewDto>>(selectionProcessesApplicants);
  71. _logger.LogInformation($"Pattern applicants mapped successfully");
  72. return applicants;
  73. }
  74. public async Task CreateAsync(PatternCreateDto patternCreateDto)
  75. {
  76. _logger.LogInformation($"Start creating Pattern");
  77. _logger.LogInformation($"Check is Pattern in database");
  78. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync();
  79. if (patternExists is not null)
  80. {
  81. _logger.LogError($"Pattern already exists in database");
  82. throw new EntityNotFoundException("Pattern already exists in database");
  83. }
  84. _logger.LogInformation($"Pattern is not in database");
  85. _logger.LogInformation($"Mapping PatternCreateDto to model");
  86. var pattern = _mapper.Map<Pattern>(patternCreateDto);
  87. _logger.LogInformation($"Pattern mapped successfully");
  88. _logger.LogInformation($"Start searching SelectionLevel with id = {patternCreateDto.SelectionLevelId}");
  89. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);
  90. if(selectionLevel == null)
  91. {
  92. _logger.LogError($"SelectionLevel with id = {patternCreateDto.SelectionLevelId} not found");
  93. throw new EntityNotFoundException("Selection level not found");
  94. }
  95. _logger.LogInformation($"Add founded SelectionLevel to pattern");
  96. pattern.SelectionLevel = selectionLevel;
  97. await _context.AddAsync(pattern);
  98. _logger.LogInformation($"Saving Ad to db...");
  99. var result = _context.SaveChangesAsync();
  100. _logger.LogInformation($"Saved Ad to db...");
  101. await result;
  102. }
  103. public async Task<ScheduleInterviewResponseDto?> ScheduleIntrviewAsync(ScheduleInterviewDto scheduleInterviewDto)
  104. {
  105. _logger.LogInformation($"Start scheduling interview");
  106. List<string> NotSentEmails = new();
  107. _logger.LogInformation("Getting pattern from DB by id");
  108. var pattern = await _context.Patterns.Include(x => x.SelectionLevel).ThenInclude(y => y.SelectionProcesses).ThenInclude(z => z.Applicant).Where(x => x.Id == scheduleInterviewDto.PatternId).FirstOrDefaultAsync();
  109. if (pattern == null)
  110. {
  111. _logger.LogError($"Pattern with id = {scheduleInterviewDto.PatternId} not found");
  112. throw new EntityNotFoundException("Pattern not found");
  113. }
  114. _logger.LogInformation("Pattern found in DB");
  115. for (int i = 0; i < scheduleInterviewDto.Emails.Count; i++)
  116. {
  117. var to = new List<string> { scheduleInterviewDto.Emails[i] };
  118. _logger.LogInformation("Select process where status is \"Čeka na zakazivanje\"");
  119. var selectionProcesses = pattern.SelectionLevel.SelectionProcesses.Where(x => x.Status == "Čeka na zakazivanje" && x.Applicant.Email == scheduleInterviewDto.Emails[i]).FirstOrDefault();
  120. if(selectionProcesses != null)
  121. {
  122. _logger.LogInformation("Selection process is not null");
  123. _logger.LogInformation("Selection process status changing to \"Zakazan\"");
  124. await _selectionProcessService.UpdateSelectionProcessStatusAsync(selectionProcesses.Id, new SelectionProcessUpdateStatusDto
  125. {
  126. Status = "Zakazan"
  127. });
  128. _logger.LogInformation("Sending pattern to selected emails on frontend");
  129. await _emailer.SendEmailAsync(to, "Schedule interview",
  130. HTMLHelper.SuccessfulStep(pattern.Message, pattern.Title, String.Format("{0:M/d/yyyy}", selectionProcesses.Date)), isHtml: true);
  131. }
  132. else
  133. {
  134. _logger.LogInformation("Selection process not found in database");
  135. NotSentEmails.Add(scheduleInterviewDto.Emails[i] + " ne postoji u bazi sa statusom \"Čeka na zakazivanje\" ");
  136. continue;
  137. }
  138. }
  139. if(NotSentEmails.Count() > 0)
  140. {
  141. _logger.LogInformation("List of not set emails are not empty");
  142. _logger.LogInformation("Returning list of not sent emails");
  143. return new ScheduleInterviewResponseDto { NotSentEmails = NotSentEmails };
  144. }
  145. _logger.LogInformation("List of not sent email is empty. Return null...");
  146. return null;
  147. }
  148. public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
  149. {
  150. _logger.LogInformation($"Start updating Pattern");
  151. _logger.LogInformation($"Check is Pattern in database");
  152. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync();
  153. if (patternExists is not null)
  154. {
  155. _logger.LogError($"Pattern already exists in database");
  156. throw new EntityNotFoundException("Pattern already exists in database");
  157. }
  158. _logger.LogInformation($"Start searching Pattern with id = {id}");
  159. var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();
  160. if (pattern is null)
  161. {
  162. _logger.LogError($"Pattern with id = {id} not found");
  163. throw new EntityNotFoundException("Pattern not found");
  164. }
  165. _logger.LogInformation($"Mapping Pattern with id = {id}");
  166. _mapper.Map(patternUpdateDto, pattern);
  167. _logger.LogInformation($"Pattern with id = {id} mapped successfully");
  168. _logger.LogInformation($"Start searching SelectionLevel with id = {patternUpdateDto.SelectionLevelId}");
  169. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);
  170. if (selectionLevel == null)
  171. {
  172. _logger.LogError($"SelectionLevel with id = {patternUpdateDto.SelectionLevelId} not found");
  173. throw new EntityNotFoundException("Selection level not found");
  174. }
  175. _logger.LogInformation($"Add founded SelectionLevel to pattern");
  176. pattern.SelectionLevel = selectionLevel;
  177. _context.Entry(pattern).State = EntityState.Modified;
  178. var result = _context.SaveChangesAsync();
  179. _logger.LogInformation($"Pattern saved to DB");
  180. await result;
  181. }
  182. public async Task DeleteAsync(int id)
  183. {
  184. _logger.LogInformation($"Start searching Pattern with id = {id}");
  185. var pattern = await _context.Patterns.FindAsync(id);
  186. if (pattern is null)
  187. {
  188. _logger.LogError($"Pattern with id = {id} not found");
  189. throw new EntityNotFoundException("Pattern not found");
  190. }
  191. _context.Patterns.Remove(pattern);
  192. var result = _context.SaveChangesAsync();
  193. _logger.LogInformation($"Ad saved to DB");
  194. await result;
  195. }
  196. }
  197. }