| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using Diligent.WebAPI.Contracts.DTOs.Stats;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class SelectionLevelService : ISelectionLevelService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly ILogger<SelectionLevelService> _logger;
-
- public SelectionLevelService(DatabaseContext context, IMapper mapper, ILogger<SelectionLevelService> logger)
- {
- _context = context;
- _mapper = mapper;
- _logger = logger;
- }
-
- public async Task<List<SelectionLevelResponseWithDataDto>> GetAllAsync()
- {
- _logger.LogInformation("Start getting all selection levels");
- _logger.LogInformation("Getting data from DB");
- var fromDb = await _context.SelectionLevels
- .Include(sl => sl.SelectionProcesses)
- .ThenInclude(sp => sp.Applicant)
- .Include(sl => sl.SelectionProcesses)
- .ThenInclude(sp => sp.Scheduler)
- .ToListAsync();
-
- _logger.LogInformation($"Received {fromDb.Count} levels from db.");
- _logger.LogInformation($"Mapping received ads to SelectionLevelResponseWithDataDto");
- var result = _mapper.Map<List<SelectionLevelResponseWithDataDto>>(fromDb);
- _logger.LogInformation($"Levels has been mapped and received to client: {result.Count} mapped levels");
-
- return result;
- }
- public async Task<SelectionLevelResposneDto> GetByIdAsync(int id)
- {
- _logger.LogInformation($"Start searching Level with id = {id}");
- var sl = await _context.SelectionLevels.FindAsync(id);
-
- if (sl is null)
- {
- _logger.LogError($"Level with id = {id} not found");
- throw new EntityNotFoundException("Selection level not found");
- }
-
- _logger.LogInformation($"Mapping Level with id = {id} to SelectionLevelResposneDto");
- var result = _mapper.Map<SelectionLevelResposneDto>(sl);
- _logger.LogInformation($"Level with id = {id} mapped successfully");
- return result;
-
- }
-
- public async Task<SelectionLevel> GetByIdEntity(int id)
- {
- _logger.LogInformation($"Start searching Level with id = {id}");
- var sl = await _context.SelectionLevels.FindAsync(id);
-
- if (sl is null)
- {
- _logger.LogError($"Level with id = {id} not found");
- throw new EntityNotFoundException("Selection level not found");
- }
-
- _logger.LogInformation($"Level with id = {id} found and returned to client");
- return sl;
- }
-
- public List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters)
- {
- _logger.LogInformation("Start getting filtered selection levels");
- _logger.LogInformation("Getting data from DB and filter it");
- var filteredLevels = _context.SelectionLevels
- .Include(x => x.SelectionProcesses)
- .ThenInclude(sp => sp.Applicant).ToList()
- .FilterLevels(filters);
-
- _logger.LogInformation($"Received {filteredLevels.Count} levels from db.");
- _logger.LogInformation($"Mapping received ads to SelectionLevelResponseWithDataDto");
- var result = _mapper.Map<List<SelectionLevelResponseWithDataDto>>(filteredLevels);
- _logger.LogInformation($"Levels has been mapped and received to client: {result.Count} mapped levels");
-
- return result;
- }
-
- public async Task<List<SelectionLevelInfoDto>> GetCountByLevels(List<string> statuses)
- {
- _logger.LogInformation("Start getting all Selection levels");
- var res = await _context.SelectionLevels.Include(n => n.SelectionProcesses).ToListAsync();
-
- _logger.LogInformation($"Received {res.Count} selection levels");
- _logger.LogInformation("Mapping levels with process counts to SelectionLevelInfo");
- var resMapped = res.Select(n => new SelectionLevelInfoDto
- {
- Level = n.Name,
- CountAll = n.SelectionProcesses.Count,
- CountDone = n.SelectionProcesses.Where(n => statuses.Contains(n.Status)).Count()
- }).ToList();
-
- return resMapped;
- }
- }
- }
|