|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
using AdminPanel.Models;
|
|
|
|
using AdminPanel.Models;
|
|
|
|
|
|
|
|
|
|
|
@ -12,30 +14,54 @@ public class HttpUsersService : IUsersService
|
|
|
|
this.Client = client;
|
|
|
|
this.Client = client;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private record ListUsersResponse(List<User> Users, uint TotalCount);
|
|
|
|
private record ServerErrorMessageDto(string? Field, string? Error, string Message);
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<(uint, List<User>)> ListUsers(uint from, uint len, string? searchString = null)
|
|
|
|
private async Task EnsureResponseIsOk(HttpResponseMessage response)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var httpResponse = await Client.GetAsync($"/api/admin/list-users?start={from}&n={len}&search={searchString ?? ""}");
|
|
|
|
|
|
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response = await httpResponse.Content.ReadFromJsonAsync<ListUsersResponse>()
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<ServerErrorMessageDto[]>();
|
|
|
|
?? throw new Exception("Received non-parseable json from server");
|
|
|
|
var messages = content!
|
|
|
|
|
|
|
|
.GroupBy(e => e.Field ?? e.Error!)
|
|
|
|
|
|
|
|
.ToDictionary(
|
|
|
|
|
|
|
|
g => g.Key,
|
|
|
|
|
|
|
|
g => g.ToList().ConvertAll(e => e.Message)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new ServiceException("Server refused request", messages);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private record ListUsersResponseDto(List<User> Users, uint TotalCount);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<(uint, List<User>)> ListUsers(uint from, uint len, string? searchString = null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var httpResponse =
|
|
|
|
|
|
|
|
await Client.GetAsync($"/api/admin/list-users?start={from}&n={len}&search={searchString ?? ""}");
|
|
|
|
|
|
|
|
await EnsureResponseIsOk(httpResponse);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = await httpResponse.Content.ReadFromJsonAsync<ListUsersResponseDto>()
|
|
|
|
|
|
|
|
?? throw new Exception("Received non-parseable json from server");
|
|
|
|
|
|
|
|
|
|
|
|
return (response.TotalCount, response.Users);
|
|
|
|
return (response.TotalCount, response.Users);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private record AddUserRequest(string Username, string Email, string Password, bool IsAdmin);
|
|
|
|
private record AddUserRequestDto(string Username, string Email, string Password, bool IsAdmin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private record AddUserResponseDto(uint Id);
|
|
|
|
|
|
|
|
|
|
|
|
private record AddUserResponse(uint Id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<User> AddUser(string username, string email, string password, bool isAdmin)
|
|
|
|
public async Task<User> AddUser(string username, string email, string password, bool isAdmin)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/add", new AddUserRequest(username, email, password, isAdmin));
|
|
|
|
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/add",
|
|
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
new AddUserRequestDto(username, email, password, isAdmin));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await EnsureResponseIsOk(httpResponse);
|
|
|
|
|
|
|
|
|
|
|
|
var response = await httpResponse.Content.ReadFromJsonAsync<AddUserResponse>()
|
|
|
|
var response = await httpResponse.Content.ReadFromJsonAsync<AddUserResponseDto>()
|
|
|
|
?? throw new Exception("Received non-parseable json from server");
|
|
|
|
?? throw new Exception("Received non-parseable json from server");
|
|
|
|
|
|
|
|
|
|
|
|
return new User
|
|
|
|
return new User
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -46,19 +72,22 @@ public class HttpUsersService : IUsersService
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private record RemoveUsersRequest(uint[] Identifiers);
|
|
|
|
private record RemoveUsersRequestDto(uint[] Identifiers);
|
|
|
|
|
|
|
|
|
|
|
|
public async Task RemoveUsers(IEnumerable<uint> userIds)
|
|
|
|
public async Task RemoveUsers(IEnumerable<uint> userIds)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/remove-all", new RemoveUsersRequest(userIds.ToArray()));
|
|
|
|
var httpResponse =
|
|
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
await Client.PostAsJsonAsync("/api/admin/user/remove-all", new RemoveUsersRequestDto(userIds.ToArray()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await EnsureResponseIsOk(httpResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private record UpdateUserRequest(string Email, string Username, bool IsAdmin);
|
|
|
|
private record UpdateUserRequestDto(string Email, string Username, bool IsAdmin);
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateUser(User user)
|
|
|
|
public async Task UpdateUser(User user)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var httpResponse = await Client.PostAsJsonAsync($"/api/admin/user/{user.Id}/update", new UpdateUserRequest(user.Email, user.Name, user.IsAdmin));
|
|
|
|
var httpResponse = await Client.PostAsJsonAsync($"/api/admin/user/{user.Id}/update",
|
|
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
new UpdateUserRequestDto(user.Email, user.Name, user.IsAdmin));
|
|
|
|
|
|
|
|
await EnsureResponseIsOk(httpResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|