-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
166 lines (135 loc) · 5.12 KB
/
Program.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace SandboxCSharpConsumeRestApi
{
class MainClass
{
// https://jsonplaceholder.typicode.com << Faked REST service
// https://github.com/typicode/json-server << Details on querying the faked REST service
private static List<User> UserList = new List<User>();
public static void Main(string[] args)
{
MainAsync().Wait();
}
public static async Task MainAsync()
{
JArray users = await RestServiceHelper.GetResponseAsObject("https://jsonplaceholder.typicode.com/users?_sort=username&_order=asc");
JArray todos = await RestServiceHelper.GetResponseAsObject("https://jsonplaceholder.typicode.com/todos?_sort=userId,title&_order=asc,asc");
JArray posts = await RestServiceHelper.GetResponseAsObject("https://jsonplaceholder.typicode.com/posts?_sort=userId,title&_order=asc,asc");
JArray comments = await RestServiceHelper.GetResponseAsObject("https://jsonplaceholder.typicode.com/comments?_sort=postId,name&_order=asc,asc");
JArray albums = await RestServiceHelper.GetResponseAsObject("https://jsonplaceholder.typicode.com/albums?_sort=userId,title&_order=asc,asc");
JArray photos = await RestServiceHelper.GetResponseAsObject("https://jsonplaceholder.typicode.com/photos?_sort=albumId,title&_order=asc,asc");
// Process Users
// *************
User foundUser = null;
foreach (JToken item in users)
{
User user = new User();
user.Id = int.Parse(item["id"].ToString());
user.Name = item["name"].ToString();
user.UserName = item["username"].ToString();
user.Email = item["email"].ToString();
user.Phone = item["phone"].ToString();
user.Website = item["website"].ToString();
user.CompanyName = item["company"]["name"].ToString();
user.CompanyCatchPhrase = item["company"]["catchPhrase"].ToString();
user.CompanyBs = item["company"]["bs"].ToString();
user.Address.Id = 0; // No ID provided
user.Address.UserId = user.Id;
user.Address.Street = item["address"]["street"].ToString();
user.Address.Suite = item["address"]["suite"].ToString();
user.Address.City = item["address"]["city"].ToString();
user.Address.ZipCode = item["address"]["zipcode"].ToString();
user.Address.GeoLatitude = decimal.Parse(item["address"]["geo"]["lat"].ToString());
user.Address.GeoLongitude = decimal.Parse(item["address"]["geo"]["lng"].ToString());
UserList.Add(user);
}
// Process ToDos
// *************
foundUser = null;
foreach (JToken item in todos)
{
ToDo todo = new ToDo();
todo.Id = int.Parse(item["id"].ToString());
todo.UserId = int.Parse(item["userId"].ToString());
todo.Title = item["title"].ToString();
todo.Completed = bool.Parse(item["completed"].ToString());
// Find the ToDo's user, and append
if (foundUser == null || todo.UserId != foundUser.Id)
{
foundUser = UserList.FirstOrDefault(x => x.Id == todo.UserId);
}
if (foundUser != null)
{
foundUser.Todos.Add(todo);
}
}
// Process Posts
// *************
foundUser = null;
foreach (JToken item in posts)
{
Post post = new Post();
post.Id = int.Parse(item["id"].ToString());
post.UserId = int.Parse(item["userId"].ToString());
post.Title = item["title"].ToString();
post.Body = item["body"].ToString();
// Append Comments for this Post
IEnumerable<JToken> postsComments = comments.Where(x => int.Parse(x["postId"].ToString()) == post.Id);
foreach (JToken c in postsComments)
{
Comment comment = new Comment();
comment.Id = int.Parse(c["id"].ToString());
comment.PostId = int.Parse(c["postId"].ToString());
comment.Name = c["name"].ToString();
comment.Email = c["email"].ToString();
comment.Body = c["body"].ToString();
post.Comments.Add(comment);
}
// Find the Post's user, and append
if (foundUser == null || post.UserId != foundUser.Id)
{
foundUser = UserList.FirstOrDefault(x => x.Id == post.UserId);
}
if (foundUser != null)
{
foundUser.Posts.Add(post);
}
}
// Process Albums
// **************
foundUser = null;
foreach (JToken item in albums)
{
Album album = new Album();
album.Id = int.Parse(item["id"].ToString());
album.UserId = int.Parse(item["userId"].ToString());
album.Title = item["title"].ToString();
// Append Photos for this Album
IEnumerable<JToken> albumsPhotos = photos.Where(x => int.Parse(x["albumId"].ToString()) == album.Id);
foreach (JToken p in albumsPhotos)
{
Photo photo = new Photo();
photo.Id = int.Parse(p["id"].ToString());
photo.AlbumId = int.Parse(p["albumId"].ToString());
photo.Title = p["title"].ToString();
photo.Url = p["url"].ToString();
photo.ThumbnailUrl = p["thumbnailUrl"].ToString();
album.Photos.Add(photo);
}
// Find the Album's user, and append
if (foundUser == null || album.UserId != foundUser.Id)
{
foundUser = UserList.FirstOrDefault(x => x.Id == album.UserId);
}
if (foundUser != null)
{
foundUser.Albums.Add(album);
}
}
}
}
}