Add pre-commit config

This commit is contained in:
2023-09-24 22:57:57 +02:00
parent 7b68f443b8
commit 31ff4c1116
25 changed files with 485 additions and 844 deletions

View File

@@ -1,29 +1,37 @@
use std::collections::HashSet;
use axum::{Router, extract::{Query, Path}, Json, response::IntoResponse, routing::get, http::StatusCode};
use axum::{
extract::{Path, Query},
http::StatusCode,
response::IntoResponse,
routing::get,
Json, Router,
};
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::{
meilisearch::{get_meili_client, AuthorMeili},
prisma::{
author,
author_annotation::{self},
book, book_author, book_sequence, translator,
},
serializers::{
allowed_langs::AllowedLangs,
author::{Author, AuthorBook},
author_annotation::AuthorAnnotation,
pagination::{Page, PageWithParent, Pagination},
},
};
use super::{Database, common::get_random_item::get_random_item};
use super::{common::get_random_item::get_random_item, Database};
async fn get_authors(
db: Database,
pagination: Query<Pagination>
) -> impl IntoResponse {
let authors_count = db
.author()
.count(vec![])
.exec()
.await
.unwrap();
async fn get_authors(db: Database, pagination: Query<Pagination>) -> impl IntoResponse {
let authors_count = db.author().count(vec![]).exec().await.unwrap();
let authors = db
.author()
.find_many(vec![])
.with(
author::author_annotation::fetch()
)
.with(author::author_annotation::fetch())
.order_by(author::id::order(prisma_client_rust::Direction::Asc))
.skip((pagination.page - 1) * pagination.size)
.take(pagination.size)
@@ -34,41 +42,32 @@ async fn get_authors(
let page: Page<Author> = Page::new(
authors.iter().map(|item| item.clone().into()).collect(),
authors_count,
&pagination
&pagination,
);
Json(page)
}
async fn get_random_author(
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 author_id = {
let client = get_meili_client();
let authors_index = client.index("authors");
let filter = format!(
"author_langs IN [{}]",
allowed_langs.join(", ")
);
let filter = format!("author_langs IN [{}]", allowed_langs.join(", "));
get_random_item::<AuthorMeili>(
authors_index,
filter
).await
get_random_item::<AuthorMeili>(authors_index, filter).await
};
let author = db
.author()
.find_unique(
author::id::equals(author_id)
)
.with(
author::author_annotation::fetch()
)
.find_unique(author::id::equals(author_id))
.with(author::author_annotation::fetch())
.exec()
.await
.unwrap()
@@ -77,19 +76,11 @@ async fn get_random_author(
Json::<Author>(author.into())
}
async fn get_author(
db: Database,
Path(author_id): Path<i32>
) -> impl IntoResponse {
async fn get_author(db: Database, Path(author_id): Path<i32>) -> impl IntoResponse {
let author = db
.author()
.find_unique(
author::id::equals(author_id)
)
.with(
author::author_annotation::fetch()
)
.find_unique(author::id::equals(author_id))
.with(author::author_annotation::fetch())
.exec()
.await
.unwrap();
@@ -100,16 +91,10 @@ async fn get_author(
}
}
async fn get_author_annotation(
db: Database,
Path(author_id): Path<i32>,
) -> impl IntoResponse {
async fn get_author_annotation(db: Database, Path(author_id): Path<i32>) -> impl IntoResponse {
let author_annotation = db
.author_annotation()
.find_unique(
author_annotation::author_id::equals(author_id)
)
.find_unique(author_annotation::author_id::equals(author_id))
.exec()
.await
.unwrap();
@@ -120,21 +105,18 @@ async fn get_author_annotation(
}
}
async fn get_author_books(
db: Database,
Path(author_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 author = db
.author()
.find_unique(
author::id::equals(author_id),
)
.with(
author::author_annotation::fetch()
)
.find_unique(author::id::equals(author_id))
.with(author::author_annotation::fetch())
.exec()
.await
.unwrap();
@@ -146,43 +128,22 @@ async fn get_author_books(
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())
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_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::translations::fetch(vec![])
.with(
translator::author::fetch()
.with(
author::author_annotation::fetch()
)
)
)
.with(
book::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(translator::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.order_by(book::id::order(prisma_client_rust::Direction::Asc))
.skip((pagination.page - 1) * pagination.size)
.take(pagination.size)
@@ -194,26 +155,25 @@ async fn get_author_books(
author.into(),
books.iter().map(|item| item.clone().into()).collect(),
books_count,
&pagination
&pagination,
);
Json(page).into_response()
}
async fn get_author_books_available_types(
db: Database,
Path(author_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_authors::some(vec![
book_author::author_id::equals(author_id)
]),
book::lang::in_vec(allowed_langs)
book::book_authors::some(vec![book_author::author_id::equals(author_id)]),
book::lang::in_vec(allowed_langs),
])
.exec()
.await
@@ -234,27 +194,29 @@ async fn get_author_books_available_types(
Json::<Vec<String>>(file_types.into_iter().collect())
}
async fn search_authors(
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 authors_index = client.index("authors");
let filter = format!(
"author_langs IN [{}]",
allowed_langs.join(", ")
);
let filter = format!("author_langs IN [{}]", allowed_langs.join(", "));
let result = authors_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::<AuthorMeili>()
.await
@@ -265,12 +227,8 @@ async fn search_authors(
let mut authors = db
.author()
.find_many(vec![
author::id::in_vec(author_ids.clone())
])
.with(
author::author_annotation::fetch()
)
.find_many(vec![author::id::in_vec(author_ids.clone())])
.with(author::author_annotation::fetch())
.order_by(author::id::order(prisma_client_rust::Direction::Asc))
.exec()
.await
@@ -286,13 +244,12 @@ async fn search_authors(
let page: Page<Author> = Page::new(
authors.iter().map(|item| item.clone().into()).collect(),
total.try_into().unwrap(),
&pagination
&pagination,
);
Json(page)
}
pub async fn get_authors_router() -> Router {
Router::new()
.route("/", get(get_authors))
@@ -300,6 +257,9 @@ pub async fn get_authors_router() -> Router {
.route("/:author_id", get(get_author))
.route("/:author_id/annotation", get(get_author_annotation))
.route("/:author_id/books", get(get_author_books))
.route("/:author_id/available_types", get(get_author_books_available_types))
.route(
"/:author_id/available_types",
get(get_author_books_available_types),
)
.route("/search/:query", get(search_authors))
}

View File

@@ -1,58 +1,52 @@
use axum::{Router, routing::get, extract::{Query, Path}, Json, response::IntoResponse, http::StatusCode};
use axum::{
extract::{Path, Query},
http::StatusCode,
response::IntoResponse,
routing::get,
Json, Router,
};
use prisma_client_rust::Direction;
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, common::get_random_item::get_random_item};
use crate::{
meilisearch::{get_meili_client, BookMeili},
prisma::{
author,
book::{self},
book_annotation, book_author, book_genre, book_sequence, genre, translator,
},
serializers::{
allowed_langs::AllowedLangs,
book::{BaseBook, Book, BookFilter, DetailBook, RandomBookFilter, RemoteBook},
book_annotation::BookAnnotation,
pagination::{Page, Pagination},
},
};
use super::{common::get_random_item::get_random_item, Database};
pub async fn get_books(
db: Database,
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<BookFilter>,
pagination: Query<Pagination>
pagination: Query<Pagination>,
) -> impl IntoResponse {
let filter = book_filter.get_filter_vec();
let books_count = db
.book()
.count(filter.clone())
.exec()
.await
.unwrap();
let books_count = db.book().count(filter.clone()).exec().await.unwrap();
let books = db
.book()
.find_many(filter)
.with(
book::book_annotation::fetch()
)
.with(
book::source::fetch()
)
.with(book::book_annotation::fetch())
.with(book::source::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(
book::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(translator::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.order_by(book::id::order(Direction::Asc))
.skip((pagination.page - 1) * pagination.size)
.take(pagination.size)
@@ -63,7 +57,7 @@ pub async fn get_books(
let page: Page<RemoteBook> = Page::new(
books.iter().map(|item| item.clone().into()).collect(),
books_count,
&pagination
&pagination,
);
Json(page)
@@ -72,23 +66,16 @@ pub async fn get_books(
pub async fn get_base_books(
db: Database,
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<BookFilter>,
pagination: Query<Pagination>
pagination: Query<Pagination>,
) -> impl IntoResponse {
let filter = book_filter.get_filter_vec();
let books_count = db
.book()
.count(filter.clone())
.exec()
.await
.unwrap();
let books_count = db.book().count(filter.clone()).exec().await.unwrap();
let books = db
.book()
.find_many(filter)
.with(
book::source::fetch()
)
.with(book::source::fetch())
.order_by(book::id::order(Direction::Asc))
.skip((pagination.page - 1) * pagination.size)
.take(pagination.size)
@@ -99,7 +86,7 @@ pub async fn get_base_books(
let page: Page<BaseBook> = Page::new(
books.iter().map(|item| item.clone().into()).collect(),
books_count,
&pagination
&pagination,
);
Json(page)
@@ -115,10 +102,7 @@ pub async fn get_random_book(
let authors_index = client.index("books");
let filter = {
let langs_filter = format!(
"lang IN [{}]",
book_filter.allowed_langs.join(", ")
);
let langs_filter = format!("lang IN [{}]", book_filter.allowed_langs.join(", "));
let genre_filter = match book_filter.genre {
Some(v) => format!(" AND genres = {v}"),
None => "".to_string(),
@@ -127,53 +111,26 @@ pub async fn get_random_book(
format!("{langs_filter}{genre_filter}")
};
get_random_item::<BookMeili>(
authors_index,
filter
).await
get_random_item::<BookMeili>(authors_index, filter).await
};
let book = db
.book()
.find_unique(book::id::equals(book_id))
.with(
book::book_annotation::fetch()
)
.with(
book::source::fetch()
)
.with(book::book_annotation::fetch())
.with(book::source::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(
book::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(translator::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.with(
book::book_genres::fetch(vec![])
.with(
book_genre::genre::fetch()
.with(
genre::source::fetch()
)
)
.with(book_genre::genre::fetch().with(genre::source::fetch())),
)
.exec()
.await
@@ -190,44 +147,20 @@ pub async fn get_remote_book(
let book = db
.book()
.find_unique(book::source_id_remote_id(source_id, remote_id))
.with(
book::book_annotation::fetch()
)
.with(
book::source::fetch()
)
.with(book::book_annotation::fetch())
.with(book::source::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(
book::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(translator::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.with(
book::book_genres::fetch(vec![])
.with(
book_genre::genre::fetch()
.with(
genre::source::fetch()
)
)
.with(book_genre::genre::fetch().with(genre::source::fetch())),
)
.exec()
.await
@@ -242,23 +175,26 @@ pub async fn get_remote_book(
pub async fn search_books(
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 book_index = client.index("books");
let filter = format!(
"lang IN [{}]",
allowed_langs.join(", ")
);
let filter = format!("lang IN [{}]", allowed_langs.join(", "));
let result = book_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::<BookMeili>()
.await
@@ -270,36 +206,17 @@ pub async fn search_books(
let mut books = db
.book()
.find_many(vec![book::id::in_vec(book_ids.clone())])
.with(
book::book_annotation::fetch()
)
.with(
book::source::fetch()
)
.with(book::book_annotation::fetch())
.with(book::source::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(
book::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(translator::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.exec()
.await
.unwrap();
@@ -314,57 +231,30 @@ pub async fn search_books(
let page: Page<Book> = Page::new(
books.iter().map(|item| item.clone().into()).collect(),
total.try_into().unwrap(),
&pagination
&pagination,
);
Json(page)
}
pub async fn get_book(
db: Database,
Path(book_id): Path<i32>,
) -> impl IntoResponse {
pub async fn get_book(db: Database, Path(book_id): Path<i32>) -> impl IntoResponse {
let book = db
.book()
.find_unique(book::id::equals(book_id))
.with(
book::book_annotation::fetch()
)
.with(
book::source::fetch()
)
.with(book::book_annotation::fetch())
.with(book::source::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(
book::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(translator::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.with(
book::book_genres::fetch(vec![])
.with(
book_genre::genre::fetch()
.with(
genre::source::fetch()
)
)
.with(book_genre::genre::fetch().with(genre::source::fetch())),
)
.exec()
.await
@@ -376,15 +266,10 @@ pub async fn get_book(
}
}
pub async fn get_book_annotation(
db: Database,
Path(book_id): Path<i32>,
) -> impl IntoResponse {
pub async fn get_book_annotation(db: Database, Path(book_id): Path<i32>) -> impl IntoResponse {
let book_annotation = db
.book_annotation()
.find_unique(
book_annotation::book_id::equals(book_id)
)
.find_unique(book_annotation::book_id::equals(book_id))
.exec()
.await
.unwrap();

View File

@@ -4,10 +4,10 @@ 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 {
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)
@@ -15,7 +15,7 @@ where T: DeserializeOwned + GetId + 'static {
.await
.unwrap();
let offset: usize = rand::thread_rng().gen_range(0..result.estimated_total_hits.unwrap().try_into().unwrap());
let offset: usize = rand::thread_rng().gen_range(0..result.estimated_total_hits.unwrap());
let result = index
.search()

View File

@@ -1,40 +1,36 @@
use std::collections::HashSet;
use axum::{Router, routing::get, extract::Query, Json, response::IntoResponse};
use axum::{extract::Query, response::IntoResponse, routing::get, Json, Router};
use prisma_client_rust::Direction;
use crate::{serializers::{pagination::{Pagination, Page}, genre::{Genre, GenreFilter}}, prisma::genre};
use crate::{
prisma::genre,
serializers::{
genre::{Genre, GenreFilter},
pagination::{Page, Pagination},
},
};
use super::Database;
pub async fn get_genres(
db: Database,
pagination: Query<Pagination>,
Query(GenreFilter { meta }): Query<GenreFilter>
Query(GenreFilter { meta }): Query<GenreFilter>,
) -> impl IntoResponse {
let filter = {
match meta {
Some(meta) => vec![
genre::meta::equals(meta)
],
Some(meta) => vec![genre::meta::equals(meta)],
None => vec![],
}
};
let genres_count = db
.genre()
.count(filter.clone())
.exec()
.await
.unwrap();
let genres_count = db.genre().count(filter.clone()).exec().await.unwrap();
let genres = db
.genre()
.find_many(filter)
.with(
genre::source::fetch()
)
.with(genre::source::fetch())
.order_by(genre::id::order(Direction::Asc))
.skip((pagination.page - 1) * pagination.size)
.take(pagination.size)
@@ -45,16 +41,13 @@ pub async fn get_genres(
let page: Page<Genre> = Page::new(
genres.iter().map(|item| item.clone().into()).collect(),
genres_count,
&pagination
&pagination,
);
Json(page)
}
pub async fn get_genre_metas(
db: Database
) -> impl IntoResponse {
pub async fn get_genre_metas(db: Database) -> impl IntoResponse {
let genres = db
.genre()
.find_many(vec![])
@@ -75,7 +68,6 @@ pub async fn get_genre_metas(
Json::<Vec<String>>(metas)
}
pub async fn get_genres_router() -> Router {
Router::new()
.route("/", get(get_genres))

View File

@@ -1,28 +1,36 @@
use std::sync::Arc;
use axum::{Router, routing::get, middleware::{self, Next}, Extension, http::{Request, StatusCode, self}, response::Response};
use axum::{
http::{self, Request, StatusCode},
middleware::{self, Next},
response::Response,
routing::get,
Extension, Router,
};
use axum_prometheus::PrometheusMetricLayer;
use tower_http::trace::{TraceLayer, self};
use tower_http::trace::{self, TraceLayer};
use tracing::Level;
use crate::{config::CONFIG, db::get_prisma_client, prisma::PrismaClient};
use self::{authors::get_authors_router, genres::get_genres_router, books::get_books_router, sequences::get_sequences_router};
use self::translators::get_translators_router;
use self::{
authors::get_authors_router, books::get_books_router, genres::get_genres_router,
sequences::get_sequences_router,
};
pub mod authors;
pub mod books;
pub mod common;
pub mod genres;
pub mod sequences;
pub mod translators;
pub mod common;
pub type Database = Extension<Arc<PrismaClient>>;
async fn auth<B>(req: Request<B>, next: Next<B>) -> Result<Response, StatusCode> {
let auth_header = req.headers()
let auth_header = req
.headers()
.get(http::header::AUTHORIZATION)
.and_then(|header| header.to_str().ok());
@@ -39,35 +47,30 @@ async fn auth<B>(req: Request<B>, next: Next<B>) -> Result<Response, StatusCode>
Ok(next.run(req).await)
}
pub async fn get_router() -> Router {
let client = Arc::new(get_prisma_client().await);
let (prometheus_layer, metric_handle) = PrometheusMetricLayer::pair();
let app_router = Router::new()
.nest("/api/v1/authors", get_authors_router().await)
.nest("/api/v1/translators", get_translators_router().await)
.nest("/api/v1/genres", get_genres_router().await)
.nest("/api/v1/books", get_books_router().await)
.nest("/api/v1/sequences", get_sequences_router().await)
.layer(middleware::from_fn(auth))
.layer(Extension(client))
.layer(prometheus_layer);
let metric_router = Router::new()
.route("/metrics", get(|| async move { metric_handle.render() }));
let metric_router =
Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
Router::new()
.nest("/", app_router)
.nest("/", metric_router)
.layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new()
.level(Level::INFO))
.on_response(trace::DefaultOnResponse::new()
.level(Level::INFO)),
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
)
}

View File

@@ -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))
}

View File

@@ -1,26 +1,42 @@
use std::collections::HashSet;
use axum::{Router, routing::get, extract::{Path, Query}, response::IntoResponse, Json, http::StatusCode};
use axum::{
extract::{Path, Query},
http::StatusCode,
response::IntoResponse,
routing::get,
Json, Router,
};
use crate::{serializers::{pagination::{Pagination, Page, PageWithParent}, author::Author, translator::TranslatorBook, allowed_langs::AllowedLangs}, meilisearch::{get_meili_client, AuthorMeili}, prisma::{author, book::{self}, translator, book_author, book_sequence}};
use crate::{
meilisearch::{get_meili_client, AuthorMeili},
prisma::{
author,
book::{self},
book_author, book_sequence, translator,
},
serializers::{
allowed_langs::AllowedLangs,
author::Author,
pagination::{Page, PageWithParent, Pagination},
translator::TranslatorBook,
},
};
use super::Database;
async fn get_translated_books(
db: Database,
Path(translator_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 translator = db
.author()
.find_unique(
author::id::equals(translator_id)
)
.with(
author::author_annotation::fetch()
)
.find_unique(author::id::equals(translator_id))
.with(author::author_annotation::fetch())
.exec()
.await
.unwrap();
@@ -32,43 +48,22 @@ async fn get_translated_books(
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())
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_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::book_sequences::fetch(vec![])
.with(
book_sequence::sequence::fetch()
)
.with(book_author::author::fetch().with(author::author_annotation::fetch())),
)
.with(book::book_sequences::fetch(vec![]).with(book_sequence::sequence::fetch()))
.order_by(book::id::order(prisma_client_rust::Direction::Asc))
.skip((pagination.page - 1) * pagination.size)
.take(pagination.size)
@@ -80,26 +75,25 @@ async fn get_translated_books(
translator.into(),
books.iter().map(|item| item.clone().into()).collect(),
books_count,
&pagination
&pagination,
);
Json(page).into_response()
}
async fn get_translated_books_available_types(
db: Database,
Path(translator_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::translations::some(vec![
translator::author_id::equals(translator_id)
]),
book::lang::in_vec(allowed_langs)
book::translations::some(vec![translator::author_id::equals(translator_id)]),
book::lang::in_vec(allowed_langs),
])
.exec()
.await
@@ -120,27 +114,29 @@ async fn get_translated_books_available_types(
Json::<Vec<String>>(file_types.into_iter().collect())
}
async fn search_translators(
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 authors_index = client.index("authors");
let filter = format!(
"translator_langs IN [{}]",
allowed_langs.join(", ")
);
let filter = format!("translator_langs IN [{}]", allowed_langs.join(", "));
let result = authors_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::<AuthorMeili>()
.await
@@ -151,12 +147,8 @@ async fn search_translators(
let mut translators = db
.author()
.find_many(vec![
author::id::in_vec(translator_ids.clone())
])
.with(
author::author_annotation::fetch()
)
.find_many(vec![author::id::in_vec(translator_ids.clone())])
.with(author::author_annotation::fetch())
.order_by(author::id::order(prisma_client_rust::Direction::Asc))
.exec()
.await
@@ -172,16 +164,18 @@ async fn search_translators(
let page: Page<Author> = Page::new(
translators.iter().map(|item| item.clone().into()).collect(),
total.try_into().unwrap(),
&pagination
&pagination,
);
Json(page)
}
pub async fn get_translators_router() -> Router {
Router::new()
.route("/:translator_id/books", get(get_translated_books))
.route("/:translator_id/available_types", get(get_translated_books_available_types))
.route(
"/:translator_id/available_types",
get(get_translated_books_available_types),
)
.route("/search/:query", get(search_translators))
}