Skip to content

Commit

Permalink
fix: fix collection children api
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 26, 2023
1 parent 5bd4bca commit fbadda0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "writing"
version = "1.3.13"
version = "1.3.14"
edition = "2021"
rust-version = "1.64"
description = ""
Expand Down
144 changes: 67 additions & 77 deletions src/api/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,23 @@ pub async fn list_children(
});

let mut res: Vec<CollectionChildrenOutput> = 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 {
Expand All @@ -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
Expand All @@ -1026,56 +1034,44 @@ 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,
language,
)
.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;
Expand All @@ -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;
}
_ => {}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/db/model_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit fbadda0

Please sign in to comment.