using Diligent.WebAPI.Contracts.DTOs.SelectionProcess; using static System.Net.Mime.MediaTypeNames; using System.Collections.Generic; namespace Diligent.WebAPI.Business.Services { public class SelectionProcessService : ISelectionProcessService { private readonly DatabaseContext _context; private readonly IMapper _mapper; public SelectionProcessService(DatabaseContext context, IMapper mapper) { _context = context; _mapper = mapper; } public async Task> GetAllAsync() => _mapper.Map>(await _context.SelectionProcesses.ToListAsync()); public async Task GetByIdAsync(int id) { var sp = await _context.SelectionProcesses.FindAsync(id); if (sp is null) throw new EntityNotFoundException("Selection process not found"); return _mapper.Map(sp); } public async Task CreateAsync(SelectionProcessCreateDto model) { await _context.SelectionProcesses.AddAsync(_mapper.Map(model)); await _context.SaveChangesAsync(); } public async Task UpdateAsync(int id, SelectionProcessCreateDto model) { var sp = await _context.SelectionProcesses.FindAsync(id); if (sp is null) throw new EntityNotFoundException("Selection process not found"); _mapper.Map(model, sp); _context.Entry(sp).State = EntityState.Modified; await _context.SaveChangesAsync(); } public async Task DeleteAsync(int id) { var sp = await _context.SelectionProcesses.FindAsync(id); if (sp is null) throw new EntityNotFoundException("Selection process not found"); _context.SelectionProcesses.Remove(sp); await _context.SaveChangesAsync(); } public async Task FinishSelectionProcess(int id) { var sp = await _context.SelectionProcesses.FindAsync(id); if (sp is null) throw new EntityNotFoundException("Selection process not found"); sp.Status = "Odrađen"; var nextLevel = _context.SelectionLevels.AsEnumerable().SkipWhile(obj => obj.Id != sp.SelectionLevelId).Skip(1).First(); if (nextLevel is null) throw new EntityNotFoundException("Candidate came to last selection level"); SelectionProcess newProcess = new SelectionProcess { Name = sp.Name, SelectionLevelId = nextLevel.Id, Status = "Čeka na zakazivanje", ApplicantId = sp.ApplicantId }; _context.SelectionProcesses.Add(newProcess); return await _context.SaveChangesAsync() > 0; } } }