-
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 #4 from abhi3700/carts
Add carts (with tests) module
- Loading branch information
Showing
4 changed files
with
257 additions
and
0 deletions.
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,72 @@ | ||
@host=https://dummyjson.com/carts | ||
|
||
### | ||
# @name GetCarts | ||
GET {{host}} | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name GetCartById | ||
@id=1 | ||
GET {{host}}/{{id}} | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name GetCartsOfUser | ||
@user_id=33 | ||
GET {{host}}/user/{{user_id}} | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name AddCart | ||
@user_id=33 | ||
POST {{host}}/add | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"userId": {{user_id}}, | ||
"products": [ | ||
{ | ||
"id": 144, | ||
"quantity": 4 | ||
}, | ||
{ | ||
"id": 98, | ||
"quantity": 1 | ||
} | ||
] | ||
} | ||
|
||
### | ||
# @name UpdateCart | ||
@cart_id=50 | ||
@user_id=34 | ||
PUT {{host}}/{{cart_id}} | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"userId": {{user_id}}, | ||
"merge": true, | ||
"products": [ | ||
{ | ||
"id": 145, | ||
"quantity": 4 | ||
}, | ||
{ | ||
"id": 98, | ||
"quantity": 1 | ||
} | ||
] | ||
} | ||
|
||
### | ||
# @name DeleteCart | ||
@cart_id=50 | ||
DELETE {{host}}/{{cart_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,112 @@ | ||
use crate::{DummyJsonClient, API_BASE_URL}; | ||
use once_cell::sync::Lazy; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
const CARTS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/carts", API_BASE_URL)); | ||
|
||
#[derive(Serialize, Deserialize, Debug, Default)] | ||
pub struct CartProduct { | ||
pub id: u32, | ||
pub title: Option<String>, | ||
pub price: Option<f32>, | ||
pub quantity: Option<u32>, | ||
pub total: Option<f32>, | ||
#[serde(rename = "discountPercentage")] | ||
pub discount_percentage: Option<f32>, | ||
#[serde(rename = "discountedTotal")] | ||
pub discounted_total: Option<f32>, | ||
pub thumbnail: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Cart { | ||
pub id: u32, | ||
pub products: Vec<CartProduct>, | ||
pub total: f32, | ||
#[serde(rename = "discountedTotal")] | ||
pub discounted_total: f32, | ||
#[serde(rename = "userId")] | ||
pub user_id: u32, | ||
#[serde(rename = "totalProducts")] | ||
pub total_products: u32, | ||
#[serde(rename = "totalQuantity")] | ||
pub total_quantity: u32, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct GetAllCartsResponse { | ||
pub carts: Vec<Cart>, | ||
pub total: u32, | ||
pub skip: u32, | ||
pub limit: u32, | ||
} | ||
|
||
#[derive(Serialize, Debug)] | ||
pub struct AddCartPayload { | ||
#[serde(rename = "userId")] | ||
pub user_id: u32, | ||
pub products: Vec<CartProduct>, | ||
} | ||
|
||
#[derive(Serialize, Debug, Default)] | ||
pub struct UpdateCartPayload { | ||
#[serde(rename = "userId")] | ||
pub user_id: Option<u32>, | ||
pub merge: Option<bool>, | ||
pub products: Vec<CartProduct>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct DeleteCartResponse { | ||
#[serde(flatten)] | ||
pub other_fields: Cart, | ||
#[serde(rename = "isDeleted")] | ||
pub is_deleted: bool, | ||
#[serde(rename = "deletedOn")] | ||
pub deleted_on: String, | ||
} | ||
|
||
impl DummyJsonClient { | ||
/// Get all carts | ||
pub async fn get_all_carts(&self) -> Result<GetAllCartsResponse, reqwest::Error> { | ||
let url = &*CARTS_BASE_URL; | ||
self.client.get(url).send().await?.json::<GetAllCartsResponse>().await | ||
} | ||
|
||
/// Get cart by id | ||
pub async fn get_cart_by_id(&self, id: u32) -> Result<Cart, reqwest::Error> { | ||
let url = format!("{}/{}", &*CARTS_BASE_URL, id); | ||
self.client.get(url).send().await?.json::<Cart>().await | ||
} | ||
|
||
/// Get carts of user | ||
pub async fn get_carts_of_user( | ||
&self, | ||
user_id: u32, | ||
) -> Result<GetAllCartsResponse, reqwest::Error> { | ||
let url = format!("{}/user/{}", &*CARTS_BASE_URL, user_id); | ||
self.client.get(url).send().await?.json::<GetAllCartsResponse>().await | ||
} | ||
|
||
/// Add cart | ||
pub async fn add_cart(&self, payload: AddCartPayload) -> Result<Cart, reqwest::Error> { | ||
let url = format!("{}/add", &*CARTS_BASE_URL); | ||
self.client.post(url).json(&payload).send().await?.json::<Cart>().await | ||
} | ||
|
||
/// Update cart | ||
pub async fn update_cart( | ||
&self, | ||
id: u32, | ||
payload: UpdateCartPayload, | ||
) -> Result<Cart, reqwest::Error> { | ||
let url = format!("{}/{}", &*CARTS_BASE_URL, id); | ||
self.client.put(url).json(&payload).send().await?.json::<Cart>().await | ||
} | ||
|
||
/// Delete cart | ||
pub async fn delete_cart(&self, cart_id: u32) -> Result<DeleteCartResponse, reqwest::Error> { | ||
let url = format!("{}/{}", &*CARTS_BASE_URL, cart_id); | ||
self.client.delete(url).send().await?.json::<DeleteCartResponse>().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
mod carts { | ||
use dummy_json_rs::{AddCartPayload, CartProduct, DummyJsonClient, UpdateCartPayload}; | ||
|
||
#[tokio::test] | ||
async fn get_all_carts() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_all_carts().await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_cart_by_id() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_cart_by_id(1).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_carts_of_user() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_carts_of_user(33).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn add_cart() { | ||
let client = DummyJsonClient::default(); | ||
let response = client | ||
.add_cart(AddCartPayload { | ||
user_id: 33, | ||
products: vec![ | ||
CartProduct { id: 144, quantity: Some(4), ..Default::default() }, | ||
CartProduct { id: 98, quantity: Some(1), ..Default::default() }, | ||
], | ||
}) | ||
.await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn update_cart() { | ||
let client = DummyJsonClient::default(); | ||
let response = client | ||
.update_cart( | ||
1, | ||
UpdateCartPayload { | ||
user_id: Some(34), | ||
merge: Some(true), | ||
products: vec![ | ||
CartProduct { id: 144, quantity: Some(4), ..Default::default() }, | ||
CartProduct { id: 98, quantity: Some(1), ..Default::default() }, | ||
], | ||
}, | ||
) | ||
.await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn delete_cart() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.delete_cart(1).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
} |