Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SelectionProcessService.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ILogger<SelectionProcessService> _logger;
  9. public SelectionProcessService(DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
  10. {
  11. _context = context;
  12. _mapper = mapper;
  13. _logger = logger;
  14. }
  15. public async Task<List<SelectionProcessResposneDto>> GetAllAsync()
  16. {
  17. _logger.LogInformation("Start getting all Selection Processes");
  18. _logger.LogInformation("Getting data from DB");
  19. var fromDB = await _context.SelectionProcesses.ToListAsync();
  20. _logger.LogInformation($"Received {fromDB.Count} processes from db.");
  21. _logger.LogInformation($"Mapping received ads to SelectionProcessResposneDto");
  22. var result = _mapper.Map<List<SelectionProcessResposneDto>>(fromDB);
  23. _logger.LogInformation($"Processes has been mapped and received to client: {result.Count} mapped processes");
  24. return result;
  25. }
  26. public async Task<bool> FinishSelectionProcess(SelectionProcessCreateDto model)
  27. {
  28. _logger.LogInformation($"Start finishing selection process with {model.Id}");
  29. var sp = await _context.SelectionProcesses.FindAsync(model.Id);
  30. if (sp is null)
  31. {
  32. _logger.LogError($"Process with id = {model.Id} not found");
  33. throw new EntityNotFoundException("Selection process not found");
  34. }
  35. _logger.LogInformation($"Changing status for {model.Id}");
  36. sp.Status = "Odrađen";
  37. _logger.LogInformation($"Skipping throught levels to come to next level");
  38. var nextLevel = _context.SelectionLevels.AsEnumerable()
  39. .SkipWhile(obj => obj.Id != sp.SelectionLevelId)
  40. .Skip(1).First();
  41. if (nextLevel is null)
  42. {
  43. _logger.LogError($"Applicant is in the last selection level");
  44. throw new EntityNotFoundException("Candidate came to the last selection level");
  45. }
  46. SelectionProcess newProcess = new SelectionProcess
  47. {
  48. Name = model.Name,
  49. SelectionLevelId = nextLevel.Id,
  50. Status = "Čeka na zakazivanje",
  51. ApplicantId = sp.ApplicantId,
  52. SchedulerId = model.SchedulerId
  53. };
  54. _context.SelectionProcesses.Add(newProcess);
  55. _logger.LogInformation($"Create and add new selection process");
  56. var result = await _context.SaveChangesAsync() > 0;
  57. _logger.LogInformation($"Saved changes to db");
  58. return result;
  59. }
  60. public async Task<List<SelectionLevelInfoDto>> GetCountByLevels(List<string> statuses)
  61. {
  62. _logger.LogInformation("Start getting all Selection levels");
  63. var res = await _context.SelectionLevels.Include(n => n.SelectionProcesses).ToListAsync();
  64. _logger.LogInformation($"Received {res.Count} selection levels");
  65. _logger.LogInformation("Mapping levels with process counts to SelectionLevelInfo");
  66. var resMapped = res.Select(n => new SelectionLevelInfoDto
  67. {
  68. Level = n.Name,
  69. CountAll = n.SelectionProcesses.Count,
  70. CountDone = n.SelectionProcesses.Where(n => statuses.Contains(n.Status)).Count()
  71. }).ToList();
  72. return resMapped;
  73. }
  74. public async Task UpdateSelectionProcessStatusAsync(int id, SelectionProcessUpdateStatusDto selectionProcessUpdateStatusDto)
  75. {
  76. _logger.LogInformation($"Start searching Ad with id = {id}");
  77. var selectionProcess = await _context.SelectionProcesses.FindAsync(id);
  78. if (selectionProcess is null)
  79. {
  80. _logger.LogError($"Selection process with id = {id} not found");
  81. throw new EntityNotFoundException("Selection process not found");
  82. }
  83. _logger.LogInformation($"Mapping Selection process with id = {id}");
  84. _mapper.Map(selectionProcessUpdateStatusDto, selectionProcess);
  85. _logger.LogInformation($"Selection process with id = {id} mapped successfully");
  86. _context.Entry(selectionProcess).State = EntityState.Modified;
  87. var result = _context.SaveChangesAsync();
  88. _logger.LogInformation($"Selection process saved to DB");
  89. await result;
  90. }
  91. }
  92. }