1use crate::{DummyJsonClient, ProductCategory, API_BASE_URL};
2use once_cell::sync::Lazy;
3use serde::{Deserialize, Serialize};
4
5static POSTS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/posts", API_BASE_URL));
6
7#[derive(Serialize, Deserialize, Debug, Default)]
8pub struct Post {
9 pub id: u32,
10 #[serde(flatten)]
11 pub other_fields: AddPost,
12}
13
14#[derive(Serialize, Deserialize, Debug, Default)]
15pub struct AddPost {
16 pub title: Option<String>,
17 pub body: Option<String>,
18 pub tags: Option<Vec<String>>,
19 pub reactions: Option<Reaction>,
20 pub views: Option<u32>,
21 #[serde(rename = "userId")]
22 pub user_id: Option<u32>,
23}
24
25#[derive(Serialize, Deserialize, Debug)]
26pub struct Reaction {
27 pub likes: u32,
28 pub dislikes: u32,
29}
30
31#[derive(Deserialize, Debug)]
32pub struct GetAllPosts {
33 pub posts: Vec<Post>,
34 pub total: u32,
35 pub skip: u32,
36 pub limit: u32,
37}
38
39#[derive(Deserialize, Debug)]
40pub struct PostComment {
41 pub id: u32,
42 pub body: String,
43 #[serde(rename = "postId")]
44 pub post_id: u32,
45 pub likes: u32,
46 pub user: UserProfile,
47}
48
49#[derive(Serialize, Deserialize, Debug)]
50pub struct UserProfile {
51 pub id: u32,
52 pub username: String,
53 #[serde(rename = "fullName")]
54 pub full_name: String,
55}
56
57#[derive(Deserialize, Debug)]
58pub struct PostCommentsResponse {
59 pub comments: Vec<PostComment>,
60 pub total: u32,
61 pub skip: u32,
62 pub limit: u32,
63}
64
65impl DummyJsonClient {
66 pub async fn get_all_posts(&self) -> Result<GetAllPosts, reqwest::Error> {
68 self.client
69 .get(POSTS_BASE_URL.as_str())
70 .send()
71 .await?
72 .json::<GetAllPosts>()
73 .await
74 }
75
76 pub async fn get_post_by_id(&self, id: u32) -> Result<Post, reqwest::Error> {
78 self.client
79 .get(format!("{}/{}", POSTS_BASE_URL.as_str(), id))
80 .send()
81 .await?
82 .json::<Post>()
83 .await
84 }
85
86 pub async fn search_posts(&self, query: &str) -> Result<GetAllPosts, reqwest::Error> {
88 self.client
89 .get(format!("{}/search?q={}", POSTS_BASE_URL.as_str(), query))
90 .send()
91 .await?
92 .json::<GetAllPosts>()
93 .await
94 }
95
96 pub async fn limit_and_skip_posts(
98 &self,
99 limit: u32,
100 skip: u32,
101 selects: &str,
102 ) -> Result<GetAllPosts, reqwest::Error> {
103 self.client
104 .get(format!(
105 "{}/?limit={}&skip={}&select={}",
106 POSTS_BASE_URL.as_str(),
107 limit,
108 skip,
109 selects
110 ))
111 .send()
112 .await?
113 .json::<GetAllPosts>()
114 .await
115 }
116
117 pub async fn sort_posts(
119 &self,
120 sort_by: &str,
121 order: &str,
123 ) -> Result<GetAllPosts, reqwest::Error> {
124 self.client
125 .get(format!("{}/?sortBy={}&order={}", POSTS_BASE_URL.as_str(), sort_by, order))
126 .send()
127 .await?
128 .json::<GetAllPosts>()
129 .await
130 }
131
132 pub async fn get_all_posts_tags(&self) -> Result<Vec<ProductCategory>, reqwest::Error> {
134 self.client
135 .get(format!("{}/tags", POSTS_BASE_URL.as_str()))
136 .send()
137 .await?
138 .json::<Vec<ProductCategory>>()
139 .await
140 }
141
142 pub async fn get_posts_by_tags(&self, tag: &str) -> Result<GetAllPosts, reqwest::Error> {
144 self.client
145 .get(format!("{}/tag/{}", POSTS_BASE_URL.as_str(), tag))
146 .send()
147 .await?
148 .json::<GetAllPosts>()
149 .await
150 }
151
152 pub async fn get_posts_by_user_id(&self, user_id: u32) -> Result<GetAllPosts, reqwest::Error> {
154 self.client
155 .get(format!("{}/user/{}", POSTS_BASE_URL.as_str(), user_id))
156 .send()
157 .await?
158 .json::<GetAllPosts>()
159 .await
160 }
161
162 pub async fn get_post_comments(&self, id: u32) -> Result<PostCommentsResponse, reqwest::Error> {
164 self.client
165 .get(format!("{}/{}/comments", POSTS_BASE_URL.as_str(), id))
166 .send()
167 .await?
168 .json::<PostCommentsResponse>()
169 .await
170 }
171
172 pub async fn add_post(&self, post: &AddPost) -> Result<Post, reqwest::Error> {
174 self.client
175 .post(format!("{}/add", POSTS_BASE_URL.as_str()))
176 .json(post)
177 .send()
178 .await?
179 .json::<Post>()
180 .await
181 }
182
183 pub async fn update_post(&self, id: u32, post: &AddPost) -> Result<AddPost, reqwest::Error> {
185 self.client
186 .put(format!("{}/{}", POSTS_BASE_URL.as_str(), id))
187 .json(post)
188 .send()
189 .await?
190 .json::<AddPost>()
191 .await
192 }
193
194 pub async fn delete_post(&self, id: u32) -> Result<Post, reqwest::Error> {
196 self.client
197 .delete(format!("{}/{}", POSTS_BASE_URL.as_str(), id))
198 .send()
199 .await?
200 .json::<Post>()
201 .await
202 }
203}