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.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.ToListAsync();
  16. return _mapper.Map<List<ApplicantViewDto>>(applicants);
  17. }
  18. public async Task<ApplicantViewDto> GetById(int id)
  19. {
  20. var applicant = await _context.Applicants.FindAsync(id);
  21. if(applicant is null)
  22. throw new EntityNotFoundException("Applicant not found");
  23. return _mapper.Map<ApplicantViewDto>(applicant);
  24. }
  25. public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  26. {
  27. var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  28. await _context.Applicants.AddAsync(applicant);
  29. await _context.SaveChangesAsync();
  30. }
  31. public async Task DeleteApplicant(int id)
  32. {
  33. var applicant = await _context.Applicants.FindAsync(id);
  34. if(applicant is null)
  35. throw new EntityNotFoundException("Applicant not found");
  36. _context.Applicants.Remove(applicant);
  37. await _context.SaveChangesAsync();
  38. }
  39. public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  40. {
  41. var applicant = await _context.Applicants.FindAsync(id);
  42. if (applicant is null)
  43. throw new EntityNotFoundException("Applicant not found");
  44. _mapper.Map(applicantUpdateDto, applicant);
  45. _context.Entry(applicant).State = EntityState.Modified;
  46. await _context.SaveChangesAsync();
  47. }
  48. }
  49. }