dummy_json_rs/
carts.rs

1use crate::{DummyJsonClient, API_BASE_URL};
2use once_cell::sync::Lazy;
3use serde::{Deserialize, Serialize};
4
5static CARTS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/carts", API_BASE_URL));
6
7#[derive(Serialize, Deserialize, Debug, Default)]
8pub struct CartProduct {
9	pub id: u32,
10	pub title: Option<String>,
11	pub price: Option<f32>,
12	pub quantity: Option<u32>,
13	pub total: Option<f32>,
14	#[serde(rename = "discountPercentage")]
15	pub discount_percentage: Option<f32>,
16	#[serde(rename = "discountedTotal")]
17	pub discounted_total: Option<f32>,
18	pub thumbnail: Option<String>,
19}
20
21#[derive(Deserialize, Debug)]
22pub struct Cart {
23	pub id: u32,
24	pub products: Vec<CartProduct>,
25	pub total: f32,
26	#[serde(rename = "discountedTotal")]
27	pub discounted_total: f32,
28	#[serde(rename = "userId")]
29	pub user_id: u32,
30	#[serde(rename = "totalProducts")]
31	pub total_products: u32,
32	#[serde(rename = "totalQuantity")]
33	pub total_quantity: u32,
34}
35
36#[derive(Deserialize, Debug)]
37pub struct GetAllCartsResponse {
38	pub carts: Vec<Cart>,
39	pub total: u32,
40	pub skip: u32,
41	pub limit: u32,
42}
43
44#[derive(Serialize, Debug)]
45pub struct AddCartPayload {
46	#[serde(rename = "userId")]
47	pub user_id: u32,
48	pub products: Vec<CartProduct>,
49}
50
51#[derive(Serialize, Debug, Default)]
52pub struct UpdateCartPayload {
53	#[serde(rename = "userId")]
54	pub user_id: Option<u32>,
55	pub merge: Option<bool>,
56	pub products: Vec<CartProduct>,
57}
58
59#[derive(Deserialize, Debug)]
60pub struct DeleteCartResponse {
61	#[serde(flatten)]
62	pub other_fields: Cart,
63	#[serde(rename = "isDeleted")]
64	pub is_deleted: bool,
65	#[serde(rename = "deletedOn")]
66	pub deleted_on: String,
67}
68
69impl DummyJsonClient {
70	/// Get all carts
71	pub async fn get_all_carts(&self) -> Result<GetAllCartsResponse, reqwest::Error> {
72		let url = CARTS_BASE_URL.as_str();
73		self.client.get(url).send().await?.json::<GetAllCartsResponse>().await
74	}
75
76	/// Get cart by id
77	pub async fn get_cart_by_id(&self, id: u32) -> Result<Cart, reqwest::Error> {
78		let url = format!("{}/{}", CARTS_BASE_URL.as_str(), id);
79		self.client.get(url).send().await?.json::<Cart>().await
80	}
81
82	/// Get carts of user
83	pub async fn get_carts_of_user(
84		&self,
85		user_id: u32,
86	) -> Result<GetAllCartsResponse, reqwest::Error> {
87		let url = format!("{}/user/{}", CARTS_BASE_URL.as_str(), user_id);
88		self.client.get(url).send().await?.json::<GetAllCartsResponse>().await
89	}
90
91	/// Add cart
92	pub async fn add_cart(&self, payload: AddCartPayload) -> Result<Cart, reqwest::Error> {
93		let url = format!("{}/add", CARTS_BASE_URL.as_str());
94		self.client.post(url).json(&payload).send().await?.json::<Cart>().await
95	}
96
97	/// Update cart
98	pub async fn update_cart(
99		&self,
100		id: u32,
101		payload: UpdateCartPayload,
102	) -> Result<Cart, reqwest::Error> {
103		let url = format!("{}/{}", CARTS_BASE_URL.as_str(), id);
104		self.client.put(url).json(&payload).send().await?.json::<Cart>().await
105	}
106
107	/// Delete cart
108	pub async fn delete_cart(&self, cart_id: u32) -> Result<DeleteCartResponse, reqwest::Error> {
109		let url = format!("{}/{}", CARTS_BASE_URL.as_str(), cart_id);
110		self.client.delete(url).send().await?.json::<DeleteCartResponse>().await
111	}
112}