Просмотр исходного кода

Merge branch 'feature/refactoring_code' of Neca/HRCenter into BE_dev

pull/147/head
safet.purkovic 3 лет назад
Родитель
Сommit
b3d111e579

+ 6
- 71
Diligent.WebAPI.Business/Services/ApplicantService.cs Просмотреть файл

@@ -1,8 +1,4 @@
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
{
@@ -12,16 +8,16 @@ namespace Diligent.WebAPI.Business.Services
private readonly IMapper _mapper;
private readonly ILogger<ApplicantService> _logger;
private readonly IUserService _userService;
private readonly IConfiguration _configuration;
private readonly IFileService _fileService;

public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger,
IUserService userService,IConfiguration configuration)
IUserService userService,IFileService fileService)
{
_context = context;
_mapper = mapper;
_logger = logger;
_userService = userService;
_configuration = configuration;
_fileService = fileService;
}
public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger)
{
@@ -76,7 +72,7 @@ namespace Diligent.WebAPI.Business.Services
}
_logger.LogInformation($"Mapping Applicant with id = {id}");
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");
return result;
}
@@ -133,17 +129,7 @@ namespace Diligent.WebAPI.Business.Services
{
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("Find ad by id");
@@ -275,56 +261,5 @@ namespace Diligent.WebAPI.Business.Services
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();
//}

}
}

+ 53
- 0
Diligent.WebAPI.Business/Services/FileService.cs Просмотреть файл

@@ -0,0 +1,53 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace Diligent.WebAPI.Business.Services
{
public class FileService : IFileService
{
private readonly IConfiguration _configuration;
public FileService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<string> GetCV(string fileName)
{
await using MemoryStream memoryStream = new();
var cloudBlockBlob = GetCloudBlockBlob(fileName);
await cloudBlockBlob.DownloadToStreamAsync(memoryStream);
Stream blobStream = cloudBlockBlob.OpenReadAsync().Result;
return ConvertToBase64(blobStream);
}
public async Task UploadCV(string fileName,IFormFile file)
{
var cloudBlockBlob = GetCloudBlockBlob(fileName);
await using var data = file.OpenReadStream();
await cloudBlockBlob.UploadFromStreamAsync(data);
}

private CloudBlockBlob GetCloudBlockBlob(string fileName)
{
string blobstorageconnection = _configuration.GetValue<string>("BlobConnectionString");
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(
_configuration.GetValue<string>("BlobContainerName"));
return container.GetBlockBlobReference(fileName);
}
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;
}
}
}

+ 0
- 3
Diligent.WebAPI.Business/Services/Interfaces/IApplicantService.cs Просмотреть файл

@@ -14,8 +14,5 @@ namespace Diligent.WebAPI.Business.Services.Interfaces
Task<List<ApplicantOptionsDTO>> GetOptions();
Task<ServiceResponseDTO<object>> InitializeProcess(ApplicantProcessRequestDTO model);
Task ImportApplicant(List<ApplicantImportDto> request);
Task<string> GetCV(string fileName);
//Task CreateApplicant(ApplicantCreateDto applicantCreateDto);
//Task UpdateApplicant(int id, ApplicantUpdateDto applicantUpdateDto);
}
}

+ 10
- 0
Diligent.WebAPI.Business/Services/Interfaces/IFileService.cs Просмотреть файл

@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Http;

namespace Diligent.WebAPI.Business.Services.Interfaces
{
public interface IFileService
{
Task<string> GetCV(string fileName);
Task UploadCV(string fileName,IFormFile file);
}
}

+ 5
- 3
Diligent.WebAPI.Host/Controllers/V1/ApplicantsController.cs Просмотреть файл

@@ -8,10 +8,12 @@ namespace Diligent.WebAPI.Host.Controllers.V1
public class ApplicantsController : ControllerBase
{
private readonly IApplicantService _applicantService;
private readonly IFileService _fileService;

public ApplicantsController(IApplicantService applicantService)
public ApplicantsController(IApplicantService applicantService,IFileService fileService)
{
_applicantService = applicantService;
_fileService = fileService;
}

[Authorize]
@@ -70,9 +72,9 @@ namespace Diligent.WebAPI.Host.Controllers.V1
}

[HttpGet("get-CV")]
public async Task<IActionResult> GetCV(string fileName)
public async Task<IActionResult> GetCandidateCV(string fileName)
{
var res = await _applicantService.GetCV(fileName);
var res = await _fileService.GetCV(fileName);
return Ok(res);
}
}

+ 1
- 0
Diligent.WebAPI.Host/Extensions/BusinessConfigurationExtension.cs Просмотреть файл

@@ -37,6 +37,7 @@
services.AddScoped<IImportService, ImportService>();
services.AddScoped<ISaveImportedDataService, SaveImportedDataService>();
services.AddScoped<IScreeningTestService, ScreeningTestService>();
services.AddScoped<IFileService, FileService>();
}

/// <summary>

Загрузка…
Отмена
Сохранить