v0.12-searchP1
This commit is contained in:
@@ -27,6 +27,18 @@ pub struct BookAndMeta {
|
||||
pub time: String,
|
||||
pub isbn: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BookAndMetaV2 {
|
||||
pub book: book::Model,
|
||||
pub authors: Vec<book_author::Model>,
|
||||
pub persons: Vec<book_person::Model>,
|
||||
pub places: Vec<book_place::Model>,
|
||||
pub subjects: Vec<book_subject::Model>,
|
||||
pub times: Vec<book_time::Model>,
|
||||
pub isbns: Vec<book_isbn::Model>,
|
||||
}
|
||||
//use ::entity::entities::prelude::Book;
|
||||
|
||||
pub struct Query;
|
||||
@@ -64,33 +76,33 @@ pub async fn find_books_plus_meta_in_page(
|
||||
db: &DbConn,
|
||||
page: usize,
|
||||
posts_per_page: usize,
|
||||
) -> Result<(Vec<BookAndMeta>, usize), DbErr> {
|
||||
) -> Result<(Vec<BookAndMetaV2>, usize), DbErr> {
|
||||
// Setup paginator
|
||||
let books = Self::find_books_in_page(db,page,posts_per_page).await?;
|
||||
let book_ids: Vec<i32> = books.0.clone().into_iter().map(|b| b.id).collect();
|
||||
/* let paginator1 = Book::find().filter(
|
||||
Condition::any()
|
||||
.add(book::Column::Id.is_in(book_ids))
|
||||
).find_with_related(Author).all(db).await?;*/
|
||||
let paginator2: Vec<BookAndMeta> = Book::find()
|
||||
.filter(Condition::any()
|
||||
.add(book::Column::Id.is_in(book_ids)))
|
||||
.join(JoinType::LeftJoin, book::Relation::BookAuthor.def())
|
||||
.join(JoinType::LeftJoin, book::Relation::BookPerson.def())
|
||||
.join(JoinType::LeftJoin, book::Relation::BookPlace.def())
|
||||
.join(JoinType::LeftJoin, book::Relation::BookSubject.def())
|
||||
.join(JoinType::LeftJoin, book::Relation::BookTime.def())
|
||||
.join(JoinType::LeftJoin, book::Relation::BookIsbn.def())
|
||||
.column_as(book_author::Column::AuthorName, "author_name")
|
||||
.column_as(book_person::Column::Person, "person")
|
||||
.column_as(book_place::Column::Place, "place")
|
||||
.column_as(book_subject::Column::Subject, "subject")
|
||||
.column_as(book_time::Column::Time, "time")
|
||||
.column_as(book_isbn::Column::Isbn, "isbn")
|
||||
.into_model::<BookAndMeta>()
|
||||
.all(db).await?;
|
||||
let mut resbooks: Vec<BookAndMetaV2> = Vec::with_capacity(book_ids.len());
|
||||
for book in books.0.iter() {
|
||||
let bauthors: Vec<book_author::Model> = book.find_related(book_author::Entity).all(db).await?;
|
||||
let bpersons: Vec<book_person::Model> = book.find_related(book_person::Entity).all(db).await?;
|
||||
let bplaces: Vec<book_place::Model> = book.find_related(book_place::Entity).all(db).await?;
|
||||
let bsubjects: Vec<book_subject::Model> = book.find_related(book_subject::Entity).all(db).await?;
|
||||
let btimes: Vec<book_time::Model> = book.find_related(book_time::Entity).all(db).await?;
|
||||
let bisbns: Vec<book_isbn::Model> = book.find_related(book_isbn::Entity).all(db).await?;
|
||||
|
||||
Ok((paginator2,books.1))
|
||||
let bookandmeta = BookAndMetaV2 {
|
||||
book: (book.clone()),
|
||||
authors: bauthors,
|
||||
persons: bpersons,
|
||||
places: bplaces,
|
||||
subjects: bsubjects,
|
||||
times: btimes,
|
||||
isbns: bisbns
|
||||
};
|
||||
resbooks.push(bookandmeta);
|
||||
|
||||
}
|
||||
|
||||
Ok((resbooks,books.1))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user