84 lines
2.9 KiB
C#
84 lines
2.9 KiB
C#
using GradeBookApp.Dtos.User;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GradeBookApp.Services
|
|
{
|
|
public class UserApiService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public UserApiService(IHttpClientFactory factory)
|
|
{
|
|
_httpClient = factory.CreateClient("AuthorizedClient");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Первый шаг — вход по логину и паролю. Возвращает SessionId и тип кода (sms/email).
|
|
/// </summary>
|
|
public async Task<VerifyCodeDto?> LoginAsync(UserLoginDto loginDto)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync("api/users/login", loginDto);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<VerifyCodeDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Второй шаг — подтверждение кода. Возвращает JWT токен, если успешно.
|
|
/// </summary>
|
|
public async Task<TokenDto?> VerifyCodeAsync(VerifyCodeDto verifyDto)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync("api/Users/verify-code", verifyDto);
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<TokenDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Отмена сессии подтверждения.
|
|
/// </summary>
|
|
public async Task<bool> CancelSessionAsync(string sessionId)
|
|
{
|
|
var content = new StringContent($"\"{sessionId}\"", Encoding.UTF8, "application/json");
|
|
var response = await _httpClient.PostAsync("api/users/cancel-session", content);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить текущего пользователя по JWT-токену.
|
|
/// </summary>
|
|
public async Task<UserReadDto?> GetCurrentUserAsync()
|
|
{
|
|
var response = await _httpClient.GetAsync("api/users/me");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<UserReadDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить список преподавателей. Требуется роль "Employee".
|
|
/// </summary>
|
|
public async Task<List<UserReadDto>?> GetTeachersAsync()
|
|
{
|
|
var response = await _httpClient.GetAsync("api/users/professors");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<List<UserReadDto>>();
|
|
}
|
|
}
|
|
}
|