feat: implemented available resumes on Back-end
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JOBot.Backend.DTOs.HeadHunterHook;
|
||||
namespace JOBot.Backend.DTOs.HeadHunterApi;
|
||||
|
||||
public record HeadHunterTokenResponseDto(
|
||||
[property: JsonPropertyName("access_token")]
|
||||
21
JOBot.Backend/DTOs/HeadHunterApi/ListOfResumeResponseDto.cs
Normal file
21
JOBot.Backend/DTOs/HeadHunterApi/ListOfResumeResponseDto.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JOBot.Backend.DTOs.HeadHunterApi;
|
||||
|
||||
public class ListOfResumeResponseDto
|
||||
{
|
||||
[JsonPropertyName("found")]
|
||||
public bool Found { get; set; }
|
||||
[JsonPropertyName("items")]
|
||||
public List<Resume> Items { get; set; } = [];
|
||||
}
|
||||
|
||||
public class Resume
|
||||
{
|
||||
[JsonPropertyName("can_publish_or_update")]
|
||||
public bool CanPublishOrUpdate { get; set; }
|
||||
[JsonPropertyName("title")]
|
||||
public string Title { get; set; } = null!;
|
||||
[JsonPropertyName("url")]
|
||||
public string Url { get; set; } = null!;
|
||||
}
|
||||
@@ -25,6 +25,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="..\Proto\*" GrpcServices="Server"></Protobuf>
|
||||
<Protobuf Update="..\Proto\resume.proto">
|
||||
<Link>Proto\resume.proto</Link>
|
||||
</Protobuf>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Backend.DTOs.HeadHunterHook;
|
||||
using JOBot.Backend.DAL.Models;
|
||||
using JOBot.Backend.DTOs.HeadHunterApi;
|
||||
using JOBot.Backend.Infrastructure.Config;
|
||||
using JOBot.Infrastructure.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -38,6 +39,13 @@ public class HeadHunterService(
|
||||
return string.Format(_config.Links.AuthLink, [_config.ClientId, GetRedirectUrl(userId)]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Auth user on HeadHunter API
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="authorizationCode"></param>
|
||||
/// <param name="error"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Status> AuthUser(int userId, string authorizationCode, string? error) //TODO: Разбить этот метод
|
||||
{
|
||||
logger.LogInformation($"Authorization for user {userId} in process...");
|
||||
@@ -123,6 +131,49 @@ public class HeadHunterService(
|
||||
return Status.Success;
|
||||
}
|
||||
|
||||
public async Task<Status> UpdateUserAccessToken(User user)
|
||||
{
|
||||
if(user.RefreshToken == null)
|
||||
return Status.UserAuthRejectedError;
|
||||
|
||||
using var client = new HttpClient(); //TODO: Написать wrapper для работы с HH API
|
||||
var form = new Dictionary<string, string>
|
||||
{
|
||||
{ "refresh_token", user.RefreshToken },
|
||||
{ "grant_type", "refresh_token" }
|
||||
};
|
||||
client.BaseAddress = new UriBuilder(_config.Links.HeadHunterApiDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https"
|
||||
}.Uri;
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jobot BackEnd Service");
|
||||
|
||||
using var res = await client.SendAsync(
|
||||
new HttpRequestMessage(
|
||||
HttpMethod.Post,
|
||||
_config.Links.HeadHunterTokenRoute)
|
||||
{
|
||||
Content = new FormUrlEncodedContent(form)
|
||||
});
|
||||
|
||||
var responseDto =
|
||||
JsonSerializer.Deserialize<HeadHunterTokenResponseDto>(await res.Content.ReadAsStringAsync());
|
||||
|
||||
if (responseDto == null)
|
||||
{
|
||||
logger.LogWarning($"User {user.UserId} access token accept completed with error " +
|
||||
$"{nameof(Status.HeadHunterResponseDeserializationFailedError)}");
|
||||
return Status.HeadHunterResponseDeserializationFailedError;
|
||||
}
|
||||
|
||||
user.RefreshToken = responseDto.RefreshToken;
|
||||
user.AccessToken = responseDto.AccessToken;
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
return Status.Success;
|
||||
}
|
||||
|
||||
private string GetRedirectUrl(long userId)
|
||||
{
|
||||
return new UriBuilder(_config.Links.HookDomain)
|
||||
|
||||
45
JOBot.Backend/Services/ResumeService.cs
Normal file
45
JOBot.Backend/Services/ResumeService.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Text.Json;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Backend.DTOs.HeadHunterApi;
|
||||
using JOBot.Backend.Infrastructure.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace JOBot.Backend.Services;
|
||||
|
||||
public class ResumeService(
|
||||
AppDbContext dbContext,
|
||||
IOptions<HeadHunterConfig> config)
|
||||
{
|
||||
public async Task<(Status Status, List<Resume>? Resume)> GetAvailableResumes(long userId)
|
||||
{
|
||||
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == userId);
|
||||
if (user == null)
|
||||
{
|
||||
return new(Status.UserNotFound, null);
|
||||
}
|
||||
|
||||
using var client = new HttpClient(); //TODO: Написать wrapper для работы с HH API
|
||||
client.BaseAddress = new UriBuilder(config.Value.Links.HeadHunterApiDomain)
|
||||
{
|
||||
Port = -1,
|
||||
Scheme = "https"
|
||||
}.Uri;
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jobot BackEnd Service");
|
||||
|
||||
using var res = await client.GetAsync("/resumes/mine");
|
||||
|
||||
var responseDto = JsonSerializer.Deserialize<ListOfResumeResponseDto>(await res.Content.ReadAsStringAsync());
|
||||
if (responseDto == null)
|
||||
return new(Status.Error, null);
|
||||
|
||||
return new(Status.Success, responseDto.Items);
|
||||
}
|
||||
|
||||
public enum Status
|
||||
{
|
||||
Success,
|
||||
UserNotFound,
|
||||
Error
|
||||
}
|
||||
}
|
||||
46
JOBot.Backend/Services/gRPC/ResumeService.cs
Normal file
46
JOBot.Backend/Services/gRPC/ResumeService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Grpc.Core;
|
||||
using JOBot.Backend.DAL.Context;
|
||||
using JOBot.Proto;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace JOBot.Backend.Services.gRPC;
|
||||
|
||||
public class ResumeService(AppDbContext dbContext, Backend.Services.ResumeService resumeService)
|
||||
: Proto.Resume.ResumeBase
|
||||
{
|
||||
public override async Task<GetResumeResponse> GetResume(GetResumeRequest request, ServerCallContext context)
|
||||
{
|
||||
var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == request.UserId);
|
||||
if (user == null)
|
||||
throw new RpcException(new Status(StatusCode.NotFound, "User not found"));
|
||||
|
||||
return new GetResumeResponse { ResumeUrl = user.CvUrl };
|
||||
}
|
||||
|
||||
public override async Task<GetAvailableResponse> GetAvailable(GetAvailableRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var response = await resumeService.GetAvailableResumes(request.UserId);
|
||||
|
||||
switch (response.Status)
|
||||
{
|
||||
case Services.ResumeService.Status.UserNotFound:
|
||||
throw new RpcException(new Status(StatusCode.NotFound, "User not found"));
|
||||
case Services.ResumeService.Status.Error:
|
||||
throw new RpcException(new Status(StatusCode.Internal, "Unknown error"));
|
||||
case Services.ResumeService.Status.Success:
|
||||
{
|
||||
var resp = new GetAvailableResponse();
|
||||
resp.Resumes.AddRange(response.Resume!.ConvertAll(x => new ResumeDesc
|
||||
{
|
||||
Name = x.Title,
|
||||
ResumeUrl = x.Url
|
||||
}));
|
||||
|
||||
return resp;
|
||||
}
|
||||
default:
|
||||
throw new RpcException(new Status(StatusCode.Unimplemented, "Unimplemented statement"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user