This commit is contained in:
2022-10-22 16:46:45 +05:30
parent 44f9a8b15f
commit 2143923ee8
20 changed files with 409 additions and 229 deletions

30
backend/orm/src/query.rs Normal file
View File

@@ -0,0 +1,30 @@
use ::entity::entities::book::Entity as Book;
use ::entity::entities::book;
//use ::entity::entities::{prelude::*, *};
use sea_orm::*;
//use ::entity::entities::prelude::Book;
pub struct Query;
impl Query {
pub async fn find_book_by_id(db: &DbConn, id: i32) -> Result<Option<book::Model>, DbErr> {
Book::find_by_id(id).one(db).await
}
/// If ok, returns (post models, num pages).
pub async fn find_posts_in_page(
db: &DbConn,
page: usize,
posts_per_page: usize,
) -> Result<(Vec<book::Model>, usize), DbErr> {
// Setup paginator
let paginator = Book::find()
.order_by_asc(book::Column::Id)
.paginate(db, posts_per_page);
let num_pages = paginator.num_pages().await?;
// Fetch paginated posts
paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
}
}