mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Fix
This commit is contained in:
@@ -11,6 +11,10 @@ pub fn get_meili_client() -> Client {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait GetId {
|
||||||
|
fn get_id(&self) -> i32;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct AuthorMeili {
|
pub struct AuthorMeili {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
@@ -22,6 +26,13 @@ pub struct AuthorMeili {
|
|||||||
pub books_count: i32
|
pub books_count: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetId for AuthorMeili {
|
||||||
|
fn get_id(&self) -> i32 {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct BookMeili {
|
pub struct BookMeili {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
@@ -30,6 +41,13 @@ pub struct BookMeili {
|
|||||||
pub genres: Vec<i32>
|
pub genres: Vec<i32>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetId for BookMeili {
|
||||||
|
fn get_id(&self) -> i32 {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct GenreMeili {
|
pub struct GenreMeili {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
@@ -39,6 +57,13 @@ pub struct GenreMeili {
|
|||||||
pub books_count: i32
|
pub books_count: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetId for GenreMeili {
|
||||||
|
fn get_id(&self) -> i32 {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct SequenceMeili {
|
pub struct SequenceMeili {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
@@ -46,3 +71,9 @@ pub struct SequenceMeili {
|
|||||||
pub langs: Vec<String>,
|
pub langs: Vec<String>,
|
||||||
pub books_count: i32
|
pub books_count: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetId for SequenceMeili {
|
||||||
|
fn get_id(&self) -> i32 {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
use axum::{Router, extract::{Query, Path}, Json, response::IntoResponse, routing::get, http::StatusCode};
|
use axum::{Router, extract::{Query, Path}, Json, response::IntoResponse, routing::get, http::StatusCode};
|
||||||
|
|
||||||
use rand::Rng;
|
|
||||||
|
|
||||||
use crate::{prisma::{author, author_annotation::{self}, book, book_author, translator, book_sequence}, serializers::{pagination::{Pagination, Page, PageWithParent}, author::{Author, AuthorBook}, author_annotation::AuthorAnnotation, allowed_langs::AllowedLangs}, meilisearch::{get_meili_client, AuthorMeili}};
|
use crate::{prisma::{author, author_annotation::{self}, book, book_author, translator, book_sequence}, serializers::{pagination::{Pagination, Page, PageWithParent}, author::{Author, AuthorBook}, author_annotation::AuthorAnnotation, allowed_langs::AllowedLangs}, meilisearch::{get_meili_client, AuthorMeili}};
|
||||||
|
|
||||||
use super::Database;
|
use super::{Database, common::get_random_item::get_random_item};
|
||||||
|
|
||||||
|
|
||||||
async fn get_authors(
|
async fn get_authors(
|
||||||
@@ -47,36 +45,20 @@ async fn get_random_author(
|
|||||||
db: Database,
|
db: Database,
|
||||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>
|
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let client = get_meili_client();
|
|
||||||
|
|
||||||
let authors_index = client.index("authors");
|
|
||||||
|
|
||||||
let filter = format!(
|
|
||||||
"author_langs IN [{}]",
|
|
||||||
allowed_langs.join(", ")
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = authors_index
|
|
||||||
.search()
|
|
||||||
.with_filter(&filter)
|
|
||||||
.execute::<AuthorMeili>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let author_id = {
|
let author_id = {
|
||||||
let offset: usize = rand::thread_rng().gen_range(0..result.estimated_total_hits.unwrap().try_into().unwrap());
|
let client = get_meili_client();
|
||||||
|
|
||||||
let result = authors_index
|
let authors_index = client.index("authors");
|
||||||
.search()
|
|
||||||
.with_limit(1)
|
|
||||||
.with_offset(offset)
|
|
||||||
.execute::<AuthorMeili>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let author = &result.hits.get(0).unwrap().result;
|
let filter = format!(
|
||||||
|
"author_langs IN [{}]",
|
||||||
|
allowed_langs.join(", ")
|
||||||
|
);
|
||||||
|
|
||||||
author.id
|
get_random_item::<AuthorMeili>(
|
||||||
|
authors_index,
|
||||||
|
filter
|
||||||
|
).await
|
||||||
};
|
};
|
||||||
|
|
||||||
let author = db
|
let author = db
|
||||||
@@ -162,28 +144,24 @@ async fn get_author_books(
|
|||||||
None => return StatusCode::NOT_FOUND.into_response(),
|
None => return StatusCode::NOT_FOUND.into_response(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let books_filter = vec![
|
||||||
|
book::is_deleted::equals(false),
|
||||||
|
book::book_authors::some(vec![
|
||||||
|
book_author::author_id::equals(author_id)
|
||||||
|
]),
|
||||||
|
book::lang::in_vec(allowed_langs.clone())
|
||||||
|
];
|
||||||
|
|
||||||
let books_count = db
|
let books_count = db
|
||||||
.book()
|
.book()
|
||||||
.count(vec![
|
.count(books_filter.clone())
|
||||||
book::is_deleted::equals(false),
|
|
||||||
book::book_authors::some(vec![
|
|
||||||
book_author::author_id::equals(author_id)
|
|
||||||
]),
|
|
||||||
book::lang::in_vec(allowed_langs.clone())
|
|
||||||
])
|
|
||||||
.exec()
|
.exec()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let books = db
|
let books = db
|
||||||
.book()
|
.book()
|
||||||
.find_many(vec![
|
.find_many(books_filter)
|
||||||
book::is_deleted::equals(false),
|
|
||||||
book::book_authors::some(vec![
|
|
||||||
book_author::author_id::equals(author_id)
|
|
||||||
]),
|
|
||||||
book::lang::in_vec(allowed_langs)
|
|
||||||
])
|
|
||||||
.with(
|
.with(
|
||||||
book::source::fetch()
|
book::source::fetch()
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
use axum::{Router, routing::get, extract::{Query, Path}, Json, response::IntoResponse, http::StatusCode};
|
use axum::{Router, routing::get, extract::{Query, Path}, Json, response::IntoResponse, http::StatusCode};
|
||||||
use prisma_client_rust::Direction;
|
use prisma_client_rust::Direction;
|
||||||
use rand::Rng;
|
|
||||||
|
|
||||||
use crate::{serializers::{book::{BookFilter, RemoteBook, BaseBook, DetailBook, RandomBookFilter, Book}, pagination::{Pagination, Page}, book_annotation::BookAnnotation, allowed_langs::AllowedLangs}, prisma::{book::{self}, book_author, author, translator, book_sequence, book_genre, book_annotation, genre}, meilisearch::{get_meili_client, BookMeili}};
|
use crate::{serializers::{book::{BookFilter, RemoteBook, BaseBook, DetailBook, RandomBookFilter, Book}, pagination::{Pagination, Page}, book_annotation::BookAnnotation, allowed_langs::AllowedLangs}, prisma::{book::{self}, book_author, author, translator, book_sequence, book_genre, book_annotation, genre}, meilisearch::{get_meili_client, BookMeili}};
|
||||||
|
|
||||||
use super::Database;
|
use super::{Database, common::get_random_item::get_random_item};
|
||||||
|
|
||||||
|
|
||||||
pub async fn get_books(
|
pub async fn get_books(
|
||||||
@@ -110,44 +109,28 @@ pub async fn get_random_book(
|
|||||||
db: Database,
|
db: Database,
|
||||||
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<RandomBookFilter>,
|
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<RandomBookFilter>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let client = get_meili_client();
|
let book_id = {
|
||||||
|
let client = get_meili_client();
|
||||||
|
|
||||||
let authors_index = client.index("books");
|
let authors_index = client.index("books");
|
||||||
|
|
||||||
let filter = {
|
let filter = {
|
||||||
let langs_filter = format!(
|
let langs_filter = format!(
|
||||||
"lang IN [{}]",
|
"lang IN [{}]",
|
||||||
book_filter.allowed_langs.join(", ")
|
book_filter.allowed_langs.join(", ")
|
||||||
);
|
);
|
||||||
let genre_filter = match book_filter.genre {
|
let genre_filter = match book_filter.genre {
|
||||||
Some(v) => format!(" AND genres = {v}"),
|
Some(v) => format!(" AND genres = {v}"),
|
||||||
None => "".to_string(),
|
None => "".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
format!("{langs_filter}{genre_filter}")
|
||||||
};
|
};
|
||||||
|
|
||||||
format!("{langs_filter}{genre_filter}")
|
get_random_item::<BookMeili>(
|
||||||
};
|
authors_index,
|
||||||
|
filter
|
||||||
let result = authors_index
|
).await
|
||||||
.search()
|
|
||||||
.with_filter(&filter)
|
|
||||||
.execute::<BookMeili>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let book_id = {
|
|
||||||
let offset: usize = rand::thread_rng().gen_range(0..result.estimated_total_hits.unwrap().try_into().unwrap());
|
|
||||||
|
|
||||||
let result = authors_index
|
|
||||||
.search()
|
|
||||||
.with_limit(1)
|
|
||||||
.with_offset(offset)
|
|
||||||
.execute::<BookMeili>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let book = &result.hits.get(0).unwrap().result;
|
|
||||||
|
|
||||||
book.id
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let book = db
|
let book = db
|
||||||
|
|||||||
31
src/views/common/get_random_item.rs
Normal file
31
src/views/common/get_random_item.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use meilisearch_sdk::indexes::Index;
|
||||||
|
use rand::Rng;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
|
use crate::meilisearch::GetId;
|
||||||
|
|
||||||
|
pub async fn get_random_item<'a, T>(
|
||||||
|
index: Index, filter: String,
|
||||||
|
) -> i32
|
||||||
|
where T: DeserializeOwned + GetId + 'static {
|
||||||
|
let result = index
|
||||||
|
.search()
|
||||||
|
.with_filter(&filter)
|
||||||
|
.execute::<T>()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let offset: usize = rand::thread_rng().gen_range(0..result.estimated_total_hits.unwrap().try_into().unwrap());
|
||||||
|
|
||||||
|
let result = index
|
||||||
|
.search()
|
||||||
|
.with_limit(1)
|
||||||
|
.with_offset(offset)
|
||||||
|
.execute::<T>()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let item = &result.hits.get(0).unwrap().result;
|
||||||
|
|
||||||
|
item.get_id()
|
||||||
|
}
|
||||||
1
src/views/common/mod.rs
Normal file
1
src/views/common/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod get_random_item;
|
||||||
@@ -15,6 +15,7 @@ pub mod books;
|
|||||||
pub mod genres;
|
pub mod genres;
|
||||||
pub mod sequences;
|
pub mod sequences;
|
||||||
pub mod translators;
|
pub mod translators;
|
||||||
|
pub mod common;
|
||||||
|
|
||||||
|
|
||||||
pub type Database = Extension<Arc<PrismaClient>>;
|
pub type Database = Extension<Arc<PrismaClient>>;
|
||||||
|
|||||||
@@ -1,47 +1,30 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use axum::{Router, routing::get, extract::{Path, Query}, http::StatusCode, response::IntoResponse, Json};
|
use axum::{Router, routing::get, extract::{Path, Query}, http::StatusCode, response::IntoResponse, Json};
|
||||||
use rand::Rng;
|
|
||||||
|
|
||||||
use crate::{prisma::{sequence, book_sequence, book, book_author, author, translator}, serializers::{sequence::{Sequence, SequenceBook}, allowed_langs::AllowedLangs, pagination::{PageWithParent, Pagination, Page}}, meilisearch::{get_meili_client, SequenceMeili}};
|
use crate::{prisma::{sequence, book_sequence, book, book_author, author, translator}, serializers::{sequence::{Sequence, SequenceBook}, allowed_langs::AllowedLangs, pagination::{PageWithParent, Pagination, Page}}, meilisearch::{get_meili_client, SequenceMeili}};
|
||||||
|
|
||||||
use super::Database;
|
use super::{Database, common::get_random_item::get_random_item};
|
||||||
|
|
||||||
|
|
||||||
async fn get_random_sequence(
|
async fn get_random_sequence(
|
||||||
db: Database,
|
db: Database,
|
||||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>
|
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let client = get_meili_client();
|
|
||||||
|
|
||||||
let authors_index = client.index("sequences");
|
|
||||||
|
|
||||||
let filter = format!(
|
|
||||||
"langs IN [{}]",
|
|
||||||
allowed_langs.join(", ")
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = authors_index
|
|
||||||
.search()
|
|
||||||
.with_filter(&filter)
|
|
||||||
.execute::<SequenceMeili>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let sequence_id = {
|
let sequence_id = {
|
||||||
let offset: usize = rand::thread_rng().gen_range(0..result.estimated_total_hits.unwrap().try_into().unwrap());
|
let client = get_meili_client();
|
||||||
|
|
||||||
let result = authors_index
|
let authors_index = client.index("sequences");
|
||||||
.search()
|
|
||||||
.with_limit(1)
|
|
||||||
.with_offset(offset)
|
|
||||||
.execute::<SequenceMeili>()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let sequence = &result.hits.get(0).unwrap().result;
|
let filter = format!(
|
||||||
|
"langs IN [{}]",
|
||||||
|
allowed_langs.join(", ")
|
||||||
|
);
|
||||||
|
|
||||||
sequence.id
|
get_random_item::<SequenceMeili>(
|
||||||
|
authors_index,
|
||||||
|
filter
|
||||||
|
).await
|
||||||
};
|
};
|
||||||
|
|
||||||
let sequence = db
|
let sequence = db
|
||||||
@@ -182,28 +165,24 @@ async fn get_sequence_books(
|
|||||||
None => return StatusCode::NOT_FOUND.into_response(),
|
None => return StatusCode::NOT_FOUND.into_response(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let books_filter = vec![
|
||||||
|
book::is_deleted::equals(false),
|
||||||
|
book::book_sequences::some(vec![
|
||||||
|
book_sequence::sequence_id::equals(sequence_id)
|
||||||
|
]),
|
||||||
|
book::lang::in_vec(allowed_langs.clone())
|
||||||
|
];
|
||||||
|
|
||||||
let books_count = db
|
let books_count = db
|
||||||
.book()
|
.book()
|
||||||
.count(vec![
|
.count(books_filter.clone())
|
||||||
book::is_deleted::equals(false),
|
|
||||||
book::book_sequences::some(vec![
|
|
||||||
book_sequence::sequence_id::equals(sequence_id)
|
|
||||||
]),
|
|
||||||
book::lang::in_vec(allowed_langs.clone())
|
|
||||||
])
|
|
||||||
.exec()
|
.exec()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let books = db
|
let books = db
|
||||||
.book()
|
.book()
|
||||||
.find_many(vec![
|
.find_many(books_filter)
|
||||||
book::is_deleted::equals(false),
|
|
||||||
book::book_sequences::some(vec![
|
|
||||||
book_sequence::sequence_id::equals(sequence_id)
|
|
||||||
]),
|
|
||||||
book::lang::in_vec(allowed_langs.clone())
|
|
||||||
])
|
|
||||||
.with(
|
.with(
|
||||||
book::source::fetch()
|
book::source::fetch()
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -30,28 +30,24 @@ async fn get_translated_books(
|
|||||||
None => return StatusCode::NOT_FOUND.into_response(),
|
None => return StatusCode::NOT_FOUND.into_response(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let books_filter = vec![
|
||||||
|
book::is_deleted::equals(false),
|
||||||
|
book::translations::some(vec![
|
||||||
|
translator::author_id::equals(translator_id)
|
||||||
|
]),
|
||||||
|
book::lang::in_vec(allowed_langs.clone())
|
||||||
|
];
|
||||||
|
|
||||||
let books_count = db
|
let books_count = db
|
||||||
.book()
|
.book()
|
||||||
.count(vec![
|
.count(books_filter.clone())
|
||||||
book::is_deleted::equals(false),
|
|
||||||
book::translations::some(vec![
|
|
||||||
translator::author_id::equals(translator_id)
|
|
||||||
]),
|
|
||||||
book::lang::in_vec(allowed_langs.clone())
|
|
||||||
])
|
|
||||||
.exec()
|
.exec()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let books = db
|
let books = db
|
||||||
.book()
|
.book()
|
||||||
.find_many(vec![
|
.find_many(books_filter)
|
||||||
book::is_deleted::equals(false),
|
|
||||||
book::translations::some(vec![
|
|
||||||
translator::author_id::equals(translator_id)
|
|
||||||
]),
|
|
||||||
book::lang::in_vec(allowed_langs)
|
|
||||||
])
|
|
||||||
.with(
|
.with(
|
||||||
book::source::fetch()
|
book::source::fetch()
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user