Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SelectionProcessService.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(SelectionProcessCreateDto model)
  47. {
  48. var sp = await _context.SelectionProcesses.FindAsync(model.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 = model.Name,
  58. SelectionLevelId = nextLevel.Id,
  59. Status = "Čeka na zakazivanje",
  60. ApplicantId = sp.ApplicantId,
  61. SchedulerId = model.SchedulerId
  62. };
  63. _context.SelectionProcesses.Add(newProcess);
  64. return await _context.SaveChangesAsync() > 0;
  65. }
  66. }
  67. }