dummy_json_rs/
comments.rs

1use crate::{DummyJsonClient, UserProfile, API_BASE_URL};
2use once_cell::sync::Lazy;
3use serde::{Deserialize, Serialize};
4use serde_json::json;
5
6static COMMENTS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/comments", API_BASE_URL));
7
8#[derive(Serialize, Deserialize, Debug)]
9pub struct Comment {
10	pub id: u32,
11	pub body: String,
12	#[serde(rename = "postId")]
13	pub post_id: u32,
14	pub user: UserProfile,
15	pub likes: Option<u32>,
16}
17
18#[derive(Serialize, Deserialize, Debug)]
19pub struct AddComment {
20	pub body: String,
21	#[serde(rename = "postId")]
22	pub post_id: u32,
23	#[serde(rename = "userId")]
24	pub user_id: u32,
25}
26
27#[derive(Deserialize, Debug)]
28pub struct GetAllComments {
29	pub comments: Vec<Comment>,
30	pub total: u32,
31	pub skip: u32,
32	pub limit: u32,
33}
34
35#[derive(Deserialize, Debug)]
36pub struct DeleteCommentResponse {
37	#[serde(flatten)]
38	pub other_fields: Comment,
39	#[serde(rename = "isDeleted")]
40	pub is_deleted: bool,
41	#[serde(rename = "deletedOn")]
42	pub deleted_on: String,
43}
44impl DummyJsonClient {
45	/// Get all comments
46	pub async fn get_all_comments(&self) -> Result<GetAllComments, reqwest::Error> {
47		self.client
48			.get(COMMENTS_BASE_URL.as_str())
49			.send()
50			.await?
51			.json::<GetAllComments>()
52			.await
53	}
54
55	/// Get comment by id
56	pub async fn get_comment_by_id(&self, id: u32) -> Result<Comment, reqwest::Error> {
57		self.client
58			.get(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id))
59			.send()
60			.await?
61			.json::<Comment>()
62			.await
63	}
64
65	/// Limit and skip comments
66	pub async fn limit_and_skip_comments(
67		&self,
68		limit: u32,
69		skip: u32,
70		select: &str,
71	) -> Result<GetAllComments, reqwest::Error> {
72		self.client
73			.get(format!(
74				"{}/?limit={}&skip={}&select={}",
75				COMMENTS_BASE_URL.as_str(),
76				limit,
77				skip,
78				select
79			))
80			.send()
81			.await?
82			.json::<GetAllComments>()
83			.await
84	}
85
86	/// Get comments by post id
87	pub async fn get_comments_by_post_id(
88		&self,
89		post_id: u32,
90	) -> Result<GetAllComments, reqwest::Error> {
91		self.client
92			.get(format!("{}/?postId={}", COMMENTS_BASE_URL.as_str(), post_id))
93			.send()
94			.await?
95			.json::<GetAllComments>()
96			.await
97	}
98
99	/// Add comment
100	pub async fn add_comment(&self, comment: &AddComment) -> Result<Comment, reqwest::Error> {
101		self.client
102			.post(format!("{}/add", COMMENTS_BASE_URL.as_str()))
103			.json(comment)
104			.send()
105			.await?
106			.json::<Comment>()
107			.await
108	}
109
110	/// Update comment
111	pub async fn update_comment(&self, id: u32, body: &str) -> Result<Comment, reqwest::Error> {
112		let update_comment = &json!({ "body": body });
113		self.client
114			.put(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id))
115			.json(update_comment)
116			.send()
117			.await?
118			.json::<Comment>()
119			.await
120	}
121
122	/// Delete comment
123	pub async fn delete_comment(&self, id: u32) -> Result<DeleteCommentResponse, reqwest::Error> {
124		self.client
125			.delete(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id))
126			.send()
127			.await?
128			.json::<DeleteCommentResponse>()
129			.await
130	}
131}