user-auth orm and meili changes
This commit is contained in:
@@ -1,14 +1,37 @@
|
||||
//use ::entity::entities::prelude::Book;
|
||||
//use ::entity::entities::{prelude::*, *};
|
||||
use ::entity::entities::{book::Entity as Book};
|
||||
use ::entity::entities::user::Entity as User;
|
||||
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::{book,book_author,book_person,book_place,book_subject,book_time,book_isbn,user};
|
||||
use sea_orm::*;
|
||||
//use ::entity::entities::prelude::Book;
|
||||
|
||||
pub struct Mutation;
|
||||
|
||||
impl Mutation {
|
||||
pub async fn create_user(
|
||||
db: &DbConn,
|
||||
form_data: user::Model,
|
||||
) -> Result<InsertResult<user::ActiveModel>, DbErr> {
|
||||
let record = user::ActiveModel {
|
||||
user_name: Set(form_data.user_name.to_owned()),
|
||||
password_hash: Set(form_data.password_hash.to_owned()),
|
||||
..Default::default()
|
||||
};
|
||||
User::insert(record).exec(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_user(db: &DbConn, id: i32) -> Result<DeleteResult, DbErr> {
|
||||
let user: user::ActiveModel = User::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::Custom("Cannot find user.".to_owned()))
|
||||
.map(Into::into)?;
|
||||
|
||||
user.delete(db).await
|
||||
}
|
||||
|
||||
pub async fn create_book(
|
||||
db: &DbConn,
|
||||
form_data: book::Model,
|
||||
@@ -26,6 +49,7 @@ impl Mutation {
|
||||
time_added: Set(form_data.time_added.to_owned()),
|
||||
rating: Set(form_data.rating.to_owned()),
|
||||
comments: Set(form_data.comments.to_owned()),
|
||||
user_id: Set(form_data.user_id.to_owned()),
|
||||
..Default::default()
|
||||
};
|
||||
Book::insert(record).exec(db).await
|
||||
@@ -134,6 +158,7 @@ impl Mutation {
|
||||
time_added: Set(form_data.time_added.to_owned()),
|
||||
rating: Set(form_data.rating.to_owned()),
|
||||
comments: Set(form_data.comments.to_owned()),
|
||||
user_id: Set(form_data.user_id.to_owned()),
|
||||
}.update(db).await
|
||||
}
|
||||
|
||||
|
||||
@@ -59,11 +59,13 @@ impl Query {
|
||||
/// If ok, returns (post models, num pages).
|
||||
pub async fn find_books_in_page(
|
||||
db: &DbConn,
|
||||
page: usize,
|
||||
posts_per_page: usize,
|
||||
) -> Result<(Vec<book::Model>, usize), DbErr> {
|
||||
page: u64,
|
||||
posts_per_page: u64,
|
||||
userid: i32,
|
||||
) -> Result<(Vec<book::Model>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let paginator = Book::find()
|
||||
.filter(book::Column::UserId.eq(userid))
|
||||
.order_by_asc(book::Column::Id)
|
||||
.paginate(db, posts_per_page);
|
||||
let num_pages = paginator.num_pages().await?;
|
||||
@@ -76,9 +78,10 @@ pub async fn find_books_plus_meta_in_page(
|
||||
db: &DbConn,
|
||||
page: usize,
|
||||
posts_per_page: usize,
|
||||
) -> Result<(Vec<BookAndMetaV2>, usize), DbErr> {
|
||||
userid: i32,
|
||||
) -> Result<(Vec<BookAndMetaV2>, u64), DbErr> {
|
||||
// Setup paginator
|
||||
let books = Self::find_books_in_page(db,page,posts_per_page).await?;
|
||||
let books = Self::find_books_in_page(db,page.try_into().unwrap(),posts_per_page.try_into().unwrap(),userid).await?;
|
||||
let book_ids: Vec<i32> = books.0.clone().into_iter().map(|b| b.id).collect();
|
||||
let mut resbooks: Vec<BookAndMetaV2> = Vec::with_capacity(book_ids.len());
|
||||
for book in books.0.iter() {
|
||||
|
||||
Reference in New Issue
Block a user