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.

PatternService.cs 12KB

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