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)]
|
||||
pub struct AuthorMeili {
|
||||
pub id: i32,
|
||||
@@ -22,6 +26,13 @@ pub struct AuthorMeili {
|
||||
pub books_count: i32
|
||||
}
|
||||
|
||||
impl GetId for AuthorMeili {
|
||||
fn get_id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct BookMeili {
|
||||
pub id: i32,
|
||||
@@ -30,6 +41,13 @@ pub struct BookMeili {
|
||||
pub genres: Vec<i32>
|
||||
}
|
||||
|
||||
impl GetId for BookMeili {
|
||||
fn get_id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GenreMeili {
|
||||
pub id: i32,
|
||||
@@ -39,6 +57,13 @@ pub struct GenreMeili {
|
||||
pub books_count: i32
|
||||
}
|
||||
|
||||
impl GetId for GenreMeili {
|
||||
fn get_id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SequenceMeili {
|
||||
pub id: i32,
|
||||
@@ -46,3 +71,9 @@ pub struct SequenceMeili {
|
||||
pub langs: Vec<String>,
|
||||
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 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 super::Database;
|
||||
use super::{Database, common::get_random_item::get_random_item};
|
||||
|
||||
|
||||
async fn get_authors(
|
||||
@@ -47,6 +45,7 @@ async fn get_random_author(
|
||||
db: Database,
|
||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>
|
||||
) -> impl IntoResponse {
|
||||
let author_id = {
|
||||
let client = get_meili_client();
|
||||
|
||||
let authors_index = client.index("authors");
|
||||
@@ -56,27 +55,10 @@ async fn get_random_author(
|
||||
allowed_langs.join(", ")
|
||||
);
|
||||
|
||||
let result = authors_index
|
||||
.search()
|
||||
.with_filter(&filter)
|
||||
.execute::<AuthorMeili>()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let author_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::<AuthorMeili>()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let author = &result.hits.get(0).unwrap().result;
|
||||
|
||||
author.id
|
||||
get_random_item::<AuthorMeili>(
|
||||
authors_index,
|
||||
filter
|
||||
).await
|
||||
};
|
||||
|
||||
let author = db
|
||||
@@ -162,28 +144,24 @@ async fn get_author_books(
|
||||
None => return StatusCode::NOT_FOUND.into_response(),
|
||||
};
|
||||
|
||||
let books_count = db
|
||||
.book()
|
||||
.count(vec![
|
||||
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
|
||||
.book()
|
||||
.count(books_filter.clone())
|
||||
.exec()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let books = db
|
||||
.book()
|
||||
.find_many(vec![
|
||||
book::is_deleted::equals(false),
|
||||
book::book_authors::some(vec![
|
||||
book_author::author_id::equals(author_id)
|
||||
]),
|
||||
book::lang::in_vec(allowed_langs)
|
||||
])
|
||||
.find_many(books_filter)
|
||||
.with(
|
||||
book::source::fetch()
|
||||
)
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use axum::{Router, routing::get, extract::{Query, Path}, Json, response::IntoResponse, http::StatusCode};
|
||||
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 super::Database;
|
||||
use super::{Database, common::get_random_item::get_random_item};
|
||||
|
||||
|
||||
pub async fn get_books(
|
||||
@@ -110,6 +109,7 @@ pub async fn get_random_book(
|
||||
db: Database,
|
||||
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<RandomBookFilter>,
|
||||
) -> impl IntoResponse {
|
||||
let book_id = {
|
||||
let client = get_meili_client();
|
||||
|
||||
let authors_index = client.index("books");
|
||||
@@ -127,27 +127,10 @@ pub async fn get_random_book(
|
||||
format!("{langs_filter}{genre_filter}")
|
||||
};
|
||||
|
||||
let result = authors_index
|
||||
.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
|
||||
get_random_item::<BookMeili>(
|
||||
authors_index,
|
||||
filter
|
||||
).await
|
||||
};
|
||||
|
||||
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 sequences;
|
||||
pub mod translators;
|
||||
pub mod common;
|
||||
|
||||
|
||||
pub type Database = Extension<Arc<PrismaClient>>;
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
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 super::Database;
|
||||
use super::{Database, common::get_random_item::get_random_item};
|
||||
|
||||
|
||||
async fn get_random_sequence(
|
||||
db: Database,
|
||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>
|
||||
) -> impl IntoResponse {
|
||||
let sequence_id = {
|
||||
let client = get_meili_client();
|
||||
|
||||
let authors_index = client.index("sequences");
|
||||
@@ -21,27 +21,10 @@ async fn get_random_sequence(
|
||||
allowed_langs.join(", ")
|
||||
);
|
||||
|
||||
let result = authors_index
|
||||
.search()
|
||||
.with_filter(&filter)
|
||||
.execute::<SequenceMeili>()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let sequence_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::<SequenceMeili>()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let sequence = &result.hits.get(0).unwrap().result;
|
||||
|
||||
sequence.id
|
||||
get_random_item::<SequenceMeili>(
|
||||
authors_index,
|
||||
filter
|
||||
).await
|
||||
};
|
||||
|
||||
let sequence = db
|
||||
@@ -182,28 +165,24 @@ async fn get_sequence_books(
|
||||
None => return StatusCode::NOT_FOUND.into_response(),
|
||||
};
|
||||
|
||||
let books_count = db
|
||||
.book()
|
||||
.count(vec![
|
||||
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
|
||||
.book()
|
||||
.count(books_filter.clone())
|
||||
.exec()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let books = db
|
||||
.book()
|
||||
.find_many(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())
|
||||
])
|
||||
.find_many(books_filter)
|
||||
.with(
|
||||
book::source::fetch()
|
||||
)
|
||||
|
||||
@@ -30,28 +30,24 @@ async fn get_translated_books(
|
||||
None => return StatusCode::NOT_FOUND.into_response(),
|
||||
};
|
||||
|
||||
let books_count = db
|
||||
.book()
|
||||
.count(vec![
|
||||
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
|
||||
.book()
|
||||
.count(books_filter.clone())
|
||||
.exec()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let books = db
|
||||
.book()
|
||||
.find_many(vec![
|
||||
book::is_deleted::equals(false),
|
||||
book::translations::some(vec![
|
||||
translator::author_id::equals(translator_id)
|
||||
]),
|
||||
book::lang::in_vec(allowed_langs)
|
||||
])
|
||||
.find_many(books_filter)
|
||||
.with(
|
||||
book::source::fetch()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user