| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- using Azure.Storage.Blobs;
- using Microsoft.Extensions.Configuration;
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Blob;
- using static Diligent.WebAPI.Data.Entities.Applicant;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class ApplicantService : IApplicantService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly ILogger<ApplicantService> _logger;
- private readonly IUserService _userService;
- private readonly IConfiguration _configuration;
-
- public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger,
- IUserService userService,IConfiguration configuration)
- {
- _context = context;
- _mapper = mapper;
- _logger = logger;
- _userService = userService;
- _configuration = configuration;
- }
- 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 ApplyForAd(ApplyForAdRequestDto request)
- {
- string fileName = string.Format(@"{0}.pdf", DateTime.Now.Ticks);
-
- string blobstorageconnection = _configuration.GetValue<string>("BlobConnectionString");
- CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
- CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = blobClient.GetContainerReference(
- _configuration.GetValue<string>("BlobContainerName"));
- CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
-
- await using (var data = request.PdfFile.OpenReadStream())
- {
- await blockBlob.UploadFromStreamAsync(data);
- }
-
- _logger.LogInformation("Start applying for ad");
- _logger.LogInformation("Find ad by id");
- var ad = await _context.Ads.Where(x => x.Id == request.AdId).FirstOrDefaultAsync();
-
- if (ad == null)
- {
- _logger.LogError($"Ad with {request.AdId} not found");
- throw new EntityNotFoundException("Ad not found in database");
- }
-
- _logger.LogInformation($"Find sent technologies from FE in database");
- var technologies = await _context.Technologies.Where(x => request.TechnologiesIds.Contains(x.TechnologyId)).ToListAsync();
-
- _logger.LogInformation($"Create applicant instance with sent data");
- Applicant applicant = new()
- {
- FirstName = request.FirstName,
- LastName = request.LastName,
- Position = ad.Title,
- DateOfApplication = DateTime.UtcNow,
- CV = fileName,
- Email = request.Email,
- PhoneNumber = request.PhoneNumber,
- GithubLink = request.GithubLink,
- LinkedlnLink = request.LinkedinLink,
- BitBucketLink = request.BitBucketLink,
- Experience = request.Experience,
- //TypeOfEmployment = (EmploymentTypes)Enum.Parse(typeof(EmploymentTypes), ad.EmploymentType, true),
- TypeOfEmployment = ad.EmploymentType == EmploymentTypes.Intership ? TypesOfEmployment.Intership : TypesOfEmployment.Posao,
- Comments = new(),
- Ads = new List<Ad> { ad },
- SelectionProcesses = new(),
- TechnologyApplicants = new(),
- ApplicationChannel = "Putem sajta"
- };
-
- _logger.LogInformation($"Saving applicant in database");
- await _context.Applicants.AddAsync(applicant);
-
- var res = await _context.SaveChangesAsync();
- _logger.LogInformation($"Applicant saved in database");
-
-
- _logger.LogInformation($"Saving TechnologyApplicants in database");
- for (int i = 0; i < technologies.Count; i++)
- {
- await _context.ApplicantTechnologies.AddAsync(new TechnologyApplicant { Applicant = applicant, ApplicantId = applicant.ApplicantId, Technology = technologies[i], TechnologyId = technologies[i].TechnologyId });
- }
-
- await _context.SaveChangesAsync();
- _logger.LogInformation($"TechnologyApplicants saved in database");
- }
-
- public async Task ImportApplicant(List<ApplicantImportDto> requests)
- {
-
- _logger.LogInformation($"Create applicant instance with sent data");
- var res = new List<Applicant>();
- var user = await _userService.GetFirst();
- foreach (var request in requests) {
- Applicant applicant = new Applicant
- {
- FirstName = request.FirstName ?? "",
- LastName = request.LastName ?? "",
- CV = request.CV ?? "",
- Email = request.Email ?? "",
- PhoneNumber = request.PhoneNumber ?? "",
- GithubLink = request.GithubLink ?? "",
- LinkedlnLink = request.LinkedlnLink ?? "",
- BitBucketLink = request.BitBucketLink ?? "",
- Position = "",
- DateOfApplication = request.DateOfApplication,
- TypeOfEmployment = request.TypeOfEmployment == "Praksa" ? TypesOfEmployment.Intership : TypesOfEmployment.Posao,
- Experience = request.Experience,
- ApplicationChannel = request.ApplicationChannel ?? "Putem sajta",
- // MORA DA SE UVEDE KO JE DAO KOMENTAR DA LI DA STAVIMO DA JE DANIJELA SVIMA STAVILA KOMENTARE ILI ??
- Comments = new List<Comment>
- {
- new Comment
- {
- User = user,
- Content = request.Comment
- }
- },
- Ads = new List<Ad> { request.Ad },
- SelectionProcesses = new(),
- TechnologyApplicants = new()
- };
- res.Add(applicant);
- }
-
- await _context.AddRangeAsync(res);
- await _context.SaveChangesAsync();
- _logger.LogInformation($"Saving applicant in database");
- _logger.LogInformation($"Applicant saved in database");
-
- }
- 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<string> GetCV(string fileName)
- {
- CloudBlockBlob blockBlob;
- await using (MemoryStream memoryStream = new())
- {
- string blobstorageconnection = _configuration.GetValue<string>("BlobConnectionString");
- CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
- CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
- CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(_configuration.GetValue<string>("BlobContainerName"));
- blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
- await blockBlob.DownloadToStreamAsync(memoryStream);
- Stream blobStream = blockBlob.OpenReadAsync().Result;
- return ConvertToBase64(blobStream);
- }
- }
-
- private static string ConvertToBase64(Stream stream)
- {
- byte[] bytes;
- using (var memoryStream = new MemoryStream())
- {
- stream.CopyTo(memoryStream);
- bytes = memoryStream.ToArray();
- }
-
- string base64 = Convert.ToBase64String(bytes);
- return base64;
- }
-
- //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();
- //}
-
- }
- }
|