* Changed views to have them in the same layout

* Added Authorization
This commit is contained in:
2025-02-28 13:33:01 +01:00
parent aedb5810c2
commit b8fbb789ad
35 changed files with 1605 additions and 1306 deletions

View File

@@ -4,26 +4,28 @@ namespace OrdersManagement.Services;
public class ServiceBase<T> where T : class
{
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly CustomAuthenticationStateProvider _authenticationStateProvider;
private readonly HttpClient _httpClient;
protected ServiceBase(IHttpClientFactory httpClientFactory, AuthenticationStateProvider authenticationStateProvider)
protected ServiceBase(IHttpClientFactory httpClientFactory, CustomAuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
_httpClient = httpClientFactory.CreateClient("FaKrosnoApi");
_ = Configure();
}
protected async Task<IEnumerable<T>?> GetListAsync(string request)
{
Configure();
var response = await _httpClient.GetAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<IEnumerable<T>>();
}
protected async Task<T?> GetByIdAsync(string request)
protected async Task<T?> GetEntityAsync(string request)
{
Configure();
var response = await _httpClient.GetAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<T>();
@@ -31,22 +33,54 @@ public class ServiceBase<T> where T : class
protected async Task<HttpResponseMessage> PostAsync(string request)
{
Configure();
var response = await _httpClient.PostAsync(request, null);
response.EnsureSuccessStatusCode();
return response;
}
private async Task Configure()
protected async Task<HttpResponseMessage> PostAsJsonAsync(string request, T obj)
{
var token = await GetToken();
Configure();
var response = await _httpClient.PostAsJsonAsync(request, obj);
response.EnsureSuccessStatusCode();
return response;
}
protected async Task<HttpResponseMessage> PostAsJsonAsync(string request, object obj)
{
Configure();
var response = await _httpClient.PostAsJsonAsync(request, obj);
response.EnsureSuccessStatusCode();
return response;
}
protected async Task<HttpResponseMessage> PutAsJsonAsync(string request, T obj)
{
Configure();
var response = await _httpClient.PutAsJsonAsync(request, obj);
response.EnsureSuccessStatusCode();
return response;
}
protected async Task<HttpResponseMessage> DeleteAsync(string request)
{
Configure();
var response = await _httpClient.DeleteAsync(request);
response.EnsureSuccessStatusCode();
return response;
}
private void Configure()
{
var token = _authenticationStateProvider.GetToken();
_httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
}
private async Task<string?> GetToken()
{
await ((CustomAuthenticationStateProvider)_authenticationStateProvider).InitializeAsync();
return ((CustomAuthenticationStateProvider)_authenticationStateProvider).GetToken();
}
}