| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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<List<SelectionProcessResposneDto>> GetAllAsync() =>
- _mapper.Map<List<SelectionProcessResposneDto>>(await _context.SelectionProcesses.ToListAsync());
-
- public async Task<SelectionProcessResposneDto> GetByIdAsync(int id)
- {
- var sp = await _context.SelectionProcesses.FindAsync(id);
-
- if (sp is null)
- throw new EntityNotFoundException("Selection process not found");
-
- return _mapper.Map<SelectionProcessResposneDto>(sp);
-
- }
-
- public async Task CreateAsync(SelectionProcessCreateDto model)
- {
- await _context.SelectionProcesses.AddAsync(_mapper.Map<SelectionProcess>(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<bool> 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;
- }
- }
- }
|