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 UserManager _userManager; private readonly ILogger _logger; public SelectionProcessService(UserManager userManager,DatabaseContext context, IMapper mapper, ILogger logger) { _context = context; _mapper = mapper; _logger = logger; _userManager = userManager; } public async Task> 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>(fromDB); _logger.LogInformation($"Processes has been mapped and received to client: {result.Count} mapped processes"); return result; } public async Task 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.LogInformation($"Changing status for {model.Id}"); sp.Status = "Odrađen"; _logger.LogInformation($"Skipping throught levels to come to next level"); if (sp.SelectionLevelId == _context.SelectionLevels.OrderBy(n => n.Id).Last().Id) { _logger.LogError($"Applicant is in the last selection level"); throw new EntityNotFoundException("Candidate came to the last selection level"); } var nextLevel = _context.SelectionLevels.AsEnumerable() .SkipWhile(obj => obj.Id != sp.SelectionLevelId) .Skip(1).First(); SelectionProcess newProcess = new SelectionProcess { Name = model.Name, SelectionLevelId = nextLevel.Id, Status = "Čeka na zakazivanje", ApplicantId = sp.ApplicantId }; _context.SelectionProcesses.Add(newProcess); _logger.LogInformation($"Create and add new selection process"); var result = await _context.SaveChangesAsync() > 0; _logger.LogInformation($"Saved changes to db"); return result; } public async Task UpdateSelectionProcessStatusAsync(int id, SelectionProcessUpdateStatusDto selectionProcessUpdateStatusDto) { _logger.LogInformation($"Start searching Ad with id = {id}"); var selectionProcess = await _context.SelectionProcesses.FindAsync(id); if (selectionProcess is null) { _logger.LogError($"Selection process with id = {id} not found"); throw new EntityNotFoundException("Selection process not found"); } _logger.LogInformation($"Mapping Selection process with id = {id}"); _mapper.Map(selectionProcessUpdateStatusDto, selectionProcess); _logger.LogInformation($"Selection process with id = {id} mapped successfully"); _context.Entry(selectionProcess).State = EntityState.Modified; var result = _context.SaveChangesAsync(); _logger.LogInformation($"Selection process saved to DB"); await result; } public async Task StatusUpdate(StatusChangeDTO model) { _logger.LogInformation($"Start searching process with id = {model.ProcessId}"); var process = await _context.SelectionProcesses.FindAsync(model.ProcessId); if (process is null) { _logger.LogError($"Process with id = {model.ProcessId} not found"); throw new EntityNotFoundException("Process does not exist."); } _logger.LogInformation($"Check which status is going to be set."); if (model.NewStatus == "Zakazan" && model.SchedulerId is not null) { _logger.LogInformation($"Start searching user with id = {model.SchedulerId}"); var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString()); if (user is null) throw new EntityNotFoundException("Scheduler does not exist."); _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}"); process.SchedulerId = user.Id; } process.Status = model.NewStatus; process.Date = model.Appointment; process.Comment = model.Comment; _logger.LogInformation($"Processing changes."); await _context.SaveChangesAsync(); } public async Task InterviewerUpdate(InterviewerUpdateDTO model) { _logger.LogInformation($"Start searching process with id = {model.ProcessId}"); var process = await _context.SelectionProcesses.FindAsync(model.ProcessId); if (process is null) throw new EntityNotFoundException("Process does not exist."); _logger.LogInformation($"Start searching user with id = {model.SchedulerId}"); var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString()); if (user is null) throw new EntityNotFoundException("Scheduler does not exist."); _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}"); process.SchedulerId = user.Id; _logger.LogInformation("Processing changes..."); await _context.SaveChangesAsync(); } } }