namespace Diligent.WebAPI.Business.Services { public class ApplicantService : IApplicantService { private readonly DatabaseContext _context; private readonly IMapper _mapper; private readonly ILogger _logger; public ApplicantService(DatabaseContext context, IMapper mapper, ILogger logger) { _context = context; _mapper = mapper; _logger = logger; } public async Task> 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 { Items = _mapper.Map>(filteredApplicants), Total = totalNumberOfItems }; } public async Task 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(applicant); _logger.LogInformation($"Applicant with id = {id} mapped successfully"); return result; } public async Task 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(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> 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>(adsApplicants); return result; } public async Task> GetOptions() { var res = await _context.Applicants.ToListAsync(); return _mapper.Map>(res); } public async Task> 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 { 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 { Data = true }; } //public async Task CreateApplicant(ApplicantCreateDto applicantCreateDto) //{ // var applicant = _mapper.Map(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(); //} } }