-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from abhi3700/comments
Add comments module (with tests)
- Loading branch information
Showing
5 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |