* Added Authorization which is not working
This commit is contained in:
@@ -1,17 +1,36 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using SytelineSaAppEfDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class CustomerOrderService(HttpClient httpClient)
|
||||
public class CustomerOrderService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
AuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<CustomerOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<CustomerOrderDto>?> GetCustomerOrdersAsync()
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<CustomerOrderDto>>("api/CustomerOrders");
|
||||
try
|
||||
{
|
||||
return await GetListAsync("api/CustomerOrders");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd HTTP w GetCustomerOrdersAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CustomerOrderDto?> GetCustomerOrderAsync(Guid customerOrderNumber)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<CustomerOrderDto>(
|
||||
$"api/CustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
try
|
||||
{
|
||||
return await GetByIdAsync($"api/CustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd HTTP w GetScheduleOrderAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,63 @@
|
||||
using OrdersManagement.Models;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using OrdersManagement.Models;
|
||||
using SytelineSaAppEfDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services
|
||||
{
|
||||
public class EdiCustomerOrderService(HttpClient httpClient)
|
||||
public class EdiCustomerOrderService(IHttpClientFactory httpClientFactory, AuthenticationStateProvider authenticationStateProvider, ErrorLogService errorLogService) : ServiceBase<EdiCustomerOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<EdiCustomerOrderDto>?> GetEdiCustomerOrdersAsync()
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<EdiCustomerOrderDto>>("api/EdiCustomerOrders");
|
||||
try
|
||||
{
|
||||
return await GetListAsync("api/EdiCustomerOrders");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd HTTP w GetEdiCustomerOrdersAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<EdiCustomerOrderDto?> GetEdiCustomerOrderAsync(Guid customerOrderNumber)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<EdiCustomerOrderDto>(
|
||||
$"api/EdiCustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
try
|
||||
{
|
||||
return await GetByIdAsync($"api/EdiCustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd HTTP w GetEdiCustomerOrdersAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ResponseModel> SendOrderToSyteline(Guid customerOrderNumber, string orderNumber)
|
||||
public async Task<ResponseModel?> SendOrderToSyteline(Guid customerOrderNumber, string orderNumber)
|
||||
{
|
||||
HttpResponseMessage responseMessage = await httpClient.PostAsync(
|
||||
$"api/EdiCustomerOrders/send-to-syteline?customerOrderNumber={customerOrderNumber}", null);
|
||||
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return new ResponseModel(1, orderNumber, null, null);
|
||||
HttpResponseMessage responseMessage = await PostAsync($"api/EdiCustomerOrders/send-to-syteline?customerOrderNumber={customerOrderNumber}");
|
||||
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
return new ResponseModel(1, orderNumber, null, null);
|
||||
}
|
||||
|
||||
string? errorMessage = null;
|
||||
IEnumerable<ErrorLogDto>? logs = await errorLogService.GetErrorLogsAsync(customerOrderNumber);
|
||||
|
||||
if (logs != null)
|
||||
{
|
||||
errorMessage = string.Join("\r\n", logs.Select(x => x.ErrMsg));
|
||||
}
|
||||
|
||||
return new ResponseModel(0, orderNumber, errorMessage, null);
|
||||
}
|
||||
|
||||
string? errorMessage = null;
|
||||
IEnumerable<ErrorLogDto>? logs = await httpClient.GetFromJsonAsync<IEnumerable<ErrorLogDto>>(
|
||||
$"api/ErrorLog/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
|
||||
if (logs != null)
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
errorMessage = string.Join("\r\n", logs.Select(x => x.ErrMsg));
|
||||
Console.WriteLine($"Błąd HTTP w GetEdiCustomerOrdersAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ResponseModel(0, orderNumber, errorMessage, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
OrdersManagement/Services/ErrorLogService.cs
Normal file
23
OrdersManagement/Services/ErrorLogService.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using SytelineSaAppEfDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class ErrorLogService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
AuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<ErrorLogDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public async Task<IEnumerable<ErrorLogDto>?> GetErrorLogsAsync(Guid customerOrderNumber)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetListAsync($"api/CustomerOrders/by-order-number/?customerOrderNumber={customerOrderNumber}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd HTTP w GetCustomerOrdersAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using FaKrosnoEfDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services
|
||||
{
|
||||
public class ScheduleOrderDetailsService(HttpClient httpClient)
|
||||
{
|
||||
public async Task<IEnumerable<ScheduleOrderDetailDto>?> GetScheduleOrderDetails(int scheduleOrderId)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<ScheduleOrderDetailDto>>(
|
||||
$"api/scheduleOrderDetails/order/{scheduleOrderId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,38 @@
|
||||
using FaKrosnoEfDataModel.Dtos;
|
||||
using System.Net.Http.Headers;
|
||||
using Blazored.LocalStorage;
|
||||
using FaKrosnoEfDataModel.Dtos;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace OrdersManagement.Services
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class ScheduleOrderService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
AuthenticationStateProvider authenticationStateProvider)
|
||||
: ServiceBase<ScheduleOrderDto>(httpClientFactory, authenticationStateProvider)
|
||||
{
|
||||
public class ScheduleOrderService(HttpClient httpClient)
|
||||
public async Task<IEnumerable<ScheduleOrderDto>?> GetScheduleOrdersAsync()
|
||||
{
|
||||
public async Task<IEnumerable<ScheduleOrderDto>?> GetScheduleOrdersAsync()
|
||||
try
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<ScheduleOrderDto>>("api/ScheduleOrders");
|
||||
return await GetListAsync("api/ScheduleOrders");
|
||||
}
|
||||
|
||||
public async Task<ScheduleOrderDto?> GetScheduleOrderAsync(int scheduleOrderId)
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<ScheduleOrderDto>(
|
||||
$"api/ScheduleOrders/{scheduleOrderId}");
|
||||
Console.WriteLine($"Błąd HTTP w GetScheduleOrdersAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ScheduleOrderDto?> GetScheduleOrderAsync(int scheduleOrderId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetByIdAsync($"api/ScheduleOrders/{scheduleOrderId}");
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd HTTP w GetScheduleOrderAsync: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
OrdersManagement/Services/ServiceBase.cs
Normal file
52
OrdersManagement/Services/ServiceBase.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class ServiceBase<T> where T : class
|
||||
{
|
||||
private readonly AuthenticationStateProvider _authenticationStateProvider;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
protected ServiceBase(IHttpClientFactory httpClientFactory, AuthenticationStateProvider authenticationStateProvider)
|
||||
{
|
||||
_authenticationStateProvider = authenticationStateProvider;
|
||||
_httpClient = httpClientFactory.CreateClient("FaKrosnoApi");
|
||||
|
||||
_ = Configure();
|
||||
}
|
||||
|
||||
protected async Task<IEnumerable<T>?> GetListAsync(string request)
|
||||
{
|
||||
var response = await _httpClient.GetAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<IEnumerable<T>>();
|
||||
}
|
||||
|
||||
protected async Task<T?> GetByIdAsync(string request)
|
||||
{
|
||||
var response = await _httpClient.GetAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadFromJsonAsync<T>();
|
||||
}
|
||||
|
||||
protected async Task<HttpResponseMessage> PostAsync(string request)
|
||||
{
|
||||
var response = await _httpClient.PostAsync(request, null);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return response;
|
||||
}
|
||||
|
||||
private async Task Configure()
|
||||
{
|
||||
var token = await 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,42 +1,62 @@
|
||||
using Blazored.LocalStorage;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using OrdersManagement.Models;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement.Services;
|
||||
|
||||
public class UserService(HttpClient httpClient)
|
||||
public class UserService(IHttpClientFactory clientFactory, AuthenticationStateProvider authStateProvider)
|
||||
{
|
||||
private readonly HttpClient _httpClient = clientFactory.CreateClient("FaKrosnoApi");
|
||||
|
||||
public async Task<IEnumerable<UserDto>?> GetUsersAsync()
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<IEnumerable<UserDto>>("api/Users");
|
||||
}
|
||||
|
||||
public async Task<UserDto?> AuthenticateUserAsync(string login, string password)
|
||||
{
|
||||
var response = await httpClient.PostAsJsonAsync("api/users/authenticate", new { Login = login, Password = password });
|
||||
return response.IsSuccessStatusCode ? await response.Content.ReadFromJsonAsync<UserDto>() : null;
|
||||
return await _httpClient.GetFromJsonAsync<IEnumerable<UserDto>>("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 });
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result = await response.Content.ReadFromJsonAsync<LoginResponseDto>();
|
||||
|
||||
if (result?.Token == null) return null;
|
||||
|
||||
await ((CustomAuthenticationStateProvider)authStateProvider).MarkUserAsAuthenticated(result.Token);
|
||||
return await GetUserByUsernameAsync(login);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Błąd logowania: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserAsync(Guid userId)
|
||||
{
|
||||
return await httpClient.GetFromJsonAsync<UserDto>($"api/Users/by-id/?id={userId}");
|
||||
return await _httpClient.GetFromJsonAsync<UserDto>($"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 _httpClient.GetFromJsonAsync<UserDto>($"api/Users/by-username/?username={username}");
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> AddUserAsync(UserDto user)
|
||||
{
|
||||
return await httpClient.PostAsJsonAsync("api/Users", user);
|
||||
return await _httpClient.PostAsJsonAsync("api/Users", user);
|
||||
}
|
||||
|
||||
public async Task UpdateUserAsync(UserDto user)
|
||||
{
|
||||
await httpClient.PutAsJsonAsync("api/Users", user);
|
||||
await _httpClient.PutAsJsonAsync("api/Users", user);
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(Guid userId)
|
||||
{
|
||||
await httpClient.DeleteAsync($"api/Users/?id={userId}");
|
||||
await _httpClient.DeleteAsync($"api/Users/?id={userId}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user