| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Diligent.WebAPI.Business.Extensions;
- using Microsoft.AspNetCore.Http;
-
- 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<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
- {
- var allApplicants = _context.Applicants
- .Include(c => c.Ads)
- .Include(x => x.TechnologyApplicants)
- .ThenInclude(x => x.Technology);
-
- var filteredApplicants = await allApplicants
- .FilterApplicants(applicantFilterDto);
-
- filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination
- {
- CurrentPage = applicantFilterDto.CurrentPage,
- PageSize = applicantFilterDto.PageSize
- });
-
- return new QueryResultDto<ApplicantViewDto>
- {
- Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants),
- Total = allApplicants.ToList().Count
- };
- }
-
- 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 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();
- }
-
- public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants()
- {
- var adsApplicants = await _context.Ads
- .Include(a => a.Applicants)
- .ThenInclude(a => a.TechnologyApplicants).ThenInclude(a => a.Technology).ToListAsync();
-
- return _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
- }
-
- public async Task<ApplicantViewDto> GetApplicantWithSelectionProcessesById(int id)
- {
- var applicant = await _context.Applicants
- .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.SelectionLevel)
- .Include(a => a.SelectionProcesses).ThenInclude(sp => sp.Scheduler)
- .FirstOrDefaultAsync(a => a.ApplicantId == id);
-
- if (applicant is null)
- throw new EntityNotFoundException("Applicant not found");
-
- return _mapper.Map<ApplicantViewDto>(applicant);
- }
- }
- }
|