diff --git a/api-requests/comments.http b/api-requests/comments.http new file mode 100644 index 0000000..c8a426d --- /dev/null +++ b/api-requests/comments.http @@ -0,0 +1,55 @@ +@host=https://dummyjson.com/comments + +### +# @name GetAllComments +GET {{host}} +Accept: application/json +Content-Type: application/json + +### +# @name GetCommentById +@id=1 +GET {{host}}/{{id}} +Accept: application/json +Content-Type: application/json + +### +# @name LimitAndSkipComments +GET {{host}}?limit=10&skip=10&select=body,postId +Accept: application/json +Content-Type: application/json + +### +# @name GetCommmentsByPostId +@postId=1 +GET {{host}}?postId={{postId}} +Accept: application/json +Content-Type: application/json + +### +# @name AddComment +POST {{host}}/add +Accept: application/json +Content-Type: application/json + +{ + "body": "This makes all sense to me!", + "postId": 3, + "userId": 5 +} + +### +# @name UpdateComment +PUT {{host}}/{{id}} +Accept: application/json +Content-Type: application/json + +{ + "body": "This makes all sense to me! - updated" +} + +### +# @name DeleteComment +DELETE {{host}}/{{id}} +Accept: application/json +Content-Type: application/json diff --git a/src/comments.rs b/src/comments.rs new file mode 100644 index 0000000..1534d9f --- /dev/null +++ b/src/comments.rs @@ -0,0 +1,131 @@ +use crate::{DummyJsonClient, UserProfile, API_BASE_URL}; +use once_cell::sync::Lazy; +use serde::{Deserialize, Serialize}; +use serde_json::json; + +static COMMENTS_BASE_URL: Lazy = Lazy::new(|| format!("{}/comments", API_BASE_URL)); + +#[derive(Serialize, Deserialize, Debug)] +pub struct Comment { + pub id: u32, + pub body: String, + #[serde(rename = "postId")] + pub post_id: u32, + pub user: UserProfile, + pub likes: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct AddComment { + pub body: String, + #[serde(rename = "postId")] + pub post_id: u32, + #[serde(rename = "userId")] + pub user_id: u32, +} + +#[derive(Deserialize, Debug)] +pub struct GetAllComments { + pub comments: Vec, + pub total: u32, + pub skip: u32, + pub limit: u32, +} + +#[derive(Deserialize, Debug)] +pub struct DeleteCommentResponse { + #[serde(flatten)] + other_fields: Comment, + #[serde(rename = "isDeleted")] + pub is_deleted: bool, + #[serde(rename = "deletedOn")] + pub deleted_on: String, +} +impl DummyJsonClient { + /// Get all comments + pub async fn get_all_comments(&self) -> Result { + self.client + .get(COMMENTS_BASE_URL.as_str()) + .send() + .await? + .json::() + .await + } + + /// Get comment by id + pub async fn get_comment_by_id(&self, id: u32) -> Result { + self.client + .get(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id)) + .send() + .await? + .json::() + .await + } + + /// Limit and skip comments + pub async fn limit_and_skip_comments( + &self, + limit: u32, + skip: u32, + select: &str, + ) -> Result { + self.client + .get(format!( + "{}/?limit={}&skip={}&select={}", + COMMENTS_BASE_URL.as_str(), + limit, + skip, + select + )) + .send() + .await? + .json::() + .await + } + + /// Get comments by post id + pub async fn get_comments_by_post_id( + &self, + post_id: u32, + ) -> Result { + self.client + .get(format!("{}/?postId={}", COMMENTS_BASE_URL.as_str(), post_id)) + .send() + .await? + .json::() + .await + } + + /// Add comment + pub async fn add_comment(&self, comment: &AddComment) -> Result { + self.client + .post(format!("{}/add", COMMENTS_BASE_URL.as_str())) + .json(comment) + .send() + .await? + .json::() + .await + } + + /// Update comment + pub async fn update_comment(&self, id: u32, body: &str) -> Result { + let update_comment = &json!({ "body": body }); + self.client + .put(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id)) + .json(update_comment) + .send() + .await? + .json::() + .await + } + + /// Delete comment + pub async fn delete_comment(&self, id: u32) -> Result { + self.client + .delete(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id)) + .send() + .await? + .json::() + .await + } +} diff --git a/src/lib.rs b/src/lib.rs index 7f46007..5f53cba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod auth; mod carts; +mod comments; mod posts; mod products; mod recipes; @@ -7,6 +8,7 @@ mod todos; pub use auth::*; pub use carts::*; +pub use comments::*; pub use posts::*; pub use products::*; pub use recipes::*; diff --git a/src/posts.rs b/src/posts.rs index e7a47fb..e8cb3d7 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -46,7 +46,7 @@ pub struct PostComment { pub user: UserProfile, } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct UserProfile { pub id: u32, pub username: String, diff --git a/tests/comments.rs b/tests/comments.rs new file mode 100644 index 0000000..e86cd31 --- /dev/null +++ b/tests/comments.rs @@ -0,0 +1,61 @@ +mod comments { + use dummy_json_rs::{AddComment, DummyJsonClient}; + + #[tokio::test] + async fn get_all_comments() { + let client = DummyJsonClient::default(); + let response = client.get_all_comments().await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_comment_by_id() { + let client = DummyJsonClient::default(); + let response = client.get_comment_by_id(1).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn limit_and_skip_comments() { + let client = DummyJsonClient::default(); + let response = client.limit_and_skip_comments(10, 10, "body,postId").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_comments_by_post_id() { + let client = DummyJsonClient::default(); + let response = client.get_comments_by_post_id(1).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn add_comment() { + let client = DummyJsonClient::default(); + let comment = + AddComment { body: "This makes all sense to me!".to_string(), post_id: 3, user_id: 5 }; + let response = client.add_comment(&comment).await; + // assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn update_comment() { + let client = DummyJsonClient::default(); + let response = client.update_comment(1, "This makes all sense to me! - updated").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn delete_comment() { + let client = DummyJsonClient::default(); + let response = client.delete_comment(1).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } +}