Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SelectionProcessService.cs 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Diligent.WebAPI.Contracts.DTOs.SelectionProcess;
  2. using static System.Net.Mime.MediaTypeNames;
  3. using System.Collections.Generic;
  4. namespace Diligent.WebAPI.Business.Services
  5. {
  6. public class SelectionProcessService : ISelectionProcessService
  7. {
  8. private readonly DatabaseContext _context;
  9. private readonly IMapper _mapper;
  10. public SelectionProcessService(DatabaseContext context, IMapper mapper)
  11. {
  12. _context = context;
  13. _mapper = mapper;
  14. }
  15. public async Task<List<SelectionProcessResposneDto>> GetAllAsync() =>
  16. _mapper.Map<List<SelectionProcessResposneDto>>(await _context.SelectionProcesses.ToListAsync());
  17. public async Task<SelectionProcessResposneDto> GetByIdAsync(int id)
  18. {
  19. var sp = await _context.SelectionProcesses.FindAsync(id);
  20. if (sp is null)
  21. throw new EntityNotFoundException("Selection process not found");
  22. return _mapper.Map<SelectionProcessResposneDto>(sp);
  23. }
  24. public async Task CreateAsync(SelectionProcessCreateDto model)
  25. {
  26. await _context.SelectionProcesses.AddAsync(_mapper.Map<SelectionProcess>(model));
  27. await _context.SaveChangesAsync();
  28. }
  29. public async Task UpdateAsync(int id, SelectionProcessCreateDto model)
  30. {
  31. var sp = await _context.SelectionProcesses.FindAsync(id);
  32. if (sp is null)
  33. throw new EntityNotFoundException("Selection process not found");
  34. _mapper.Map(model, sp);
  35. _context.Entry(sp).State = EntityState.Modified;
  36. await _context.SaveChangesAsync();
  37. }
  38. public async Task DeleteAsync(int id)
  39. {
  40. var sp = await _context.SelectionProcesses.FindAsync(id);
  41. if (sp is null)
  42. throw new EntityNotFoundException("Selection process not found");
  43. _context.SelectionProcesses.Remove(sp);
  44. await _context.SaveChangesAsync();
  45. }
  46. public async Task<bool> FinishSelectionProcess(int id)
  47. {
  48. var sp = await _context.SelectionProcesses.FindAsync(id);
  49. if (sp is null)
  50. throw new EntityNotFoundException("Selection process not found");
  51. sp.Status = "Odrađen";
  52. var nextLevel = _context.SelectionLevels.AsEnumerable().SkipWhile(obj => obj.Id != sp.SelectionLevelId).Skip(1).First();
  53. if (nextLevel is null)
  54. throw new EntityNotFoundException("Candidate came to last selection level");
  55. SelectionProcess newProcess = new SelectionProcess
  56. {
  57. Name = sp.Name,
  58. SelectionLevelId = nextLevel.Id,
  59. Status = "Čeka na zakazivanje",
  60. ApplicantId = sp.ApplicantId
  61. };
  62. _context.SelectionProcesses.Add(newProcess);
  63. return await _context.SaveChangesAsync() > 0;
  64. }
  65. }
  66. }