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.

SelectionProcessService.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. var nextLevel = _context.SelectionLevels.AsEnumerable()
  41. .SkipWhile(obj => obj.Id != sp.SelectionLevelId)
  42. .Skip(1).First();
  43. if (nextLevel is null)
  44. {
  45. _logger.LogError($"Applicant is in the last selection level");
  46. throw new EntityNotFoundException("Candidate came to the last selection level");
  47. // ovde sacuvati promene i return odraditi
  48. // da ne bi pravilo novi proces
  49. // exception treba ukloniti
  50. }
  51. SelectionProcess newProcess = new SelectionProcess
  52. {
  53. Name = model.Name,
  54. SelectionLevelId = nextLevel.Id,
  55. Status = "Čeka na zakazivanje",
  56. ApplicantId = sp.ApplicantId,
  57. //SchedulerId = model.SchedulerId
  58. };
  59. _context.SelectionProcesses.Add(newProcess);
  60. _logger.LogInformation($"Create and add new selection process");
  61. var result = await _context.SaveChangesAsync() > 0;
  62. _logger.LogInformation($"Saved changes to db");
  63. return result;
  64. }
  65. public async Task<List<SelectionLevelInfoDto>> GetCountByLevels(List<string> statuses)
  66. {
  67. _logger.LogInformation("Start getting all Selection levels");
  68. var res = await _context.SelectionLevels.Include(n => n.SelectionProcesses).ToListAsync();
  69. _logger.LogInformation($"Received {res.Count} selection levels");
  70. _logger.LogInformation("Mapping levels with process counts to SelectionLevelInfo");
  71. var resMapped = res.Select(n => new SelectionLevelInfoDto
  72. {
  73. Level = n.Name,
  74. CountAll = n.SelectionProcesses.Count,
  75. CountDone = n.SelectionProcesses.Where(n => statuses.Contains(n.Status)).Count()
  76. }).ToList();
  77. return resMapped;
  78. }
  79. public async Task UpdateSelectionProcessStatusAsync(int id, SelectionProcessUpdateStatusDto selectionProcessUpdateStatusDto)
  80. {
  81. _logger.LogInformation($"Start searching Ad with id = {id}");
  82. var selectionProcess = await _context.SelectionProcesses.FindAsync(id);
  83. if (selectionProcess is null)
  84. {
  85. _logger.LogError($"Selection process with id = {id} not found");
  86. throw new EntityNotFoundException("Selection process not found");
  87. }
  88. _logger.LogInformation($"Mapping Selection process with id = {id}");
  89. _mapper.Map(selectionProcessUpdateStatusDto, selectionProcess);
  90. _logger.LogInformation($"Selection process with id = {id} mapped successfully");
  91. _context.Entry(selectionProcess).State = EntityState.Modified;
  92. var result = _context.SaveChangesAsync();
  93. _logger.LogInformation($"Selection process saved to DB");
  94. await result;
  95. }
  96. public async Task StatusUpdate(StatusChangeDTO model)
  97. {
  98. var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
  99. if (process is null)
  100. throw new EntityNotFoundException("Process does not exist.");
  101. if (model.NewStatus == "Zakazan" && model.SchedulerId is not null)
  102. {
  103. var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
  104. if (user is null)
  105. throw new EntityNotFoundException("Scheduler does not exist.");
  106. process.SchedulerId = user.Id;
  107. }
  108. process.Status = model.NewStatus;
  109. process.Date = model.Appointment;
  110. await _context.SaveChangesAsync();
  111. }
  112. }
  113. }