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.
86 lines
2.6 KiB
86 lines
2.6 KiB
using System;
|
|
using HeartTrack.Models;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace HeartTrack.Services.UserDataService
|
|
{
|
|
public class UserDataServiceAPI : IUserDataService
|
|
{
|
|
[Inject]
|
|
private HttpClient _clientHttp { get; set; }
|
|
|
|
|
|
public UserDataServiceAPI(HttpClient clientHttp)
|
|
{
|
|
this._clientHttp = clientHttp;
|
|
}
|
|
|
|
public async Task AddUser(User u)
|
|
{
|
|
HttpResponseMessage response = await _clientHttp.PostAsJsonAsync("http://localhost:8080/api/users", u);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
Console.WriteLine("API - User avec l'id " + u.Id + " ajouté avec succès");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("API - Problème ajout User");
|
|
}
|
|
}
|
|
|
|
public async Task<User> getUserById(int id)
|
|
{
|
|
User User = await _clientHttp.GetFromJsonAsync<User>("http://localhost:8080/api/users/{id}");
|
|
return User;
|
|
}
|
|
|
|
public async Task<List<User>> getAllUsers()
|
|
{
|
|
List<User> lUsers = await _clientHttp.GetFromJsonAsync<List<User>>("http://localhost:8080/api/users");
|
|
return lUsers;
|
|
}
|
|
|
|
public async Task RemoveUser(User u)
|
|
{
|
|
HttpResponseMessage response = await _clientHttp.DeleteAsync($"http://localhost:8080/api/users/{u.Id}");
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
Console.WriteLine("API - User avec l'id " + u.Id + " supprimé avec succès");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("API - Problème suppression User");
|
|
}
|
|
}
|
|
|
|
public async Task SaveAllUsers(List<User> list)
|
|
{
|
|
HttpResponseMessage response = await _clientHttp.PutAsJsonAsync("http://localhost:8080/api/users", list);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
Console.WriteLine("API - List de users sauvegardé avec succès");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("API - Problème sauvegarde List de users");
|
|
}
|
|
}
|
|
|
|
public async Task UpdateUser(User u)
|
|
{
|
|
HttpResponseMessage response = await _clientHttp.PutAsJsonAsync($"http://localhost:8080/api/users/{u.Id}", u);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
Console.WriteLine("API - User avec l'id " + u.Id + " mis à jour avec succès");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("API - Problème mise à jour User");
|
|
}
|
|
}
|
|
}
|
|
} |