Přidání nového projektu - pokus o explorer
This commit is contained in:
1 parent
b0b0b3ee34
commit
d06a9f332b
48 files changed
+1429
No files matched your search
@@ -0,0 +1,8 @@
|
||||
namespace winui_learning_2.Services;
|
||||
|
||||
public static class ApiConstants
|
||||
{
|
||||
public const string BaseUrl = "https://api.nodplay.cz";
|
||||
public const string LoginPath = "/api/users/authenticate";
|
||||
public const string AdminDashboardPath = "/api/admin/dashboard";
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace winui_learning_2.Services;
|
||||
|
||||
public static class AuthSession
|
||||
{
|
||||
public static string? Token { get; private set; }
|
||||
public static string? Username { get; private set; }
|
||||
|
||||
public static bool IsAuthenticated => !string.IsNullOrEmpty(Token);
|
||||
|
||||
public static void SignIn(string username, string? token)
|
||||
{
|
||||
Username = username;
|
||||
Token = token;
|
||||
}
|
||||
|
||||
public static void SignOut()
|
||||
{
|
||||
Username = null;
|
||||
Token = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Threading.Tasks;
|
||||
using winui_learning_2.Models;
|
||||
|
||||
namespace winui_learning_2.Services;
|
||||
|
||||
public class NodplayAdminService
|
||||
{
|
||||
private readonly NodplayApiClient _apiClient = new();
|
||||
|
||||
public Task<DashBoardViewModel?> GetDashboardAsync(int page = 1) =>
|
||||
_apiClient.GetAsync<DashBoardViewModel>($"{ApiConstants.AdminDashboardPath}?page={page}");
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace winui_learning_2.Services;
|
||||
|
||||
public class NodplayApiClient
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
private readonly HttpClient _httpClient = new()
|
||||
{
|
||||
BaseAddress = new Uri(ApiConstants.BaseUrl)
|
||||
};
|
||||
|
||||
public async Task<T?> GetAsync<T>(string path)
|
||||
{
|
||||
using var request = CreateRequest(HttpMethod.Get, path);
|
||||
using var response = await _httpClient.SendAsync(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
throw new NodplayApiException((int)response.StatusCode, body);
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
return JsonSerializer.Deserialize<T>(json, JsonOptions);
|
||||
}
|
||||
|
||||
private static HttpRequestMessage CreateRequest(HttpMethod method, string path)
|
||||
{
|
||||
var request = new HttpRequestMessage(method, path);
|
||||
|
||||
if (!string.IsNullOrEmpty(AuthSession.Token))
|
||||
{
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthSession.Token);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
public class NodplayApiException : Exception
|
||||
{
|
||||
public int StatusCode { get; }
|
||||
|
||||
public NodplayApiException(int statusCode, string? body)
|
||||
: base(string.IsNullOrWhiteSpace(body)
|
||||
? $"API request failed ({statusCode})."
|
||||
: body)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using winui_learning_2.Models;
|
||||
|
||||
namespace winui_learning_2.Services;
|
||||
|
||||
public class NodplayAuthService
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
private readonly HttpClient _httpClient = new()
|
||||
{
|
||||
BaseAddress = new Uri(ApiConstants.BaseUrl)
|
||||
};
|
||||
|
||||
public async Task<LoginResult> LoginAsync(string username, string password)
|
||||
{
|
||||
var request = new UserLoginDto
|
||||
{
|
||||
Username = username,
|
||||
Password = password
|
||||
};
|
||||
|
||||
using var response = await _httpClient.PostAsJsonAsync(ApiConstants.LoginPath, request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(body))
|
||||
{
|
||||
return LoginResult.Success(username, token: null);
|
||||
}
|
||||
|
||||
var loginResponse = JsonSerializer.Deserialize<LoginResponse>(body, JsonOptions);
|
||||
return LoginResult.Success(
|
||||
loginResponse?.Username ?? username,
|
||||
loginResponse?.GetToken());
|
||||
}
|
||||
|
||||
var errorBody = await response.Content.ReadAsStringAsync();
|
||||
var message = string.IsNullOrWhiteSpace(errorBody)
|
||||
? $"Login failed ({(int)response.StatusCode})."
|
||||
: errorBody;
|
||||
|
||||
return LoginResult.Failure(message);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct LoginResult(bool IsSuccess, string? Username, string? Token, string? ErrorMessage)
|
||||
{
|
||||
public static LoginResult Success(string username, string? token) =>
|
||||
new(true, username, token, null);
|
||||
|
||||
public static LoginResult Failure(string message) =>
|
||||
new(false, null, null, message);
|
||||
}
|
||||
Reference in new issue
Block a user