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