| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using Diligent.WebAPI.Contracts.DTOs.Applicant;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class ApplicantService : IApplicantService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
-
- public ApplicantService(DatabaseContext context,IMapper mapper)
- {
- _context = context;
- _mapper = mapper;
- }
-
- public async Task<List<ApplicantViewDto>> GetAll()
- {
- var applicants = await _context.Applicants.Include(c => c.Ads).ToListAsync();
- return _mapper.Map<List<ApplicantViewDto>>(applicants);
- }
-
- public async Task<ApplicantViewDto> GetById(int id)
- {
- var applicant = await _context.Applicants
- .Include(x => x.Ads)
- .ThenInclude(x => x.Technologies)
- .Include(x => x.TechnologyApplicants)
- .ThenInclude(x => x.Technology)
- .Include(x => x.Comments)
- .ThenInclude(t => t.User)
- .FirstOrDefaultAsync(x => x.ApplicantId == id);
-
- if (applicant is null)
- throw new EntityNotFoundException("Applicant not found");
-
- return _mapper.Map<ApplicantViewDto>(applicant);
- }
- public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
- {
- var applicant = await _context.Applicants
- .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
- .FirstOrDefaultAsync(a => a.ApplicantId == id);
-
- if (applicant is null)
- throw new EntityNotFoundException("Applicant not found");
-
- return _mapper.Map<ApplicantViewDto>(applicant);
- }
- public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
- {
- var applicant = _mapper.Map<Applicant>(applicantCreateDto);
- await _context.Applicants.AddAsync(applicant);
-
- await _context.SaveChangesAsync();
- }
-
- public async Task DeleteApplicant(int id)
- {
- var applicant = await _context.Applicants.FindAsync(id);
-
- if(applicant is null)
- throw new EntityNotFoundException("Applicant not found");
-
- _context.Applicants.Remove(applicant);
- await _context.SaveChangesAsync();
- }
-
- public async Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto)
- {
- var applicant = await _context.Applicants.FindAsync(id);
- if (applicant is null)
- throw new EntityNotFoundException("Applicant not found");
-
- _mapper.Map(applicantUpdateDto, applicant);
-
- _context.Entry(applicant).State = EntityState.Modified;
- await _context.SaveChangesAsync();
- }
- }
- }
|