v0.08
This commit is contained in:
@@ -1,7 +1,32 @@
|
||||
use ::entity::entities::book::Entity as Book;
|
||||
use ::entity::entities::book;
|
||||
use ::entity::entities::{book::Entity as Book};
|
||||
//, book_author::Entity as Author, book_person::Entity as Person, book_place::Entity as Place, book_subject::Entity as Subject, book_time::Entity as Time, book_isbn::Entity as ISBN};
|
||||
use ::entity::entities::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn};
|
||||
//use ::entity::entities::{prelude::*, *};
|
||||
use sea_orm::*;
|
||||
//use sea_query::Expr;
|
||||
|
||||
#[derive(FromQueryResult)]
|
||||
pub struct BookAndMeta {
|
||||
pub id: i32,
|
||||
pub open_library_key: String,
|
||||
pub title: String,
|
||||
pub edition_count: i32,
|
||||
pub first_publish_year: i32,
|
||||
pub median_page_count: i32,
|
||||
pub goodread_id: String,
|
||||
pub description: String,
|
||||
pub cover: String,
|
||||
pub location: String,
|
||||
pub time_added: String,
|
||||
pub rating: i32,
|
||||
pub comments: String,
|
||||
pub author_name: String,
|
||||
pub person: String,
|
||||
pub place: String,
|
||||
pub subject: String,
|
||||
pub time: String,
|
||||
pub isbn: String,
|
||||
}
|
||||
//use ::entity::entities::prelude::Book;
|
||||
|
||||
pub struct Query;
|
||||
@@ -11,8 +36,16 @@ impl Query {
|
||||
Book::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
/* pub async fn find_bookplusmeta_by_id(db: &DbConn, id: i32) -> Result<Option<(book::Model, Vec<book_author::Model>, Vec<book_person::Model>)>, DbErr> {
|
||||
let book = Book::find_by_id(id).one(db).await?.unwrap();
|
||||
let authors = book.find_related(Author).all(db).await?;
|
||||
let persons = book.find_related(Person).all(db).await?;
|
||||
Ok(Some((book, authors, persons)))
|
||||
}
|
||||
*/
|
||||
|
||||
/// If ok, returns (post models, num pages).
|
||||
pub async fn find_posts_in_page(
|
||||
pub async fn find_books_in_page(
|
||||
db: &DbConn,
|
||||
page: usize,
|
||||
posts_per_page: usize,
|
||||
@@ -26,5 +59,34 @@ impl Query {
|
||||
// Fetch paginated posts
|
||||
paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
|
||||
}
|
||||
|
||||
pub async fn find_books_plus_meta_in_page(
|
||||
db: &DbConn,
|
||||
page: usize,
|
||||
posts_per_page: usize,
|
||||
) -> Result<(Vec<BookAndMeta>, 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 = Book::find()
|
||||
.filter(Condition::any()
|
||||
.add(book::Column::Id.is_in(book_ids)))
|
||||
.join(JoinType::LeftJoin, book_author::Relation::Book.def())
|
||||
.join(JoinType::LeftJoin, book_person::Relation::Book.def())
|
||||
.join(JoinType::LeftJoin, book_place::Relation::Book.def())
|
||||
.join(JoinType::LeftJoin, book_subject::Relation::Book.def())
|
||||
.join(JoinType::LeftJoin, book_time::Relation::Book.def())
|
||||
.join(JoinType::LeftJoin, book_isbn::Relation::Book.def())
|
||||
.column_as(book::Column::Id, "id")
|
||||
.into_model::<BookAndMeta>()
|
||||
.all(db).await?;
|
||||
|
||||
Ok((paginator2,books.1))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user