use serde::{Deserialize, Serialize};
use crate::rest::{ResourceOperation, ResourcePath, RestResource};
use crate::HttpMethod;
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Collect {
#[serde(skip_serializing)]
pub id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub product_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub collection_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sort_value: Option<String>,
#[serde(skip_serializing)]
pub created_at: Option<String>,
#[serde(skip_serializing)]
pub updated_at: Option<String>,
}
impl RestResource for Collect {
type Id = u64;
type FindParams = CollectFindParams;
type AllParams = CollectListParams;
type CountParams = CollectCountParams;
const NAME: &'static str = "Collect";
const PLURAL: &'static str = "collects";
const PATHS: &'static [ResourcePath] = &[
ResourcePath::new(
HttpMethod::Get,
ResourceOperation::Find,
&["id"],
"collects/{id}",
),
ResourcePath::new(HttpMethod::Get, ResourceOperation::All, &[], "collects"),
ResourcePath::new(
HttpMethod::Get,
ResourceOperation::Count,
&[],
"collects/count",
),
ResourcePath::new(HttpMethod::Post, ResourceOperation::Create, &[], "collects"),
ResourcePath::new(
HttpMethod::Delete,
ResourceOperation::Delete,
&["id"],
"collects/{id}",
),
];
fn get_id(&self) -> Option<Self::Id> {
self.id
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct CollectFindParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct CollectListParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub since_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub product_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub collection_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct CollectCountParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub product_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub collection_id: Option<u64>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rest::{get_path, ResourceOperation, RestResource};
#[test]
fn test_collect_serialization() {
let collect = Collect {
id: Some(455204334),
product_id: Some(632910392),
collection_id: Some(841564295),
position: Some(1),
sort_value: Some("0000000001".to_string()),
created_at: Some("2024-01-15T10:30:00-05:00".to_string()),
updated_at: Some("2024-01-15T10:30:00-05:00".to_string()),
};
let json = serde_json::to_string(&collect).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(parsed["product_id"], 632910392);
assert_eq!(parsed["collection_id"], 841564295);
assert_eq!(parsed["position"], 1);
assert_eq!(parsed["sort_value"], "0000000001");
assert!(parsed.get("id").is_none());
assert!(parsed.get("created_at").is_none());
assert!(parsed.get("updated_at").is_none());
}
#[test]
fn test_collect_deserialization() {
let json = r#"{
"id": 455204334,
"product_id": 632910392,
"collection_id": 841564295,
"position": 1,
"sort_value": "0000000001",
"created_at": "2024-01-15T10:30:00-05:00",
"updated_at": "2024-01-15T10:30:00-05:00"
}"#;
let collect: Collect = serde_json::from_str(json).unwrap();
assert_eq!(collect.id, Some(455204334));
assert_eq!(collect.product_id, Some(632910392));
assert_eq!(collect.collection_id, Some(841564295));
assert_eq!(collect.position, Some(1));
assert_eq!(collect.sort_value, Some("0000000001".to_string()));
assert_eq!(
collect.created_at,
Some("2024-01-15T10:30:00-05:00".to_string())
);
}
#[test]
fn test_collect_limited_paths_no_update() {
let find_path = get_path(Collect::PATHS, ResourceOperation::Find, &["id"]);
assert!(find_path.is_some());
assert_eq!(find_path.unwrap().template, "collects/{id}");
let all_path = get_path(Collect::PATHS, ResourceOperation::All, &[]);
assert!(all_path.is_some());
assert_eq!(all_path.unwrap().template, "collects");
let count_path = get_path(Collect::PATHS, ResourceOperation::Count, &[]);
assert!(count_path.is_some());
assert_eq!(count_path.unwrap().template, "collects/count");
let create_path = get_path(Collect::PATHS, ResourceOperation::Create, &[]);
assert!(create_path.is_some());
assert_eq!(create_path.unwrap().template, "collects");
let delete_path = get_path(Collect::PATHS, ResourceOperation::Delete, &["id"]);
assert!(delete_path.is_some());
assert_eq!(delete_path.unwrap().template, "collects/{id}");
let update_path = get_path(Collect::PATHS, ResourceOperation::Update, &["id"]);
assert!(update_path.is_none());
}
#[test]
fn test_collect_constants() {
assert_eq!(Collect::NAME, "Collect");
assert_eq!(Collect::PLURAL, "collects");
}
#[test]
fn test_collect_get_id() {
let collect_with_id = Collect {
id: Some(455204334),
product_id: Some(632910392),
collection_id: Some(841564295),
..Default::default()
};
assert_eq!(collect_with_id.get_id(), Some(455204334));
let collect_without_id = Collect::default();
assert_eq!(collect_without_id.get_id(), None);
}
#[test]
fn test_collect_list_params() {
let params = CollectListParams {
limit: Some(50),
since_id: Some(1000),
product_id: Some(632910392),
collection_id: Some(841564295),
fields: Some("id,product_id,collection_id".to_string()),
};
let json = serde_json::to_value(¶ms).unwrap();
assert_eq!(json["limit"], 50);
assert_eq!(json["since_id"], 1000);
assert_eq!(json["product_id"], 632910392);
assert_eq!(json["collection_id"], 841564295);
assert_eq!(json["fields"], "id,product_id,collection_id");
}
#[test]
fn test_collect_count_params() {
let params = CollectCountParams {
product_id: Some(632910392),
collection_id: None,
};
let json = serde_json::to_value(¶ms).unwrap();
assert_eq!(json["product_id"], 632910392);
assert!(json.get("collection_id").is_none());
}
}