| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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(SelectionProcessCreateDto model)
- {
- var sp = await _context.SelectionProcesses.FindAsync(model.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 = model.Name,
- SelectionLevelId = nextLevel.Id,
- Status = "Čeka na zakazivanje",
- ApplicantId = sp.ApplicantId,
- SchedulerId = model.SchedulerId
- };
- _context.SelectionProcesses.Add(newProcess);
-
- return await _context.SaveChangesAsync() > 0;
- }
- }
- }
|