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

ApplicantService.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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
  21. .Include(x => x.TechnologyApplicants)
  22. .Include(x => x.Comments)
  23. .ThenInclude(t => t.User)
  24. .FirstOrDefaultAsync(a => a.ApplicantId == id);
  25. if (applicant is null)
  26. throw new EntityNotFoundException("Applicant not found");
  27. return _mapper.Map<ApplicantViewDto>(applicant);
  28. }
  29. public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
  30. {
  31. var applicant = _mapper.Map<Applicant>(applicantCreateDto);
  32. await _context.Applicants.AddAsync(applicant);
  33. await _context.SaveChangesAsync();
  34. }
  35. public async Task DeleteApplicant(int id)
  36. {
  37. var applicant = await _context.Applicants.FindAsync(id);
  38. if(applicant is null)
  39. throw new EntityNotFoundException("Applicant not found");
  40. _context.Applicants.Remove(applicant);
  41. await _context.SaveChangesAsync();
  42. }
  43. public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
  44. {
  45. var applicant = await _context.Applicants.FindAsync(id);
  46. if (applicant is null)
  47. throw new EntityNotFoundException("Applicant not found");
  48. _mapper.Map(applicantUpdateDto, applicant);
  49. _context.Entry(applicant).State = EntityState.Modified;
  50. await _context.SaveChangesAsync();
  51. }
  52. }
  53. }