You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicantService.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Diligent.WebAPI.Contracts.DTOs.Applicant;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class ApplicantService : IApplicantService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. public ApplicantService(DatabaseContext context,IMapper mapper)
  9. {
  10. _context = context;
  11. _mapper = mapper;
  12. }
  13. public async Task<List<ApplicantViewDto>> GetAll()
  14. {
  15. var applicants = await _context.Applicants.Include(c => c.Ads).ToListAsync();
  16. return _mapper.Map<List<ApplicantViewDto>>(applicants);
  17. }
  18. public async Task<ApplicantViewDto> GetById(int id)
  19. {
  20. var applicant = await _context.Applicants
  21. .Include(x => x.Ads)
  22. .ThenInclude(x => x.Technologies)
  23. .Include(x => x.TechnologyApplicants)
  24. .ThenInclude(x => x.Technology)
  25. .Include(x => x.Comments)
  26. .ThenInclude(t => t.User)
  27. .FirstOrDefaultAsync(x => x.ApplicantId == id);
  28. if (applicant is null)
  29. throw new EntityNotFoundException("Applicant not found");
  30. return _mapper.Map<ApplicantViewDto>(applicant);
  31. }
  32. public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
  33. {
  34. var applicant = await _context.Applicants
  35. .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
  36. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  37. if (applicant is null)
  38. throw new EntityNotFoundException("Applicant not found");
  39. return _mapper.Map<ApplicantViewDto>(applicant);
  40. }
  41. public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  42. {
  43. var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  44. await _context.Applicants.AddAsync(applicant);
  45. await _context.SaveChangesAsync();
  46. }
  47. public async Task DeleteApplicant(int id)
  48. {
  49. var applicant = await _context.Applicants.FindAsync(id);
  50. if(applicant is null)
  51. throw new EntityNotFoundException("Applicant not found");
  52. _context.Applicants.Remove(applicant);
  53. await _context.SaveChangesAsync();
  54. }
  55. public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  56. {
  57. var applicant = await _context.Applicants.FindAsync(id);
  58. if (applicant is null)
  59. throw new EntityNotFoundException("Applicant not found");
  60. _mapper.Map(applicantUpdateDto, applicant);
  61. _context.Entry(applicant).State = EntityState.Modified;
  62. await _context.SaveChangesAsync();
  63. }
  64. }
  65. }