update routes

routes-update
Maxime BATISTA 9 months ago
parent 97ecfbd51d
commit 6c351dfd99

@ -14,7 +14,7 @@ builder.Services
var client = new HttpClient
{
BaseAddress = new Uri("http://localhost:8080")
BaseAddress = new Uri("http://localhost:5254")
};
builder.Services.AddScoped<IUsersService>(sp => new HttpUsersService(client));

@ -5,7 +5,6 @@ namespace AdminPanel.Services;
public class HttpTeamService : ITeamService
{
private readonly HttpClient _client;
public HttpTeamService(HttpClient client)
@ -13,38 +12,46 @@ public class HttpTeamService : ITeamService
this._client = client;
}
private record ListTeamResponse(uint TotalCount, List<Team> Teams);
private record CountTeamsResponse(uint Value);
public async Task<(uint, List<Team>)> ListTeam(uint from, uint count)
{
var httpResponse = await _client.GetAsync($"/api/admin/list-team?start={from}&n={count}");
var httpResponse = await _client.GetAsync($"/admin/teams?start={from}&n={count}");
httpResponse.EnsureSuccessStatusCode();
var response = await httpResponse.Content.ReadFromJsonAsync<ListTeamResponse>()!;
var teams = (await httpResponse.Content.ReadFromJsonAsync<List<Team>>())!;
httpResponse =
await _client.GetAsync($"/admin/teams/count");
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
return (response.TotalCount, response.Teams);
var countResponse = await httpResponse.Content.ReadFromJsonAsync<CountTeamsResponse>();
return (countResponse!.Value, teams);
}
private record AddTeamDTORequest(string Name, string Picture, string MainColor, string SecondaryColor);
private record AddTeamRequest(string Name, string Picture, string FirstColor, string SecondColor);
public async Task AddTeam(string name, string picture, string mainColor, string secondaryColor)
{
var httpResponse = await _client.PostAsJsonAsync($"/api/admin/add-team", new AddTeamDTORequest(name,picture,mainColor,secondaryColor));
var httpResponse = await _client.PostAsJsonAsync($"/admin/teams",
new AddTeamRequest(name, picture, mainColor, secondaryColor));
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
}
private record DeleteTeamsDTORequest(List<uint> Teams);
private record DeleteTeamsRequest(List<uint> Teams);
public async Task DeleteTeams(List<uint> teams)
{
var httpResponse = await _client.PostAsJsonAsync($"/api/admin/delete-teams", new DeleteTeamsDTORequest(teams));
var httpResponse = await _client.PostAsJsonAsync($"/admin/teams/remove-all", new DeleteTeamsRequest(teams));
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
}
private record UpdateTeamDTORequest(uint Id, string Name, string Picture, string MainColor, string SecondaryColor);
private record UpdateTeamRequest(uint Id, string Name, string Picture, string MainColor, string SecondaryColor);
public async Task UpdateTeam(Team team)
{
var httpResponse = await _client.PostAsJsonAsync($"/api/admin/team/{team.Id}/update",
new UpdateTeamDTORequest(team.Id, team.Name, team.Picture, team.MainColor, team.SecondColor) );
var httpResponse = await _client.PutAsJsonAsync($"/admin/teams/{team.Id}",
new UpdateTeamRequest(team.Id, team.Name, team.Picture, team.MainColor, team.SecondColor));
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
}
}

@ -13,19 +13,28 @@ public class HttpUsersService : IUsersService
{
this.Client = client;
}
private record ListUsersResponseDto(List<User> Users, uint TotalCount);
private record CountRequestResult(uint Value);
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 Client.GetAsync($"/admin/users?start={from}&n={len}&search={searchString ?? ""}");
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
var response = await httpResponse.Content.ReadFromJsonAsync<ListUsersResponseDto>()
?? throw new Exception("Received non-parseable json from server");
var users = await httpResponse.Content.ReadFromJsonAsync<List<User>>()
?? throw new Exception("Received non-parseable json from server");
return (response.TotalCount, response.Users);
var search = searchString != null ? $"&search={searchString}" : "";
httpResponse =
await Client.GetAsync($"/admin/users/count{search}");
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
var count = await httpResponse.Content.ReadFromJsonAsync<CountRequestResult>();
return (count!.Value, users);
}
private record AddUserRequestDto(string Username, string Email, string Password, bool IsAdmin);
@ -34,7 +43,7 @@ public class HttpUsersService : IUsersService
public async Task<User> AddUser(string username, string email, string password, bool isAdmin)
{
var httpResponse = await Client.PostAsJsonAsync("/api/admin/user/add",
var httpResponse = await Client.PostAsJsonAsync("/admin/users",
new AddUserRequestDto(username, email, password, isAdmin));
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
@ -56,8 +65,8 @@ public class HttpUsersService : IUsersService
public async Task RemoveUsers(IEnumerable<uint> userIds)
{
var httpResponse =
await Client.PostAsJsonAsync("/api/admin/user/remove-all", new RemoveUsersRequestDto(userIds.ToArray()));
await Client.PostAsJsonAsync("/admin/users/remove-all", new RemoveUsersRequestDto(userIds.ToArray()));
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
}
@ -65,7 +74,7 @@ public class HttpUsersService : IUsersService
public async Task UpdateUser(User user)
{
var httpResponse = await Client.PostAsJsonAsync($"/api/admin/user/{user.Id}/update",
var httpResponse = await Client.PutAsJsonAsync($"/admin/users/{user.Id}",
new UpdateUserRequestDto(user.Email, user.Name, user.IsAdmin));
await ErrorsUtils.EnsureResponseIsOk(httpResponse);
}

Loading…
Cancel
Save