mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 07:05:36 +01:00
Add pre-commit config
This commit is contained in:
@@ -1,37 +1,44 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use axum::{Router, routing::get, extract::{Path, Query}, http::StatusCode, response::IntoResponse, Json};
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
|
||||
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, common::get_random_item::get_random_item};
|
||||
use crate::{
|
||||
meilisearch::{get_meili_client, SequenceMeili},
|
||||
prisma::{author, book, book_author, book_sequence, sequence, translator},
|
||||
serializers::{
|
||||
allowed_langs::AllowedLangs,
|
||||
pagination::{Page, PageWithParent, Pagination},
|
||||
sequence::{Sequence, SequenceBook},
|
||||
},
|
||||
};
|
||||
|
||||
use super::{common::get_random_item::get_random_item, Database};
|
||||
|
||||
async fn get_random_sequence(
|
||||
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 {
|
||||
let sequence_id = {
|
||||
let client = get_meili_client();
|
||||
|
||||
let authors_index = client.index("sequences");
|
||||
|
||||
let filter = format!(
|
||||
"langs IN [{}]",
|
||||
allowed_langs.join(", ")
|
||||
);
|
||||
let filter = format!("langs IN [{}]", allowed_langs.join(", "));
|
||||
|
||||
get_random_item::<SequenceMeili>(
|
||||
authors_index,
|
||||
filter
|
||||
).await
|
||||
get_random_item::<SequenceMeili>(authors_index, filter).await
|
||||
};
|
||||
|
||||
let sequence = db
|
||||
.sequence()
|
||||
.find_unique(
|
||||
sequence::id::equals(sequence_id)
|
||||
)
|
||||
.find_unique(sequence::id::equals(sequence_id))
|
||||
.exec()
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -43,23 +50,26 @@ async fn get_random_sequence(
|
||||
async fn search_sequence(
|
||||
db: Database,
|
||||
Path(query): Path<String>,
|
||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>,
|
||||
pagination: Query<Pagination>
|
||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<
|
||||
AllowedLangs,
|
||||
>,
|
||||
pagination: Query<Pagination>,
|
||||
) -> impl IntoResponse {
|
||||
let client = get_meili_client();
|
||||
|
||||
let sequence_index = client.index("sequences");
|
||||
|
||||
let filter = format!(
|
||||
"langs IN [{}]",
|
||||
allowed_langs.join(", ")
|
||||
);
|
||||
let filter = format!("langs IN [{}]", allowed_langs.join(", "));
|
||||
|
||||
let result = sequence_index
|
||||
.search()
|
||||
.with_query(&query)
|
||||
.with_filter(&filter)
|
||||
.with_offset(((pagination.page - 1) * pagination.size).try_into().unwrap())
|
||||
.with_offset(
|
||||
((pagination.page - 1) * pagination.size)
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
)
|
||||
.with_limit(pagination.size.try_into().unwrap())
|
||||
.execute::<SequenceMeili>()
|
||||
.await
|
||||
@@ -70,9 +80,7 @@ async fn search_sequence(
|
||||
|
||||
let mut sequences = db
|
||||
.sequence()
|
||||
.find_many(vec![
|
||||
sequence::id::in_vec(sequence_ids.clone())
|
||||
])
|
||||
.find_many(vec![sequence::id::in_vec(sequence_ids.clone())])
|
||||
.exec()
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -87,21 +95,16 @@ async fn search_sequence(
|
||||
let page: Page<Sequence> = Page::new(
|
||||
sequences.iter().map(|item| item.clone().into()).collect(),
|
||||
total.try_into().unwrap(),
|
||||
&pagination
|
||||
&pagination,
|
||||
);
|
||||
|
||||
Json(page)
|
||||
}
|
||||
|
||||
async fn get_sequence(
|
||||
db: Database,
|
||||
Path(sequence_id): Path<i32>
|
||||
) -> impl IntoResponse {
|
||||
async fn get_sequence(db: Database, Path(sequence_id): Path<i32>) -> impl IntoResponse {
|
||||
let sequence = db
|
||||
.sequence()
|
||||
.find_unique(
|
||||
sequence::id::equals(sequence_id)
|
||||
)
|
||||
.find_unique(sequence::id::equals(sequence_id))
|
||||
.exec()
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -115,16 +118,16 @@ async fn get_sequence(
|
||||
async fn get_sequence_available_types(
|
||||
db: Database,
|
||||
Path(sequence_id): Path<i32>,
|
||||
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 {
|
||||
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)
|
||||
book::book_sequences::some(vec![book_sequence::sequence_id::equals(sequence_id)]),
|
||||
book::lang::in_vec(allowed_langs),
|
||||
])
|
||||
.exec()
|
||||
.await
|
||||
@@ -148,14 +151,14 @@ async fn get_sequence_available_types(
|
||||
async fn get_sequence_books(
|
||||
db: Database,
|
||||
Path(sequence_id): Path<i32>,
|
||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<AllowedLangs>,
|
||||
pagination: Query<Pagination>
|
||||
axum_extra::extract::Query(AllowedLangs { allowed_langs }): axum_extra::extract::Query<
|
||||
AllowedLangs,
|
||||
>,
|
||||
pagination: Query<Pagination>,
|
||||
) -> impl IntoResponse {
|
||||
let sequence = db
|
||||
.sequence()
|
||||
.find_unique(
|
||||
sequence::id::equals(sequence_id)
|
||||
)
|
||||
.find_unique(sequence::id::equals(sequence_id))
|
||||
.exec()
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -167,45 +170,24 @@ async fn get_sequence_books(
|
||||
|
||||
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())
|
||||
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_count = db.book().count(books_filter.clone()).exec().await.unwrap();
|
||||
|
||||
let books = db
|
||||
.book()
|
||||
.find_many(books_filter)
|
||||
.with(
|
||||
book::source::fetch()
|
||||
)
|
||||
.with(
|
||||
book::book_annotation::fetch()
|
||||
)
|
||||
.with(book::source::fetch())
|
||||
.with(book::book_annotation::fetch())
|
||||
.with(
|
||||
book::book_authors::fetch(vec![])
|
||||
.with(
|
||||
book_author::author::fetch()
|
||||
.with(
|
||||
author::author_annotation::fetch()
|
||||
)
|
||||
)
|
||||
.with(book_author::author::fetch().with(author::author_annotation::fetch())),
|
||||
)
|
||||
.with(
|
||||
book::translations::fetch(vec![])
|
||||
.with(
|
||||
translator::author::fetch()
|
||||
.with(
|
||||
author::author_annotation::fetch()
|
||||
)
|
||||
)
|
||||
.with(translator::author::fetch().with(author::author_annotation::fetch())),
|
||||
)
|
||||
.order_by(book::id::order(prisma_client_rust::Direction::Asc))
|
||||
.skip((pagination.page - 1) * pagination.size)
|
||||
@@ -218,18 +200,20 @@ async fn get_sequence_books(
|
||||
sequence.into(),
|
||||
books.iter().map(|item| item.clone().into()).collect(),
|
||||
books_count,
|
||||
&pagination
|
||||
&pagination,
|
||||
);
|
||||
|
||||
Json(page).into_response()
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_sequences_router() -> Router {
|
||||
Router::new()
|
||||
.route("/random", get(get_random_sequence))
|
||||
.route("/search/:query", get(search_sequence))
|
||||
.route("/:sequence_id", get(get_sequence))
|
||||
.route("/:sequence_id/available_types", get(get_sequence_available_types))
|
||||
.route(
|
||||
"/:sequence_id/available_types",
|
||||
get(get_sequence_available_types),
|
||||
)
|
||||
.route("/:sequence_id/books", get(get_sequence_books))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user