Skip to content

Commit

Permalink
Merge pull request #8 from abhi3700/comments
Browse files Browse the repository at this point in the history
Add comments module (with tests)
  • Loading branch information
abhi3700 authored Nov 1, 2024
2 parents 95eb339 + 1d768ca commit 212750d
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 1 deletion.
55 changes: 55 additions & 0 deletions api-requests/comments.http
Original file line number Diff line number Diff line change
@@ -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
131 changes: 131 additions & 0 deletions src/comments.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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<u32>,
}

#[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<Comment>,
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<GetAllComments, reqwest::Error> {
self.client
.get(COMMENTS_BASE_URL.as_str())
.send()
.await?
.json::<GetAllComments>()
.await
}

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

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

/// Get comments by post id
pub async fn get_comments_by_post_id(
&self,
post_id: u32,
) -> Result<GetAllComments, reqwest::Error> {
self.client
.get(format!("{}/?postId={}", COMMENTS_BASE_URL.as_str(), post_id))
.send()
.await?
.json::<GetAllComments>()
.await
}

/// Add comment
pub async fn add_comment(&self, comment: &AddComment) -> Result<Comment, reqwest::Error> {
self.client
.post(format!("{}/add", COMMENTS_BASE_URL.as_str()))
.json(comment)
.send()
.await?
.json::<Comment>()
.await
}

/// Update comment
pub async fn update_comment(&self, id: u32, body: &str) -> Result<Comment, reqwest::Error> {
let update_comment = &json!({ "body": body });
self.client
.put(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id))
.json(update_comment)
.send()
.await?
.json::<Comment>()
.await
}

/// Delete comment
pub async fn delete_comment(&self, id: u32) -> Result<DeleteCommentResponse, reqwest::Error> {
self.client
.delete(format!("{}/{}", COMMENTS_BASE_URL.as_str(), id))
.send()
.await?
.json::<DeleteCommentResponse>()
.await
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod auth;
mod carts;
mod comments;
mod posts;
mod products;
mod recipes;
mod todos;

pub use auth::*;
pub use carts::*;
pub use comments::*;
pub use posts::*;
pub use products::*;
pub use recipes::*;
Expand Down
2 changes: 1 addition & 1 deletion src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions tests/comments.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 212750d

Please sign in to comment.