From fbadda0e7d30a9f8a7902c32a3dd10179e84d8be Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Sun, 26 Nov 2023 08:50:13 +0800 Subject: [PATCH] fix: fix collection children api --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/api/collection.rs | 144 ++++++++++++++++++--------------------- src/db/model_creation.rs | 7 +- 4 files changed, 74 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9eecae1..7e7fca9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3097,7 +3097,7 @@ dependencies = [ [[package]] name = "writing" -version = "1.3.13" +version = "1.3.14" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index a75163a..5db39a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "writing" -version = "1.3.13" +version = "1.3.14" edition = "2021" rust-version = "1.64" description = "" diff --git a/src/api/collection.rs b/src/api/collection.rs index 182262e..19c3229 100644 --- a/src/api/collection.rs +++ b/src/api/collection.rs @@ -977,6 +977,23 @@ pub async fn list_children( }); let mut res: Vec = Vec::with_capacity(children.len()); + let collection_fields = vec![ + "gid".to_string(), + "status".to_string(), + "rating".to_string(), + "info".to_string(), + "updated_at".to_string(), + "price".to_string(), + ]; + let publication_fields = vec![ + "language".to_string(), + "version".to_string(), + "status".to_string(), + "title".to_string(), + "summary".to_string(), + "cover".to_string(), + "updated_at".to_string(), + ]; for child in children { let mut output = CollectionChildrenOutput { @@ -992,16 +1009,7 @@ pub async fn list_children( 2 => { let mut doc = db::Collection::with_pk(child.cid); if doc - .get_one( - &app.scylla, - vec![ - "gid".to_string(), - "status".to_string(), - "rating".to_string(), - "info".to_string(), - ], - ctx.language, - ) + .get_one(&app.scylla, collection_fields.clone(), ctx.language) .await .is_ok() && doc.status >= status @@ -1026,7 +1034,7 @@ pub async fn list_children( output.rating = icreation.rating; output.price = icreation.price; - match db::PublicationIndex::get_implicit_published( + if let Ok(ipub) = db::PublicationIndex::get_implicit_published( &app.scylla, child.cid, db::ZERO_ID, @@ -1034,48 +1042,36 @@ pub async fn list_children( ) .await { - Ok(ipub) => { - let mut doc = db::Publication::with_pk( - ipub.gid, - ipub.cid, - ipub.language, - ipub.version, - ); - doc.get_one( - &app.scylla, - vec![ - "status".to_string(), - "title".to_string(), - "summary".to_string(), - "updated_at".to_string(), - ], - ) - .await?; - output.gid = to.with(doc.gid); - output.status = 2; - output.updated_at = doc.updated_at; - output.language = to.with(doc.language); - output.version = doc.version; - output.title = doc.title; - output.summary = doc.summary; - output.kind = 1; - } - _ if child.kind == 1 && icreation.gid == user_gid => { - // get for owner - let doc = db::Publication::get_implicit_one( - &app.scylla, - icreation.gid, - icreation.id, - language, - vec![ - "status".to_string(), - "title".to_string(), - "summary".to_string(), - "updated_at".to_string(), - ], - Some(status), - ) - .await?; + let mut doc = db::Publication::with_pk( + ipub.gid, + ipub.cid, + ipub.language, + ipub.version, + ); + doc.get_one(&app.scylla, publication_fields.clone()).await?; + output.gid = to.with(doc.gid); + output.status = 2; + output.updated_at = doc.updated_at; + output.language = to.with(doc.language); + output.version = doc.version; + output.title = doc.title; + output.summary = doc.summary; + output.cover = doc.cover; + output.kind = 1; + } + + if icreation.gid == user_gid { + // get for owner + if let Ok(doc) = db::Publication::get_implicit_one( + &app.scylla, + icreation.gid, + icreation.id, + language, + publication_fields.clone(), + Some(status), + ) + .await + { if doc.status >= status { output.gid = to.with(doc.gid); output.status = doc.status; @@ -1084,34 +1080,28 @@ pub async fn list_children( output.version = doc.version; output.title = doc.title; output.summary = doc.summary; + output.cover = doc.cover; output.kind = 1; } } - _ if child.kind == 0 && icreation.gid == user_gid => { - // get for owner - let mut doc = db::Creation::with_pk(icreation.gid, icreation.id); - doc.get_one( - &app.scylla, - vec![ - "status".to_string(), - "title".to_string(), - "summary".to_string(), - "updated_at".to_string(), - ], - ) - .await?; - if doc.status >= status { - output.gid = to.with(doc.gid); - output.status = doc.status; - output.updated_at = doc.updated_at; - output.language = to.with(doc.language); - output.version = doc.version; - output.title = doc.title; - output.summary = doc.summary; - output.kind = 0; - } + + let mut doc = db::Creation::with_pk(icreation.gid, icreation.id); + if doc + .get_one(&app.scylla, publication_fields.clone()) + .await + .is_ok() + && doc.status >= status + { + output.gid = to.with(doc.gid); + output.status = doc.status; + output.updated_at = doc.updated_at; + output.language = to.with(doc.language); + output.version = doc.version; + output.title = doc.title; + output.summary = doc.summary; + output.cover = doc.cover; + output.kind = 0; } - _ => {} } } } diff --git a/src/db/model_creation.rs b/src/db/model_creation.rs index 50b89d7..b8cbf88 100644 --- a/src/db/model_creation.rs +++ b/src/db/model_creation.rs @@ -904,9 +904,12 @@ mod tests { .await .unwrap(); assert_eq!(doc3.title.as_str(), "Hello World"); - assert_eq!(doc3.version, 0); + assert_eq!(doc3.version, 1); assert_eq!(doc3.language, Language::Eng); - assert_eq!(doc3._fields, vec!["gid", "title", "language"]); + assert_eq!( + doc3._fields, + vec!["gid", "title", "language", "version", "status"] + ); assert!(doc3._content.is_empty()); }