* Changed views to have them in the same layout
* Added Authorization
This commit is contained in:
@@ -5,7 +5,7 @@ namespace OrdersManagement.Services;
|
||||
|
||||
public class CustomerOrderService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
AuthenticationStateProvider authenticationStateProvider)
|
||||
CustomAuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<CustomerOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<CustomerOrderDto>?> GetCustomerOrdersAsync()
|
||||
@@ -25,7 +25,7 @@ public class CustomerOrderService(
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetByIdAsync($"api/CustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
return await GetEntityAsync($"api/CustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ using SytelineSaAppEfDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services
|
||||
{
|
||||
public class EdiCustomerOrderService(IHttpClientFactory httpClientFactory, AuthenticationStateProvider authenticationStateProvider, ErrorLogService errorLogService) : ServiceBase<EdiCustomerOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
public class EdiCustomerOrderService(IHttpClientFactory httpClientFactory, CustomAuthenticationStateProvider authenticationStateProvider, ErrorLogService errorLogService) : ServiceBase<EdiCustomerOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<EdiCustomerOrderDto>?> GetEdiCustomerOrdersAsync()
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace OrdersManagement.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetByIdAsync($"api/EdiCustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
return await GetEntityAsync($"api/EdiCustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace OrdersManagement.Services;
|
||||
|
||||
public class ErrorLogService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
AuthenticationStateProvider authenticationStateProvider)
|
||||
CustomAuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<ErrorLogDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<ErrorLogDto>?> GetErrorLogsAsync(Guid customerOrderNumber)
|
||||
|
||||
@@ -2,27 +2,30 @@ using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class HangfireService(HttpClient httpClient)
|
||||
public class HangfireService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
CustomAuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<TaskSchedulerDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<TaskSchedulerDto>?> GetTaskSchedulersAsync()
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<TaskSchedulerDto>>("api/HangfireJobs/GetTasks");
|
||||
return await GetListAsync("api/HangfireJobs/");
|
||||
}
|
||||
|
||||
public async Task<TaskSchedulerDto?> GetTaskSchedulerAsync(Guid id)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<TaskSchedulerDto>($"api/HangfireJobs/{id}");
|
||||
return await GetEntityAsync($"api/HangfireJobs/{id}");
|
||||
}
|
||||
|
||||
public async Task<int> AddTaskSchedulerAsync(TaskSchedulerDto taskSchedulerDto)
|
||||
{
|
||||
HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync("api/HangfireJobs/AddTask", taskSchedulerDto);
|
||||
HttpResponseMessage responseMessage = await PostAsJsonAsync("api/HangfireJobs/add", taskSchedulerDto);
|
||||
return responseMessage.IsSuccessStatusCode ? 1 : 0;
|
||||
}
|
||||
|
||||
public async Task<int> DeleteTaskSchedulerAsync(TaskSchedulerDto taskSchedulerDto)
|
||||
{
|
||||
HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync("api/HangfireJobs/DeleteTask", taskSchedulerDto);
|
||||
HttpResponseMessage responseMessage = await PostAsJsonAsync("api/HangfireJobs/delete", taskSchedulerDto);
|
||||
return responseMessage.IsSuccessStatusCode ? 1 : 0;
|
||||
}
|
||||
}
|
||||
@@ -2,40 +2,38 @@ using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class RoleService(HttpClient httpClient)
|
||||
public class RoleService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
CustomAuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<RoleDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<RoleDto>?> GetRolesAsync()
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<RoleDto>>("api/Roles");
|
||||
return await GetListAsync("api/Roles");
|
||||
}
|
||||
|
||||
public async Task<RoleDto?> GetRoleAsync(Guid roleId)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<RoleDto>($"api/Roles/by-id/?id={roleId}");
|
||||
return await GetEntityAsync($"api/Roles/by-id/?id={roleId}");
|
||||
}
|
||||
|
||||
public async Task<RoleDto?> GetRoleByNameAsync(string roleName)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<RoleDto>($"api/Roles/by-name/?name={roleName}");
|
||||
return await GetEntityAsync($"api/Roles/by-name/?name={roleName}");
|
||||
}
|
||||
|
||||
public async Task AddRoleAsync(RoleDto role)
|
||||
public async Task<HttpResponseMessage> AddRoleAsync(RoleDto role)
|
||||
{
|
||||
await httpClient.PostAsJsonAsync("api/Roles", role);
|
||||
return await PostAsJsonAsync("api/Roles", role);
|
||||
}
|
||||
|
||||
public async Task UpdateRoleAsync(RoleDto role)
|
||||
public async Task<HttpResponseMessage> UpdateRoleAsync(RoleDto role)
|
||||
{
|
||||
await httpClient.PutAsJsonAsync("api/Roles", role);
|
||||
return await PutAsJsonAsync("api/Roles", role);
|
||||
}
|
||||
|
||||
public async Task DeleteRoleAsync(Guid roleId)
|
||||
public async Task<HttpResponseMessage> DeleteRoleAsync(Guid roleId)
|
||||
{
|
||||
await httpClient.DeleteAsync($"api/Roles/?id={roleId}");
|
||||
return await DeleteAsync($"api/Roles/?id={roleId}");
|
||||
}
|
||||
|
||||
// public async Task<IEnumerable<UserDto>?> GetUsersInRoleAsync(Guid roleId)
|
||||
// {
|
||||
// return await httpClient.GetFromJsonAsync<IEnumerable<UserDto>>($"api/Roles/{roleId}/Users");
|
||||
// }
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace OrdersManagement.Services;
|
||||
|
||||
public class ScheduleOrderService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
AuthenticationStateProvider authenticationStateProvider)
|
||||
CustomAuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<ScheduleOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<ScheduleOrderDto>?> GetScheduleOrdersAsync()
|
||||
@@ -27,7 +27,7 @@ public class ScheduleOrderService(
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetByIdAsync($"api/ScheduleOrders/{scheduleOrderId}");
|
||||
return await GetEntityAsync($"api/ScheduleOrders/{scheduleOrderId}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,31 @@
|
||||
using Blazored.LocalStorage;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using OrdersManagement.Models;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class UserService(IHttpClientFactory clientFactory, AuthenticationStateProvider authStateProvider)
|
||||
public class UserService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
CustomAuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<UserDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
private readonly HttpClient _httpClient = clientFactory.CreateClient("FaKrosnoApi");
|
||||
private readonly HttpClient _httpClient = httpClientFactory.CreateClient("FaKrosnoApi");
|
||||
|
||||
public async Task<IEnumerable<UserDto>?> GetUsersAsync()
|
||||
{
|
||||
return await _httpClient.GetFromJsonAsync<IEnumerable<UserDto>>("api/Users");
|
||||
return await GetListAsync("api/Users");
|
||||
}
|
||||
|
||||
public async Task<UserDto?> AuthenticateUserAsync(string login, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await _httpClient.PostAsJsonAsync("api/Users/login", new { Login = login, Password = password });
|
||||
var response = await PostAsJsonAsync("api/Users/login", new { Login = login, Password = password });
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result = await response.Content.ReadFromJsonAsync<LoginResponseDto>();
|
||||
|
||||
if (result?.Token == null) return null;
|
||||
|
||||
await ((CustomAuthenticationStateProvider)authStateProvider).MarkUserAsAuthenticated(result.Token);
|
||||
await authenticationStateProvider.MarkUserAsAuthenticated(result.Token);
|
||||
return await GetUserByUsernameAsync(login);
|
||||
|
||||
}
|
||||
@@ -37,26 +38,26 @@ public class UserService(IHttpClientFactory clientFactory, AuthenticationStatePr
|
||||
|
||||
public async Task<UserDto?> GetUserAsync(Guid userId)
|
||||
{
|
||||
return await _httpClient.GetFromJsonAsync<UserDto>($"api/Users/by-id/?id={userId}");
|
||||
return await GetEntityAsync($"api/Users/by-id/?id={userId}");
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserByUsernameAsync(string username)
|
||||
{
|
||||
return await _httpClient.GetFromJsonAsync<UserDto>($"api/Users/by-username/?username={username}");
|
||||
return await GetEntityAsync($"api/Users/by-username/?username={username}");
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> AddUserAsync(UserDto user)
|
||||
{
|
||||
return await _httpClient.PostAsJsonAsync("api/Users", user);
|
||||
return await PostAsJsonAsync("api/Users", user);
|
||||
}
|
||||
|
||||
public async Task UpdateUserAsync(UserDto user)
|
||||
public async Task<HttpResponseMessage> UpdateUserAsync(UserDto user)
|
||||
{
|
||||
await _httpClient.PutAsJsonAsync("api/Users", user);
|
||||
return await PutAsJsonAsync("api/Users", user);
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(Guid userId)
|
||||
public async Task<HttpResponseMessage> DeleteUserAsync(Guid userId)
|
||||
{
|
||||
await _httpClient.DeleteAsync($"api/Users/?id={userId}");
|
||||
return await DeleteAsync($"api/Users/?id={userId}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user