Mentés + JwtMiddleware-ből a dolgok berakva

This commit is contained in:
2025-03-14 10:25:08 +01:00
parent ca789cfbc3
commit 9d0a577c1f
9 changed files with 54 additions and 117 deletions
+12 -2
View File
@@ -1,23 +1,33 @@
using System.Net.Http.Headers;
namespace WorkFlowCheck.Web.Services
{
public class BaseService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public readonly HttpClient _httpClient;
public readonly IConfiguration _configuration;
public BaseService(HttpClient httpClient, IConfiguration configuration)
public BaseService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
{
_httpClient = httpClient;
_configuration = configuration;
_httpContextAccessor = httpContextAccessor;
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
_httpClient.BaseAddress = new Uri(apiBaseUrl);
var apiKey = configuration["ApiKey"];
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
var jwtToken = _httpContextAccessor.HttpContext?.Request.Cookies["AuthToken"];
if (!string.IsNullOrEmpty(jwtToken))
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
}
}
}
}
@@ -1,4 +1,5 @@
using Serilog;
using Newtonsoft.Json;
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;
@@ -6,7 +7,7 @@ namespace WorkFlowCheck.Web.Services
{
public class BaseStockService : BaseService, IBaseStockService
{
public BaseStockService(HttpClient httpClient, IConfiguration configuration) : base(httpClient, configuration)
public BaseStockService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
{
}
@@ -55,9 +56,33 @@ namespace WorkFlowCheck.Web.Services
return retVal;
}
public async Task<CheckPointDTO> UpdateCheckPoint(CheckPointDTO checkPointDto)
public async Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint(CheckPointDTO checkPointDTO)
{
return null;
try
{
string endpoint = $"{_httpClient.BaseAddress}api/sync/updatecheckpoint";
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkPointDTO))
{
httpResponseMessage.EnsureSuccessStatusCode();
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
var response = JsonConvert.DeserializeObject<ApiResponseDTO<CheckPointDTO>>(jsonString);
return response ?? new ApiResponseDTO<CheckPointDTO>
{
IsSuccess = false,
};
}
}
catch (Exception ex)
{
// Hiba visszaadása
return new ApiResponseDTO<CheckPointDTO>
{
IsSuccess = false
};
}
}
}
@@ -6,6 +6,6 @@ namespace WorkFlowCheck.Web.Services.Interfaces
{
Task<List<CheckPointDTO>> GetAllCheckPoints();
Task<CheckPointDTO> GetCheckPoint(int id);
Task<CheckPointDTO> UpdateCheckPoint(CheckPointDTO checkPoint);
Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint(CheckPointDTO checkPoint);
}
}
@@ -7,7 +7,7 @@ namespace WorkFlowCheck.Web.Services
{
public class UserService : BaseService, IUserService
{
public UserService(HttpClient httpClient, IConfiguration configuration) : base(httpClient, configuration)
public UserService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
{
}
public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)