-
Notifications
You must be signed in to change notification settings - Fork 29
[WJ-1361] implement PageQueryService::select() with SelectedFields #2819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
75c8d35
db2932c
fe672e0
0d6dc16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ use crate::models::page_connection::{self, Entity as PageConnection}; | |
| use crate::models::page_parent::{self, Entity as PageParent}; | ||
| use crate::models::{page_revision, text}; | ||
| use crate::services::{PageService, ParentService}; | ||
| use crate::utils::get_category_name; | ||
| use sea_query::{Expr, Query}; | ||
|
|
||
| #[derive(Debug)] | ||
|
|
@@ -483,4 +484,107 @@ impl PageQueryService { | |
|
|
||
| Ok(FoundPages { pages: rows }) | ||
| } | ||
|
|
||
| /// Takes the output of `find()` and extracts only the fields | ||
| /// specified in `SelectedFields`, producing a `SelectedPages` | ||
| /// result with each page's values populated as typed fields. | ||
| pub fn select(found: FoundPages, fields: SelectedFields) -> Result<SelectedPages> { | ||
| info!( | ||
| "Selecting {} fields from {} pages", | ||
| fields.len(), | ||
| found.total(), | ||
| ); | ||
|
|
||
| let pages = found | ||
| .pages | ||
| .into_iter() | ||
| .map(|row| SelectedPageRow { | ||
| page_id: row.page_id, | ||
| site_id: fields | ||
| .contains(SelectedField::SiteId) | ||
| .then_some(row.site_id), | ||
| title: if fields.contains(SelectedField::Title) { | ||
| row.title | ||
| } else { | ||
| None | ||
| }, | ||
| alt_title: if fields.contains(SelectedField::AltTitle) { | ||
| row.alt_title | ||
| } else { | ||
| None | ||
| }, | ||
| slug: if fields.contains(SelectedField::PageSlug) | ||
| || fields.contains(SelectedField::FullSlug) | ||
| { | ||
| row.slug.clone() | ||
| } else { | ||
| None | ||
| }, | ||
| // Extract the category prefix from the slug (e.g. "scp" from "scp:scp-173"). | ||
| category: if fields.contains(SelectedField::Category) { | ||
| row.slug.as_deref().map(|s| get_category_name(s).to_owned()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is closer, though I was saying I think we should also modify the struct type (That is, this processing, e.g. |
||
| } else { | ||
| None | ||
| }, | ||
| created_at: if fields.contains(SelectedField::CreatedAt) { | ||
| row.created_at | ||
| } else { | ||
| None | ||
| }, | ||
| created_by: if fields.contains(SelectedField::CreatedBy) { | ||
| row.created_by | ||
| } else { | ||
| None | ||
| }, | ||
| updated_at: if fields.contains(SelectedField::UpdatedAt) { | ||
| row.updated_at | ||
| } else { | ||
| None | ||
| }, | ||
| updated_by: if fields.contains(SelectedField::UpdatedBy) { | ||
| row.updated_by | ||
| } else { | ||
| None | ||
| }, | ||
| tags: if fields.contains(SelectedField::Tags) { | ||
| row.tags.clone() | ||
| } else { | ||
| None | ||
| }, | ||
| // Hidden tags are those starting with '_'. | ||
| hidden_tags: if fields.contains(SelectedField::HiddenTags) { | ||
| row.tags.as_ref().map(|tags| { | ||
| tags.iter() | ||
| .filter(|t| t.starts_with('_')) | ||
| .cloned() | ||
| .collect() | ||
| }) | ||
| } else { | ||
| None | ||
| }, | ||
| score: if fields.contains(SelectedField::Score) { | ||
| row.score | ||
| } else { | ||
| None | ||
| }, | ||
| score_votes: None, // TODO: requires vote join | ||
| revisions: None, // TODO: requires revision count join | ||
| comments: None, // TODO: requires forum post count join | ||
| children: None, // TODO: requires page_parent count join | ||
| size: None, // TODO: requires text join | ||
| page_category_id: if fields.contains(SelectedField::PageCategoryId) { | ||
| row.page_category_id | ||
| } else { | ||
| None | ||
| }, | ||
| page_revision_id: if fields.contains(SelectedField::PageRevisionId) { | ||
| row.page_revision_id | ||
| } else { | ||
| None | ||
| }, | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(SelectedPages { pages }) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.