|
|
|
|
|
|
|
|
using Azure.Storage.Blobs; |
|
|
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
|
|
using Microsoft.WindowsAzure.Storage; |
|
|
|
|
|
using Microsoft.WindowsAzure.Storage.Blob; |
|
|
|
|
|
using static Diligent.WebAPI.Data.Entities.Applicant; |
|
|
|
|
|
|
|
|
using static Diligent.WebAPI.Data.Entities.Applicant; |
|
|
|
|
|
|
|
|
namespace Diligent.WebAPI.Business.Services |
|
|
namespace Diligent.WebAPI.Business.Services |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
private readonly IMapper _mapper; |
|
|
private readonly IMapper _mapper; |
|
|
private readonly ILogger<ApplicantService> _logger; |
|
|
private readonly ILogger<ApplicantService> _logger; |
|
|
private readonly IUserService _userService; |
|
|
private readonly IUserService _userService; |
|
|
private readonly IConfiguration _configuration; |
|
|
|
|
|
|
|
|
private readonly IFileService _fileService; |
|
|
|
|
|
|
|
|
public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger, |
|
|
public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger, |
|
|
IUserService userService,IConfiguration configuration) |
|
|
|
|
|
|
|
|
IUserService userService,IFileService fileService) |
|
|
{ |
|
|
{ |
|
|
_context = context; |
|
|
_context = context; |
|
|
_mapper = mapper; |
|
|
_mapper = mapper; |
|
|
_logger = logger; |
|
|
_logger = logger; |
|
|
_userService = userService; |
|
|
_userService = userService; |
|
|
_configuration = configuration; |
|
|
|
|
|
|
|
|
_fileService = fileService; |
|
|
} |
|
|
} |
|
|
public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger) |
|
|
public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
_logger.LogInformation($"Mapping Applicant with id = {id}"); |
|
|
_logger.LogInformation($"Mapping Applicant with id = {id}"); |
|
|
var result = _mapper.Map<ApplicantViewDto>(applicant); |
|
|
var result = _mapper.Map<ApplicantViewDto>(applicant); |
|
|
result.CV = await GetCV("638077305621281656.pdf"); |
|
|
|
|
|
|
|
|
result.CV = await _fileService.GetCV("638077305621281656.pdf"); |
|
|
_logger.LogInformation($"Applicant with id = {id} mapped successfully"); |
|
|
_logger.LogInformation($"Applicant with id = {id} mapped successfully"); |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
{ |
|
|
{ |
|
|
string fileName = string.Format(@"{0}.pdf", DateTime.Now.Ticks); |
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
await _fileService.UploadCV(fileName, request.PdfFile); |
|
|
|
|
|
|
|
|
_logger.LogInformation("Start applying for ad"); |
|
|
_logger.LogInformation("Start applying for ad"); |
|
|
_logger.LogInformation("Find ad by id"); |
|
|
_logger.LogInformation("Find ad by id"); |
|
|
|
|
|
|
|
|
Data = true |
|
|
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(); |
|
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |