65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
using Entities.Contracts;
|
|
using Entities.DTO;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http.Json;
|
|
using TechHelper.Client.HttpRepository;
|
|
using TechHelper.Services;
|
|
|
|
namespace TechHelper.Client.Services
|
|
{
|
|
public class ClasssServices : IClassServices
|
|
{
|
|
private readonly HttpClient _client;
|
|
private readonly IAuthenticationClientService _authenticationClientService;
|
|
|
|
public ClasssServices(HttpClient client, IAuthenticationClientService authenticationClientService)
|
|
{
|
|
_client = client;
|
|
_authenticationClientService = authenticationClientService;
|
|
}
|
|
|
|
public Task<ResponseDto> CreateClass(UserRegistrationToClassDto userClass)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<ApiResponse> GetClassStudents()
|
|
{
|
|
try
|
|
{
|
|
var result = await _client.PostAsJsonAsync("class/getClassStudents","");
|
|
var content = await result.Content.ReadAsStringAsync();
|
|
var users = JsonConvert.DeserializeObject<List<StudentDto>>(content);
|
|
return ApiResponse.Success(result: users);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
return ApiResponse.Error($"获取失败,{ex.Message}, InnerException: {ex.InnerException}");
|
|
}
|
|
}
|
|
|
|
public async Task<ResponseDto> UserRegister(UserRegistrationToClassDto userRegistrationToClassDto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _client.PostAsJsonAsync("class/userRegiste",
|
|
userRegistrationToClassDto);
|
|
var data = await result.Content.ReadAsStringAsync();
|
|
|
|
await _authenticationClientService.RefreshTokenAsync();
|
|
|
|
return new ResponseDto
|
|
{
|
|
IsSuccessfulRegistration = result.IsSuccessStatusCode,
|
|
Errors = result.IsSuccessStatusCode ? null : new string[] { data }
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 实际应用中,这里应该加入日志记录
|
|
Console.WriteLine($"Error in UserRegister: {ex.Message}");
|
|
return new ResponseDto { IsSuccessfulRegistration = false, Errors = new string[] { ex.Message } };
|
|
}
|
|
}
|
|
}
|
|
} |