-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDemo.cs
More file actions
73 lines (61 loc) · 2.04 KB
/
UserDemo.cs
File metadata and controls
73 lines (61 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Windows.Forms;
namespace ClientRest
{
public class UserDemo
{
HttpClient httpClient=new HttpClient();
public UserDemo()
{
httpClient.BaseAddress = new System.Uri("https://localhost:44322/");
}
public int id { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public List<UserDemo> GetAllUser()
{
string informations = httpClient.GetStringAsync("api/users").Result;
List<UserDemo> userDemos = JsonConvert.DeserializeObject<List<UserDemo>>(informations);
return userDemos;
}
public void ShowAllUsers(DataGridView dtgList)
{
List<UserDemo> userDemosList = GetAllUser();
if (dtgList.Rows.Count >= 0)
{
dtgList.Rows.Clear();
foreach (var user in userDemosList)
{
dtgList.Rows.Add(user.id, user.Nom, user.Prenom);
}
}
else
{
foreach (var user in userDemosList)
{
dtgList.Rows.Add(user.id, user.Nom, user.Prenom);
}
}
}
public UserDemo ShowUserById(int id)
{
string informations = httpClient.GetStringAsync("api/users/" + id).Result;
UserDemo userDemo = JsonConvert.DeserializeObject<UserDemo>(informations);
return userDemo;
}
public async void CreateUser(UserDemo userDemo)
{
var reponse = httpClient.PostAsJsonAsync("api/users", userDemo).Result;
}
public async void UpdateUser(int id, UserDemo userDemo)
{
var response = httpClient.PutAsJsonAsync("api/users/" + id, userDemo).Result;
}
public async void DeleteUser(int id)
{
var response = httpClient.DeleteAsync("api/users/" + id);
}
}
}