ExamEdit&Check
This commit is contained in:
@@ -1,19 +1,15 @@
|
||||
using Entities.DTO;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace TechHelper.Client.Services
|
||||
{
|
||||
public class ClasssServices : IClassServices
|
||||
{
|
||||
private HttpClient _client;
|
||||
private IHttpClientFactory _httpClientFactory;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public ClasssServices(HttpClient client, IHttpClientFactory httpClientFactory)
|
||||
public ClasssServices(HttpClient client)
|
||||
{
|
||||
_client = client;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public Task<ResponseDto> CreateClass(UserRegistrationToClassDto userClass)
|
||||
@@ -23,23 +19,24 @@ namespace TechHelper.Client.Services
|
||||
|
||||
public async Task<ResponseDto> UserRegister(UserRegistrationToClassDto userRegistrationToClassDto)
|
||||
{
|
||||
using (_client = _httpClientFactory.CreateClient("Default"))
|
||||
try
|
||||
{
|
||||
var result = await _client.PostAsJsonAsync("class/userRegiste",
|
||||
userRegistrationToClassDto);
|
||||
var data = await result.Content.ReadAsStringAsync();
|
||||
|
||||
try
|
||||
return new ResponseDto
|
||||
{
|
||||
|
||||
var result = await _client.PostAsJsonAsync("class/userRegiste",
|
||||
userRegistrationToClassDto);
|
||||
var data = await result.Content.ReadAsStringAsync();
|
||||
return new ResponseDto { IsSuccessfulRegistration = result.IsSuccessStatusCode, Errors = new string[] { data } };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ResponseDto { IsSuccessfulRegistration = false, Errors = new string[] { ex.Message } };
|
||||
}
|
||||
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 } };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user