feat: implemented available resumes on Back-end
This commit is contained in:
parent
7c22b4b9a7
commit
171757705a
@ -1,6 +1,6 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace JOBot.Backend.DTOs.HeadHunterHook;
|
namespace JOBot.Backend.DTOs.HeadHunterApi;
|
||||||
|
|
||||||
public record HeadHunterTokenResponseDto(
|
public record HeadHunterTokenResponseDto(
|
||||||
[property: JsonPropertyName("access_token")]
|
[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>
|
<ItemGroup>
|
||||||
<Protobuf Include="..\Proto\*" GrpcServices="Server"></Protobuf>
|
<Protobuf Include="..\Proto\*" GrpcServices="Server"></Protobuf>
|
||||||
|
<Protobuf Update="..\Proto\resume.proto">
|
||||||
|
<Link>Proto\resume.proto</Link>
|
||||||
|
</Protobuf>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using JOBot.Backend.DAL.Context;
|
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.Backend.Infrastructure.Config;
|
||||||
using JOBot.Infrastructure.Config;
|
using JOBot.Infrastructure.Config;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@ -38,6 +39,13 @@ public class HeadHunterService(
|
|||||||
return string.Format(_config.Links.AuthLink, [_config.ClientId, GetRedirectUrl(userId)]);
|
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: Разбить этот метод
|
public async Task<Status> AuthUser(int userId, string authorizationCode, string? error) //TODO: Разбить этот метод
|
||||||
{
|
{
|
||||||
logger.LogInformation($"Authorization for user {userId} in process...");
|
logger.LogInformation($"Authorization for user {userId} in process...");
|
||||||
@ -123,6 +131,49 @@ public class HeadHunterService(
|
|||||||
return Status.Success;
|
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)
|
private string GetRedirectUrl(long userId)
|
||||||
{
|
{
|
||||||
return new UriBuilder(_config.Links.HookDomain)
|
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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,6 +32,9 @@
|
|||||||
<None Update="appsettings.Example.json">
|
<None Update="appsettings.Example.json">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -58,12 +61,6 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Content Include="appsettings.json">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</Content>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\JOBot.Infrastructure\JOBot.Infrastructure.csproj" />
|
<ProjectReference Include="..\JOBot.Infrastructure\JOBot.Infrastructure.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
30
Proto/resume.proto
Normal file
30
Proto/resume.proto
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
option csharp_namespace = "JOBot.Proto";
|
||||||
|
|
||||||
|
import "google/protobuf/wrappers.proto";
|
||||||
|
|
||||||
|
service Resume{
|
||||||
|
rpc GetResume(GetResumeRequest) returns (GetResumeResponse);
|
||||||
|
rpc GetAvailable(GetAvailableRequest) returns (GetAvailableResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetResumeRequest{
|
||||||
|
int64 user_id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetResumeResponse{
|
||||||
|
google.protobuf.StringValue resumeUrl = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetAvailableRequest{
|
||||||
|
int64 user_id = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetAvailableResponse{
|
||||||
|
repeated ResumeDesc resumes = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ResumeDesc{
|
||||||
|
string resumeUrl = 1;
|
||||||
|
string name = 2;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user