Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

SelectionProcessService.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Diligent.WebAPI.Contracts.DTOs.Stats;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class SelectionProcessService : ISelectionProcessService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly UserManager<User> _userManager;
  9. private readonly ILogger<SelectionProcessService> _logger;
  10. public SelectionProcessService(UserManager<User> userManager,DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
  11. {
  12. _context = context;
  13. _mapper = mapper;
  14. _logger = logger;
  15. _userManager = userManager;
  16. }
  17. public async Task<List<SelectionProcessResposneDto>> GetAllAsync()
  18. {
  19. _logger.LogInformation("Start getting all Selection Processes");
  20. _logger.LogInformation("Getting data from DB");
  21. var fromDB = await _context.SelectionProcesses.ToListAsync();
  22. _logger.LogInformation($"Received {fromDB.Count} processes from db.");
  23. _logger.LogInformation($"Mapping received ads to SelectionProcessResposneDto");
  24. var result = _mapper.Map<List<SelectionProcessResposneDto>>(fromDB);
  25. _logger.LogInformation($"Processes has been mapped and received to client: {result.Count} mapped processes");
  26. return result;
  27. }
  28. public async Task<bool> FinishSelectionProcess(SelectionProcessCreateDto model)
  29. {
  30. _logger.LogInformation($"Start finishing selection process with {model.Id}");
  31. var sp = await _context.SelectionProcesses.FindAsync(model.Id);
  32. if (sp is null)
  33. {
  34. _logger.LogError($"Process with id = {model.Id} not found");
  35. throw new EntityNotFoundException("Selection process not found");
  36. }
  37. _logger.LogInformation($"Changing status for {model.Id}");
  38. sp.Status = "Odrađen";
  39. _logger.LogInformation($"Skipping throught levels to come to next level");
  40. if (sp.SelectionLevelId == _context.SelectionLevels.Last().Id)
  41. {
  42. _logger.LogError($"Applicant is in the last selection level");
  43. throw new EntityNotFoundException("Candidate came to the last selection level");
  44. }
  45. var nextLevel = _context.SelectionLevels.AsEnumerable()
  46. .SkipWhile(obj => obj.Id != sp.SelectionLevelId)
  47. .Skip(1).First();
  48. SelectionProcess newProcess = new SelectionProcess
  49. {
  50. Name = model.Name,
  51. SelectionLevelId = nextLevel.Id,
  52. Status = "Čeka na zakazivanje",
  53. ApplicantId = sp.ApplicantId
  54. };
  55. _context.SelectionProcesses.Add(newProcess);
  56. _logger.LogInformation($"Create and add new selection process");
  57. var result = await _context.SaveChangesAsync() > 0;
  58. _logger.LogInformation($"Saved changes to db");
  59. return result;
  60. }
  61. public async Task UpdateSelectionProcessStatusAsync(int id, SelectionProcessUpdateStatusDto selectionProcessUpdateStatusDto)
  62. {
  63. _logger.LogInformation($"Start searching Ad with id = {id}");
  64. var selectionProcess = await _context.SelectionProcesses.FindAsync(id);
  65. if (selectionProcess is null)
  66. {
  67. _logger.LogError($"Selection process with id = {id} not found");
  68. throw new EntityNotFoundException("Selection process not found");
  69. }
  70. _logger.LogInformation($"Mapping Selection process with id = {id}");
  71. _mapper.Map(selectionProcessUpdateStatusDto, selectionProcess);
  72. _logger.LogInformation($"Selection process with id = {id} mapped successfully");
  73. _context.Entry(selectionProcess).State = EntityState.Modified;
  74. var result = _context.SaveChangesAsync();
  75. _logger.LogInformation($"Selection process saved to DB");
  76. await result;
  77. }
  78. public async Task StatusUpdate(StatusChangeDTO model)
  79. {
  80. _logger.LogInformation($"Start searching process with id = {model.ProcessId}");
  81. var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
  82. if (process is null)
  83. {
  84. _logger.LogError($"Process with id = {model.ProcessId} not found");
  85. throw new EntityNotFoundException("Process does not exist.");
  86. }
  87. _logger.LogInformation($"Check which status is going to be set.");
  88. if (model.NewStatus == "Zakazan" && model.SchedulerId is not null)
  89. {
  90. _logger.LogInformation($"Start searching user with id = {model.SchedulerId}");
  91. var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
  92. if (user is null)
  93. throw new EntityNotFoundException("Scheduler does not exist.");
  94. _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}");
  95. process.SchedulerId = user.Id;
  96. }
  97. process.Status = model.NewStatus;
  98. process.Date = model.Appointment;
  99. process.Comment = model.Comment;
  100. _logger.LogInformation($"Processing changes.");
  101. await _context.SaveChangesAsync();
  102. }
  103. public async Task InterviewerUpdate(InterviewerUpdateDTO model)
  104. {
  105. _logger.LogInformation($"Start searching process with id = {model.ProcessId}");
  106. var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
  107. if (process is null)
  108. throw new EntityNotFoundException("Process does not exist.");
  109. _logger.LogInformation($"Start searching user with id = {model.SchedulerId}");
  110. var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
  111. if (user is null)
  112. throw new EntityNotFoundException("Scheduler does not exist.");
  113. _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}");
  114. process.SchedulerId = user.Id;
  115. _logger.LogInformation("Processing changes...");
  116. await _context.SaveChangesAsync();
  117. }
  118. }
  119. }