| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using Diligent.WebAPI.Contracts.DTOs.Stats;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class SelectionProcessService : ISelectionProcessService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly ILogger<SelectionProcessService> _logger;
-
- public SelectionProcessService(DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
- {
- _context = context;
- _mapper = mapper;
- _logger = logger;
- }
-
- public async Task<List<SelectionProcessResposneDto>> GetAllAsync()
- {
- _logger.LogInformation("Start getting all Selection Processes");
- _logger.LogInformation("Getting data from DB");
- var fromDB = await _context.SelectionProcesses.ToListAsync();
- _logger.LogInformation($"Received {fromDB.Count} processes from db.");
- _logger.LogInformation($"Mapping received ads to SelectionProcessResposneDto");
- var result = _mapper.Map<List<SelectionProcessResposneDto>>(fromDB);
- _logger.LogInformation($"Processes has been mapped and received to client: {result.Count} mapped processes");
- return result;
- }
- public async Task<bool> FinishSelectionProcess(SelectionProcessCreateDto model)
- {
- _logger.LogInformation($"Start finishing selection process with {model.Id}");
- var sp = await _context.SelectionProcesses.FindAsync(model.Id);
-
- if (sp is null)
- {
- _logger.LogError($"Process with id = {model.Id} not found");
- throw new EntityNotFoundException("Selection process not found");
- }
-
- _logger.LogError($"Changing status for {model.Id}");
- sp.Status = "Odrađen";
-
- _logger.LogError($"Skipping throught levels to come to next level");
- var nextLevel = _context.SelectionLevels.AsEnumerable()
- .SkipWhile(obj => obj.Id != sp.SelectionLevelId)
- .Skip(1).First();
-
- if (nextLevel is null)
- {
- _logger.LogError($"Applicant is in the last selection level");
- throw new EntityNotFoundException("Candidate came to the last selection level");
- }
-
- SelectionProcess newProcess = new SelectionProcess
- {
- Name = model.Name,
- SelectionLevelId = nextLevel.Id,
- Status = "Čeka na zakazivanje",
- ApplicantId = sp.ApplicantId,
- SchedulerId = model.SchedulerId
- };
- _context.SelectionProcesses.Add(newProcess);
- _logger.LogError($"Create and add new selection process");
- var result = await _context.SaveChangesAsync() > 0;
- _logger.LogError($"Saved changes to db");
- 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;
- }
- }
- }
|