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

SelectionLevelService.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Diligent.WebAPI.Contracts.DTOs.Stats;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class SelectionLevelService : ISelectionLevelService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly ILogger<SelectionLevelService> _logger;
  9. public SelectionLevelService(DatabaseContext context, IMapper mapper, ILogger<SelectionLevelService> logger)
  10. {
  11. _context = context;
  12. _mapper = mapper;
  13. _logger = logger;
  14. }
  15. public async Task<List<SelectionLevelResponseWithDataDto>> GetAllAsync()
  16. {
  17. _logger.LogInformation("Start getting all selection levels");
  18. _logger.LogInformation("Getting data from DB");
  19. var fromDb = await _context.SelectionLevels
  20. .Include(sl => sl.SelectionProcesses)
  21. .ThenInclude(sp => sp.Applicant)
  22. .Include(sl => sl.SelectionProcesses)
  23. .ThenInclude(sp => sp.Scheduler)
  24. .ToListAsync();
  25. _logger.LogInformation($"Received {fromDb.Count} levels from db.");
  26. _logger.LogInformation($"Mapping received ads to SelectionLevelResponseWithDataDto");
  27. var result = _mapper.Map<List<SelectionLevelResponseWithDataDto>>(fromDb);
  28. _logger.LogInformation($"Levels has been mapped and received to client: {result.Count} mapped levels");
  29. return result;
  30. }
  31. public async Task<SelectionLevelResposneDto> GetByIdAsync(int id)
  32. {
  33. _logger.LogInformation($"Start searching Level with id = {id}");
  34. var sl = await _context.SelectionLevels.FindAsync(id);
  35. if (sl is null)
  36. {
  37. _logger.LogError($"Level with id = {id} not found");
  38. throw new EntityNotFoundException("Selection level not found");
  39. }
  40. _logger.LogInformation($"Mapping Level with id = {id} to SelectionLevelResposneDto");
  41. var result = _mapper.Map<SelectionLevelResposneDto>(sl);
  42. _logger.LogInformation($"Level with id = {id} mapped successfully");
  43. return result;
  44. }
  45. public async Task<SelectionLevel> GetByIdEntity(int id)
  46. {
  47. _logger.LogInformation($"Start searching Level with id = {id}");
  48. var sl = await _context.SelectionLevels.FindAsync(id);
  49. if (sl is null)
  50. {
  51. _logger.LogError($"Level with id = {id} not found");
  52. throw new EntityNotFoundException("Selection level not found");
  53. }
  54. _logger.LogInformation($"Level with id = {id} found and returned to client");
  55. return sl;
  56. }
  57. public List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters)
  58. {
  59. _logger.LogInformation("Start getting filtered selection levels");
  60. _logger.LogInformation("Getting data from DB and filter it");
  61. var filteredLevels = _context.SelectionLevels
  62. .Include(x => x.SelectionProcesses)
  63. .ThenInclude(sp => sp.Applicant).ToList()
  64. .FilterLevels(filters);
  65. _logger.LogInformation($"Received {filteredLevels.Count} levels from db.");
  66. _logger.LogInformation($"Mapping received ads to SelectionLevelResponseWithDataDto");
  67. var result = _mapper.Map<List<SelectionLevelResponseWithDataDto>>(filteredLevels);
  68. _logger.LogInformation($"Levels has been mapped and received to client: {result.Count} mapped levels");
  69. return result;
  70. }
  71. public async Task<List<SelectionLevelInfoDto>> GetCountByLevels(List<string> statuses)
  72. {
  73. _logger.LogInformation("Start getting all Selection levels");
  74. var res = await _context.SelectionLevels.Include(n => n.SelectionProcesses).ToListAsync();
  75. _logger.LogInformation($"Received {res.Count} selection levels");
  76. _logger.LogInformation("Mapping levels with process counts to SelectionLevelInfo");
  77. var resMapped = res.Select(n => new SelectionLevelInfoDto
  78. {
  79. Level = n.Name,
  80. CountAll = n.SelectionProcesses.Count,
  81. CountDone = n.SelectionProcesses.Where(n => statuses.Contains(n.Status)).Count()
  82. }).ToList();
  83. return resMapped;
  84. }
  85. }
  86. }