dummy_json_rs/
posts.rs

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::{DummyJsonClient, ProductCategory, API_BASE_URL};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

static POSTS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/posts", API_BASE_URL));

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Post {
	pub id: u32,
	#[serde(flatten)]
	pub other_fields: AddPost,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AddPost {
	pub title: Option<String>,
	pub body: Option<String>,
	pub tags: Option<Vec<String>>,
	pub reactions: Option<Reaction>,
	pub views: Option<u32>,
	#[serde(rename = "userId")]
	pub user_id: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Reaction {
	pub likes: u32,
	pub dislikes: u32,
}

#[derive(Deserialize, Debug)]
pub struct GetAllPosts {
	pub posts: Vec<Post>,
	pub total: u32,
	pub skip: u32,
	pub limit: u32,
}

#[derive(Deserialize, Debug)]
pub struct PostComment {
	pub id: u32,
	pub body: String,
	#[serde(rename = "postId")]
	pub post_id: u32,
	pub likes: u32,
	pub user: UserProfile,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UserProfile {
	pub id: u32,
	pub username: String,
	#[serde(rename = "fullName")]
	pub full_name: String,
}

#[derive(Deserialize, Debug)]
pub struct PostCommentsResponse {
	pub comments: Vec<PostComment>,
	pub total: u32,
	pub skip: u32,
	pub limit: u32,
}

impl DummyJsonClient {
	/// Get all posts
	pub async fn get_all_posts(&self) -> Result<GetAllPosts, reqwest::Error> {
		self.client
			.get(POSTS_BASE_URL.as_str())
			.send()
			.await?
			.json::<GetAllPosts>()
			.await
	}

	/// Get post by id
	pub async fn get_post_by_id(&self, id: u32) -> Result<Post, reqwest::Error> {
		self.client
			.get(format!("{}/{}", POSTS_BASE_URL.as_str(), id))
			.send()
			.await?
			.json::<Post>()
			.await
	}

	/// Search posts
	pub async fn search_posts(&self, query: &str) -> Result<GetAllPosts, reqwest::Error> {
		self.client
			.get(format!("{}/search?q={}", POSTS_BASE_URL.as_str(), query))
			.send()
			.await?
			.json::<GetAllPosts>()
			.await
	}

	/// Limit and skip posts
	pub async fn limit_and_skip_posts(
		&self,
		limit: u32,
		skip: u32,
		selects: &str,
	) -> Result<GetAllPosts, reqwest::Error> {
		self.client
			.get(format!(
				"{}/?limit={}&skip={}&select={}",
				POSTS_BASE_URL.as_str(),
				limit,
				skip,
				selects
			))
			.send()
			.await?
			.json::<GetAllPosts>()
			.await
	}

	/// Sort posts
	pub async fn sort_posts(
		&self,
		sort_by: &str,
		// TODO: Change to enum
		order: &str,
	) -> Result<GetAllPosts, reqwest::Error> {
		self.client
			.get(format!("{}/?sortBy={}&order={}", POSTS_BASE_URL.as_str(), sort_by, order))
			.send()
			.await?
			.json::<GetAllPosts>()
			.await
	}

	/// Get all posts tags
	pub async fn get_all_posts_tags(&self) -> Result<Vec<ProductCategory>, reqwest::Error> {
		self.client
			.get(format!("{}/tags", POSTS_BASE_URL.as_str()))
			.send()
			.await?
			.json::<Vec<ProductCategory>>()
			.await
	}

	/// Get posts by tags
	pub async fn get_posts_by_tags(&self, tag: &str) -> Result<GetAllPosts, reqwest::Error> {
		self.client
			.get(format!("{}/tag/{}", POSTS_BASE_URL.as_str(), tag))
			.send()
			.await?
			.json::<GetAllPosts>()
			.await
	}

	/// Get posts by user id
	pub async fn get_posts_by_user_id(&self, user_id: u32) -> Result<GetAllPosts, reqwest::Error> {
		self.client
			.get(format!("{}/user/{}", POSTS_BASE_URL.as_str(), user_id))
			.send()
			.await?
			.json::<GetAllPosts>()
			.await
	}

	/// Get post comments
	pub async fn get_post_comments(&self, id: u32) -> Result<PostCommentsResponse, reqwest::Error> {
		self.client
			.get(format!("{}/{}/comments", POSTS_BASE_URL.as_str(), id))
			.send()
			.await?
			.json::<PostCommentsResponse>()
			.await
	}

	/// Add post
	pub async fn add_post(&self, post: &AddPost) -> Result<Post, reqwest::Error> {
		self.client
			.post(format!("{}/add", POSTS_BASE_URL.as_str()))
			.json(post)
			.send()
			.await?
			.json::<Post>()
			.await
	}

	/// Update post
	pub async fn update_post(&self, id: u32, post: &AddPost) -> Result<AddPost, reqwest::Error> {
		self.client
			.put(format!("{}/{}", POSTS_BASE_URL.as_str(), id))
			.json(post)
			.send()
			.await?
			.json::<AddPost>()
			.await
	}

	/// Delete post
	pub async fn delete_post(&self, id: u32) -> Result<Post, reqwest::Error> {
		self.client
			.delete(format!("{}/{}", POSTS_BASE_URL.as_str(), id))
			.send()
			.await?
			.json::<Post>()
			.await
	}
}