using System.Net.Http.Headers; 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, ILogger logger, IOptions config, HeadHunterService headHunterService) { public async Task<(Status Status, List? Resume)> GetAvailableResumes(long userId) { var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserId == userId); if (user == null) { return new(Status.UserNotFound, null); } if (!user.ExpiresIn.HasValue || new DateTime().AddSeconds(user.ExpiresIn.Value) > DateTime.Now) { var status = await headHunterService.UpdateUserAccessToken(user); if (status != HeadHunterService.Status.Success) { logger.LogError($"User {userId} has expired access and update of it was unsuccessful: {status}"); return (Status.TokenExpired, 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"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", user.AccessToken); using var res = await client.GetAsync("/resumes/mine"); if (!res.IsSuccessStatusCode) { logger.LogWarning( $"User {user.Username ?? user.UserId.ToString()} resume list is unavailable by unsuccessful status code: {res.StatusCode}"); return new(Status.RequestError, null); } var responseDto = JsonSerializer.Deserialize(await res.Content.ReadAsStringAsync()); if (responseDto == null) return new(Status.Error, null); return new(Status.Success, responseDto.Items); } public enum Status { Success, UserNotFound, Error, RequestError, TokenExpired } }