| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- namespace Diligent.WebAPI.Business.Services
- {
- public class ApplicantService : IApplicantService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly ILogger<ApplicantService> _logger;
-
- public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger)
- {
- _context = context;
- _mapper = mapper;
- _logger = logger;
- }
-
- public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
- {
- _logger.LogInformation("Start getting filtered applicants");
- _logger.LogInformation("Getting data from DB and filter");
- var filteredApplicants = (await _context.Applicants
- .Include(c => c.Ads)
- .Include(x => x.TechnologyApplicants)
- .ThenInclude(x => x.Technology).ToListAsync())
- .FilterApplicants(applicantFilterDto);
-
- int totalNumberOfItems = filteredApplicants.Count;
- _logger.LogInformation($"Got {totalNumberOfItems} applicants");
-
- filteredApplicants = PaginationExtension.ApplyPagging(filteredApplicants, new Pagination
- {
- CurrentPage = applicantFilterDto.CurrentPage,
- PageSize = applicantFilterDto.PageSize
- });
-
- _logger.LogInformation($"Return list of applicants");
- return new QueryResultDto<ApplicantViewDto>
- {
- Items = _mapper.Map<List<ApplicantViewDto>>(filteredApplicants),
- Total = totalNumberOfItems
- };
- }
-
- public async Task<ApplicantViewDto> GetById(int id)
- {
- _logger.LogInformation($"Start searching Applicant with id = {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)
- {
- _logger.LogError($"Applicant with id = {id} not found");
- throw new EntityNotFoundException("Applicant not found");
- }
- _logger.LogInformation($"Mapping Applicant with id = {id}");
- var result = _mapper.Map<ApplicantViewDto>(applicant);
- _logger.LogInformation($"Applicant with id = {id} mapped successfully");
- return result;
- }
-
- 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);
- }
-
- public async Task DeleteApplicant(int id)
- {
- _logger.LogInformation($"Start searching Applicant with id = {id}");
- var applicant = await _context.Applicants.FindAsync(id);
-
- if (applicant is null)
- {
- _logger.LogError($"Applicant with id = {id} not found");
- throw new EntityNotFoundException("Applicant not found");
- }
-
- _logger.LogInformation($"Removing Applicant with id = {id}");
- _context.Applicants.Remove(applicant);
- var result = _context.SaveChangesAsync();
- _logger.LogInformation($"Applicant with id = {id} is removed successfully");
- await result;
- }
-
- public async Task<List<AdApplicantsViewDto>> GetAllAdsApplicants(ApplicantFilterDto applicantFilterDto)
- {
- _logger.LogInformation("Start getting filtered applicants");
- _logger.LogInformation("Getting data from DB and filter");
- var adsApplicants = (await _context.Ads
- .Include(a => a.Applicants)
- .ThenInclude(a => a.TechnologyApplicants)
- .ThenInclude(a => a.Technology)
- .ToListAsync())
- .FilterAdApplicants(applicantFilterDto);
-
- _logger.LogInformation($"Got {adsApplicants.Count} ads");
-
- var result = _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
- return result;
- }
-
- public async Task<List<ApplicantOptionsDTO>> GetOptions()
- {
- var res = await _context.Applicants.ToListAsync();
- return _mapper.Map<List<ApplicantOptionsDTO>>(res);
- }
-
- public async Task<ServiceResponseDTO<object>> InitializeProcess(ApplicantProcessRequestDTO model)
- {
- var applicant = await _context.Applicants.Include(n => n.SelectionProcesses).Where(n=> n.ApplicantId ==model.ApplicantId).FirstOrDefaultAsync();
-
- if (applicant == null)
- return new ServiceResponseDTO<object>
- {
- IsError = true,
- ErrorMessage = "Applicant does not exist."
- };
-
- applicant.SelectionProcesses.Add(new SelectionProcess
- {
- Name = StringGenerator.GenerateRandomPassword(),
- SchedulerId = model.SchedulerId,
- SelectionLevelId = 1,
- Status = model.Appointment != null ? "Zakazan" : "Čeka na zakazivanje",
- Date = model.Appointment
- });
-
- await _context.SaveChangesAsync();
-
- return new ServiceResponseDTO<object>
- {
- Data = true
- };
- }
-
- //public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto)
- //{
- // var applicant = _mapper.Map<Applicant>(applicantCreateDto);
- // await _context.Applicants.AddAsync(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();
- //}
-
- }
- }
|