v0.07
This commit is contained in:
7
backend/orm/src/lib.rs
Normal file
7
backend/orm/src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod mutation;
|
||||
mod query;
|
||||
|
||||
pub use mutation::*;
|
||||
pub use query::*;
|
||||
|
||||
pub use sea_orm;
|
||||
55
backend/orm/src/mutation.rs
Normal file
55
backend/orm/src/mutation.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
//use ::entity::entities::prelude::Book;
|
||||
//use ::entity::entities::{prelude::*, *};
|
||||
use sea_orm::*;
|
||||
//use ::entity::entities::prelude::Book;
|
||||
|
||||
pub struct Mutation;
|
||||
|
||||
impl Mutation {
|
||||
/* pub async fn create_post(
|
||||
db: &DbConn,
|
||||
form_data: post::Model,
|
||||
) -> Result<post::ActiveModel, DbErr> {
|
||||
post::ActiveModel {
|
||||
title: Set(form_data.title.to_owned()),
|
||||
text: Set(form_data.text.to_owned()),
|
||||
..Default::default()
|
||||
}
|
||||
.save(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update_post_by_id(
|
||||
db: &DbConn,
|
||||
id: i32,
|
||||
form_data: post::Model,
|
||||
) -> Result<post::Model, DbErr> {
|
||||
let post: post::ActiveModel = Post::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find post.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
post::ActiveModel {
|
||||
id: post.id,
|
||||
title: Set(form_data.title.to_owned()),
|
||||
text: Set(form_data.text.to_owned()),
|
||||
}
|
||||
.update(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_post(db: &DbConn, id: i32) -> Result<DeleteResult, DbErr> {
|
||||
let post: post::ActiveModel = Post::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find post.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
post.delete(db).await
|
||||
}
|
||||
*/
|
||||
/* pub async fn delete_all_books(db: &DbConn) -> Result<DeleteResult, DbErr> {
|
||||
Book::delete_many().exec(db).await
|
||||
} */
|
||||
}
|
||||
30
backend/orm/src/query.rs
Normal file
30
backend/orm/src/query.rs
Normal 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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user