You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.2 KiB
64 lines
2.2 KiB
using System.Net.Http.Json;
|
|
using AdminPanel.Models;
|
|
|
|
namespace AdminPanel.Services;
|
|
|
|
public class HttpUsersService : IUsersService
|
|
{
|
|
private HttpClient Client { get; }
|
|
|
|
public HttpUsersService(HttpClient client)
|
|
{
|
|
this.Client = client;
|
|
}
|
|
|
|
private record ListUsersResponse(List<User> Users, uint TotalCount);
|
|
|
|
public async Task<(uint, List<User>)> ListUsers(uint from, uint len)
|
|
{
|
|
var httpResponse = await Client.GetAsync($"/api/admin/list-users?start={from}&n={len}");
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
var response = await httpResponse.Content.ReadFromJsonAsync<ListUsersResponse>()
|
|
?? throw new Exception("Received non-parseable json from server");
|
|
|
|
return (response.TotalCount, response.Users);
|
|
}
|
|
|
|
private record AddUserRequest(string Username, string Email, string Password, bool IsAdmin);
|
|
|
|
private record AddUserResponse(uint Id);
|
|
|
|
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));
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
|
|
var response = await httpResponse.Content.ReadFromJsonAsync<AddUserResponse>()
|
|
?? throw new Exception("Received non-parseable json from server");
|
|
|
|
return new User
|
|
{
|
|
Name = username,
|
|
Email = email,
|
|
IsAdmin = isAdmin,
|
|
Id = response.Id
|
|
};
|
|
}
|
|
|
|
private record RemoveUsersRequest(uint[] Identifiers);
|
|
|
|
public async Task RemoveUsers(IEnumerable<uint> userIds)
|
|
{
|
|
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/remove-all", new RemoveUsersRequest(userIds.ToArray()));
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
}
|
|
|
|
private record UpdateUserRequest(string Email, string Username, bool IsAdmin);
|
|
|
|
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));
|
|
httpResponse.EnsureSuccessStatusCode();
|
|
}
|
|
} |