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

7
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
- id: cargo-check
- id: clippy

View File

@@ -16,7 +16,7 @@ pub struct Config {
pub meili_host: String,
pub meili_master_key: String,
pub sentry_dsn: String
pub sentry_dsn: String,
}
impl Config {
@@ -33,11 +33,9 @@ impl Config {
meili_host: get_env("MEILI_HOST"),
meili_master_key: get_env("MEILI_MASTER_KEY"),
sentry_dsn: get_env("SENTRY_DSN")
sentry_dsn: get_env("SENTRY_DSN"),
}
}
}
pub static CONFIG: Lazy<Config> = Lazy::new(|| {
Config::load()
});
pub static CONFIG: Lazy<Config> = Lazy::new(Config::load);

View File

@@ -1,5 +1,4 @@
use crate::{prisma::PrismaClient, config::CONFIG};
use crate::{config::CONFIG, prisma::PrismaClient};
pub async fn get_prisma_client() -> PrismaClient {
let database_url: String = format!(

View File

@@ -1,17 +1,16 @@
pub mod config;
pub mod views;
pub mod prisma;
pub mod db;
pub mod serializers;
pub mod meilisearch;
pub mod prisma;
pub mod serializers;
pub mod views;
use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions};
use std::{net::SocketAddr, str::FromStr};
use sentry::{ClientOptions, types::Dsn, integrations::debug_images::DebugImagesIntegration};
use tracing::info;
use crate::views::get_router;
#[tokio::main]
async fn main() {
let options = ClientOptions {

View File

@@ -3,12 +3,8 @@ use serde::Deserialize;
use crate::config::CONFIG;
pub fn get_meili_client() -> Client {
Client::new(
&CONFIG.meili_host,
Some(CONFIG.meili_master_key.clone())
)
Client::new(&CONFIG.meili_host, Some(CONFIG.meili_master_key.clone()))
}
pub trait GetId {
@@ -23,7 +19,7 @@ pub struct AuthorMeili {
pub middle_name: String,
pub author_langs: Vec<String>,
pub translator_langs: Vec<String>,
pub books_count: i32
pub books_count: i32,
}
impl GetId for AuthorMeili {
@@ -32,13 +28,12 @@ impl GetId for AuthorMeili {
}
}
#[derive(Deserialize)]
pub struct BookMeili {
pub id: i32,
pub title: String,
pub lang: String,
pub genres: Vec<i32>
pub genres: Vec<i32>,
}
impl GetId for BookMeili {
@@ -47,14 +42,13 @@ impl GetId for BookMeili {
}
}
#[derive(Deserialize)]
pub struct GenreMeili {
pub id: i32,
pub description: String,
pub meta: String,
pub langs: Vec<String>,
pub books_count: i32
pub books_count: i32,
}
impl GetId for GenreMeili {
@@ -63,13 +57,12 @@ impl GetId for GenreMeili {
}
}
#[derive(Deserialize)]
pub struct SequenceMeili {
pub id: i32,
pub name: String,
pub langs: Vec<String>,
pub books_count: i32
pub books_count: i32,
}
impl GetId for SequenceMeili {

View File

@@ -1,7 +1,8 @@
// Code generated by Prisma Client Rust. DO NOT EDIT
pub static DATAMODEL_STR: &str =
include_str!("../prisma/schema.prisma");
#![allow(clippy::all)]
pub static DATAMODEL_STR: &str = include_str!("../prisma/schema.prisma");
static DATABASE_STR: &str = "postgresql";
pub async fn new_client() -> Result<PrismaClient, ::prisma_client_rust::NewClientError> {
PrismaClient::_builder().build().await
@@ -21,9 +22,7 @@ pub mod author_annotation {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -92,9 +91,7 @@ pub mod author_annotation {
pub mod author_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "author_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -167,9 +164,7 @@ pub mod author_annotation {
pub mod title {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "title";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -234,9 +229,7 @@ pub mod author_annotation {
pub mod text {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "text";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -297,9 +290,7 @@ pub mod author_annotation {
pub mod file {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "file";
pub struct Set(pub Option<String>);
impl From<Set> for SetParam {
@@ -364,9 +355,7 @@ pub mod author_annotation {
pub mod author {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "author";
pub struct Fetch(pub author::UniqueArgs);
impl Fetch {
@@ -1019,9 +1008,7 @@ pub mod author {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -1090,9 +1077,7 @@ pub mod author {
pub mod source_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "source_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -1165,9 +1150,7 @@ pub mod author {
pub mod remote_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "remote_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -1240,9 +1223,7 @@ pub mod author {
pub mod first_name {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "first_name";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -1307,9 +1288,7 @@ pub mod author {
pub mod last_name {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "last_name";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -1374,9 +1353,7 @@ pub mod author {
pub mod middle_name {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "middle_name";
pub struct Set(pub Option<String>);
impl From<Set> for SetParam {
@@ -1441,9 +1418,7 @@ pub mod author {
pub mod source {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "source";
pub struct Fetch(pub source::UniqueArgs);
impl Fetch {
@@ -1549,9 +1524,7 @@ pub mod author {
pub mod author_annotation {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "author_annotation";
pub struct Fetch(pub author_annotation::UniqueArgs);
impl Fetch {
@@ -1637,9 +1610,7 @@ pub mod author {
pub mod book_authors {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_authors";
pub struct Fetch(pub book_author::ManyArgs);
impl Fetch {
@@ -1786,9 +1757,7 @@ pub mod author {
pub mod translations {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "translations";
pub struct Fetch(pub translator::ManyArgs);
impl Fetch {
@@ -2903,9 +2872,7 @@ pub mod book_annotation {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -2974,9 +2941,7 @@ pub mod book_annotation {
pub mod book_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "book_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -3045,9 +3010,7 @@ pub mod book_annotation {
pub mod title {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "title";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -3112,9 +3075,7 @@ pub mod book_annotation {
pub mod text {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "text";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -3175,9 +3136,7 @@ pub mod book_annotation {
pub mod file {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "file";
pub struct Set(pub Option<String>);
impl From<Set> for SetParam {
@@ -3242,9 +3201,7 @@ pub mod book_annotation {
pub mod book {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book";
pub struct Fetch(pub book::UniqueArgs);
impl Fetch {
@@ -3885,9 +3842,7 @@ pub mod book_author {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -3956,9 +3911,7 @@ pub mod book_author {
pub mod author_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "author_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -4031,9 +3984,7 @@ pub mod book_author {
pub mod book_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "book_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -4102,9 +4053,7 @@ pub mod book_author {
pub mod author {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "author";
pub struct Fetch(pub author::UniqueArgs);
impl Fetch {
@@ -4210,9 +4159,7 @@ pub mod book_author {
pub mod book {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book";
pub struct Fetch(pub book::UniqueArgs);
impl Fetch {
@@ -4923,9 +4870,7 @@ pub mod book_genre {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -4994,9 +4939,7 @@ pub mod book_genre {
pub mod genre_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "genre_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -5065,9 +5008,7 @@ pub mod book_genre {
pub mod book_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "book_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -5136,9 +5077,7 @@ pub mod book_genre {
pub mod book {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book";
pub struct Fetch(pub book::UniqueArgs);
impl Fetch {
@@ -5243,9 +5182,7 @@ pub mod book_genre {
pub mod genre {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "genre";
pub struct Fetch(pub genre::UniqueArgs);
impl Fetch {
@@ -5956,9 +5893,7 @@ pub mod book_sequence {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -6027,9 +5962,7 @@ pub mod book_sequence {
pub mod position {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "position";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -6102,9 +6035,7 @@ pub mod book_sequence {
pub mod sequence_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "sequence_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -6177,9 +6108,7 @@ pub mod book_sequence {
pub mod book_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "book_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -6248,9 +6177,7 @@ pub mod book_sequence {
pub mod book {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book";
pub struct Fetch(pub book::UniqueArgs);
impl Fetch {
@@ -6355,9 +6282,7 @@ pub mod book_sequence {
pub mod sequence {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "sequence";
pub struct Fetch(pub sequence::UniqueArgs);
impl Fetch {
@@ -7142,9 +7067,7 @@ pub mod book {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -7213,9 +7136,7 @@ pub mod book {
pub mod source_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "source_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -7288,9 +7209,7 @@ pub mod book {
pub mod remote_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "remote_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -7363,9 +7282,7 @@ pub mod book {
pub mod title {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "title";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -7430,9 +7347,7 @@ pub mod book {
pub mod lang {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "lang";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -7493,9 +7408,7 @@ pub mod book {
pub mod file_type {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "file_type";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -7560,9 +7473,7 @@ pub mod book {
pub mod uploaded {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "uploaded";
pub struct Set(
pub ::prisma_client_rust::chrono::DateTime<::prisma_client_rust::chrono::FixedOffset>,
@@ -7665,9 +7576,7 @@ pub mod book {
pub mod is_deleted {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "is_deleted";
pub struct Set(pub bool);
impl From<Set> for SetParam {
@@ -7722,9 +7631,7 @@ pub mod book {
pub mod pages {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "pages";
pub struct Set(pub Option<i32>);
impl From<Set> for SetParam {
@@ -7797,9 +7704,7 @@ pub mod book {
pub mod source {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "source";
pub struct Fetch(pub source::UniqueArgs);
impl Fetch {
@@ -7905,9 +7810,7 @@ pub mod book {
pub mod book_annotation {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_annotation";
pub struct Fetch(pub book_annotation::UniqueArgs);
impl Fetch {
@@ -7993,9 +7896,7 @@ pub mod book {
pub mod book_authors {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_authors";
pub struct Fetch(pub book_author::ManyArgs);
impl Fetch {
@@ -8142,9 +8043,7 @@ pub mod book {
pub mod book_genres {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_genres";
pub struct Fetch(pub book_genre::ManyArgs);
impl Fetch {
@@ -8291,9 +8190,7 @@ pub mod book {
pub mod book_sequences {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_sequences";
pub struct Fetch(pub book_sequence::ManyArgs);
impl Fetch {
@@ -8410,9 +8307,7 @@ pub mod book {
pub mod translations {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "translations";
pub struct Fetch(pub translator::ManyArgs);
impl Fetch {
@@ -9901,9 +9796,7 @@ pub mod genre {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -9972,9 +9865,7 @@ pub mod genre {
pub mod source_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "source_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -10047,9 +9938,7 @@ pub mod genre {
pub mod remote_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "remote_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -10122,9 +10011,7 @@ pub mod genre {
pub mod code {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "code";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -10185,9 +10072,7 @@ pub mod genre {
pub mod description {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "description";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -10252,9 +10137,7 @@ pub mod genre {
pub mod meta {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "meta";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -10315,9 +10198,7 @@ pub mod genre {
pub mod source {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "source";
pub struct Fetch(pub source::UniqueArgs);
impl Fetch {
@@ -10423,9 +10304,7 @@ pub mod genre {
pub mod book_genres {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_genres";
pub struct Fetch(pub book_genre::ManyArgs);
impl Fetch {
@@ -11334,9 +11213,7 @@ pub mod sequence {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -11405,9 +11282,7 @@ pub mod sequence {
pub mod source_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "source_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -11480,9 +11355,7 @@ pub mod sequence {
pub mod remote_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "remote_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -11555,9 +11428,7 @@ pub mod sequence {
pub mod name {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "name";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -11618,9 +11489,7 @@ pub mod sequence {
pub mod source {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "source";
pub struct Fetch(pub source::UniqueArgs);
impl Fetch {
@@ -11726,9 +11595,7 @@ pub mod sequence {
pub mod book_sequences {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book_sequences";
pub struct Fetch(pub book_sequence::ManyArgs);
impl Fetch {
@@ -12535,9 +12402,7 @@ pub mod source {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -12606,9 +12471,7 @@ pub mod source {
pub mod name {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "name";
pub struct Set(pub String);
impl From<Set> for SetParam {
@@ -12669,9 +12532,7 @@ pub mod source {
pub mod authors {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "authors";
pub struct Fetch(pub author::ManyArgs);
impl Fetch {
@@ -12818,9 +12679,7 @@ pub mod source {
pub mod books {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "books";
pub struct Fetch(pub book::ManyArgs);
impl Fetch {
@@ -12960,9 +12819,7 @@ pub mod source {
pub mod genres {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "genres";
pub struct Fetch(pub genre::ManyArgs);
impl Fetch {
@@ -13108,9 +12965,7 @@ pub mod source {
pub mod sequences {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "sequences";
pub struct Fetch(pub sequence::ManyArgs);
impl Fetch {
@@ -14068,9 +13923,7 @@ pub mod translator {
pub mod id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam};
pub const NAME: &str = "id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -14139,9 +13992,7 @@ pub mod translator {
pub mod position {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "position";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -14214,9 +14065,7 @@ pub mod translator {
pub mod author_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "author_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -14289,9 +14138,7 @@ pub mod translator {
pub mod book_id {
use super::super::*;
use super::{
OrderByParam, SetParam, UncheckedSetParam, WhereParam,
};
use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam};
pub const NAME: &str = "book_id";
pub struct Set(pub i32);
impl From<Set> for SetParam {
@@ -14360,9 +14207,7 @@ pub mod translator {
pub mod author {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "author";
pub struct Fetch(pub author::UniqueArgs);
impl Fetch {
@@ -14468,9 +14313,7 @@ pub mod translator {
pub mod book {
use super::super::*;
use super::{
SetParam, WhereParam, WithParam,
};
use super::{SetParam, WhereParam, WithParam};
pub const NAME: &str = "book";
pub struct Fetch(pub book::UniqueArgs);
impl Fetch {

View File

@@ -1,15 +1,11 @@
use serde::Deserialize;
fn default_langs() -> Vec<String> {
vec![
"ru".to_string(),
"be".to_string(),
"uk".to_string()
]
vec!["ru".to_string(), "be".to_string(), "uk".to_string()]
}
#[derive(Deserialize)]
pub struct AllowedLangs {
#[serde(default = "default_langs")]
pub allowed_langs: Vec<String>
pub allowed_langs: Vec<String>,
}

View File

@@ -2,7 +2,10 @@ use serde::Serialize;
use crate::prisma::{author, book};
use super::{sequence::Sequence, utils::{get_available_types, get_translators, get_sequences}};
use super::{
sequence::Sequence,
utils::{get_available_types, get_sequences, get_translators},
};
#[derive(Serialize)]
pub struct Author {
@@ -34,7 +37,6 @@ impl From<author::Data> for Author {
}
}
#[derive(Serialize)]
pub struct AuthorBook {
pub id: i32,

View File

@@ -7,18 +7,24 @@ pub struct AuthorAnnotation {
pub id: i32,
pub title: String,
pub text: String,
pub file: Option<String>
pub file: Option<String>,
}
impl From<author_annotation::Data> for AuthorAnnotation {
fn from(val: author_annotation::Data) -> Self {
let author_annotation::Data { id, title, text, file, .. } = val;
let author_annotation::Data {
id,
title,
text,
file,
..
} = val;
AuthorAnnotation {
id,
title,
text,
file
file,
}
}
}

View File

@@ -1,20 +1,20 @@
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use crate::prisma::book::{self};
use super::{source::Source, utils::{get_available_types, get_translators, get_sequences, get_authors, get_genres}, author::Author, sequence::Sequence, genre::Genre};
use super::{
author::Author,
genre::Genre,
sequence::Sequence,
source::Source,
utils::{get_authors, get_available_types, get_genres, get_sequences, get_translators},
};
fn default_langs() -> Vec<String> {
vec![
"ru".to_string(),
"be".to_string(),
"uk".to_string()
]
vec!["ru".to_string(), "be".to_string(), "uk".to_string()]
}
#[derive(Deserialize)]
pub struct BookFilter {
#[serde(default = "default_langs")]
@@ -30,45 +30,39 @@ impl BookFilter {
pub fn get_filter_vec(self) -> Vec<book::WhereParam> {
let mut result = vec![];
result.push(
book::lang::in_vec(self.allowed_langs)
);
result.push(book::lang::in_vec(self.allowed_langs));
match self.is_deleted {
Some(v) => {
result.push(
book::is_deleted::equals(v)
);
},
result.push(book::is_deleted::equals(v));
}
None => {
result.push(
book::is_deleted::equals(false)
);
},
result.push(book::is_deleted::equals(false));
}
};
if let Some(uploaded_gte) = self.uploaded_gte {
result.push(
book::uploaded::gte(NaiveDateTime::new(uploaded_gte, NaiveTime::default()).and_utc().into())
);
result.push(book::uploaded::gte(
NaiveDateTime::new(uploaded_gte, NaiveTime::default())
.and_utc()
.into(),
));
};
if let Some(uploaded_lte) = self.uploaded_lte {
result.push(
book::uploaded::lte(NaiveDateTime::new(uploaded_lte, NaiveTime::default()).and_utc().into())
);
result.push(book::uploaded::lte(
NaiveDateTime::new(uploaded_lte, NaiveTime::default())
.and_utc()
.into(),
));
};
if let Some(id_gte) = self.id_gte {
result.push(
book::id::gte(id_gte)
);
result.push(book::id::gte(id_gte));
};
if let Some(id_lte) = self.id_lte {
result.push(
book::id::lte(id_lte)
);
result.push(book::id::lte(id_lte));
};
result
@@ -120,7 +114,7 @@ impl From<book::Data> for RemoteBook {
sequences: get_sequences(book_sequences),
annotation_exists: book_annotation.unwrap().is_some(),
source: source.unwrap().as_ref().clone().into(),
remote_id
remote_id,
}
}
}
@@ -163,7 +157,7 @@ pub struct DetailBook {
pub remote_id: i32,
pub genres: Vec<Genre>,
pub is_deleted: bool,
pub pages: Option<i32>
pub pages: Option<i32>,
}
impl From<book::Data> for DetailBook {
@@ -209,7 +203,7 @@ impl From<book::Data> for DetailBook {
#[derive(Deserialize)]
pub struct RandomBookFilter {
pub allowed_langs: Vec<String>,
pub genre: Option<i32>
pub genre: Option<i32>,
}
#[derive(Serialize)]

View File

@@ -2,13 +2,12 @@ use serde::Serialize;
use crate::prisma::book_annotation;
#[derive(Serialize)]
pub struct BookAnnotation {
pub id: i32,
pub title: String,
pub text: String,
pub file: Option<String>
pub file: Option<String>,
}
impl From<book_annotation::Data> for BookAnnotation {
@@ -25,7 +24,7 @@ impl From<book_annotation::Data> for BookAnnotation {
id,
title,
text,
file
file,
}
}
}

View File

@@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use crate::prisma::genre;
@@ -11,7 +11,7 @@ pub struct Genre {
pub remote_id: i32,
pub code: String,
pub description: String,
pub meta: String
pub meta: String,
}
impl From<genre::Data> for Genre {
@@ -32,12 +32,11 @@ impl From<genre::Data> for Genre {
code,
description,
meta,
source: source.unwrap().as_ref().clone().into()
source: source.unwrap().as_ref().clone().into(),
}
}
}
#[derive(Deserialize)]
pub struct GenreFilter {
pub meta: Option<String>,

View File

@@ -1,11 +1,11 @@
pub mod pagination;
pub mod allowed_langs;
pub mod author;
pub mod author_annotation;
pub mod genre;
pub mod source;
pub mod book;
pub mod sequence;
pub mod utils;
pub mod translator;
pub mod allowed_langs;
pub mod book_annotation;
pub mod genre;
pub mod pagination;
pub mod sequence;
pub mod source;
pub mod translator;
pub mod utils;

View File

@@ -1,6 +1,5 @@
use serde::{Deserialize, Serialize};
fn default_page() -> i64 {
1
}
@@ -14,17 +13,16 @@ pub struct Pagination {
#[serde(default = "default_page")]
pub page: i64,
#[serde(default = "default_size")]
pub size: i64
pub size: i64,
}
#[derive(Serialize)]
pub struct Page<T> {
pub items: Vec<T>,
pub total: i64,
pub page: i64,
pub size: i64,
pub pages: i64
pub pages: i64,
}
#[derive(Serialize)]
@@ -34,7 +32,7 @@ pub struct PageWithParent<T, P> {
pub page: i64,
pub size: i64,
pub pages: i64,
pub parent_item: P
pub parent_item: P,
}
impl<T> Page<T> {
@@ -44,7 +42,7 @@ impl<T> Page<T> {
total,
page: pagination.page,
size: pagination.size,
pages: (total + pagination.size - 1) / pagination.size
pages: (total + pagination.size - 1) / pagination.size,
}
}
}
@@ -57,7 +55,7 @@ impl<T, P> PageWithParent<T, P> {
page: pagination.page,
size: pagination.size,
pages: (total + pagination.size - 1) / pagination.size,
parent_item
parent_item,
}
}
}

View File

@@ -1,9 +1,11 @@
use serde::Serialize;
use crate::prisma::{sequence, book};
use super::{author::Author, utils::{get_available_types, get_authors, get_translators}};
use crate::prisma::{book, sequence};
use super::{
author::Author,
utils::{get_authors, get_available_types, get_translators},
};
#[derive(Serialize)]
pub struct Sequence {

View File

@@ -5,21 +5,13 @@ use crate::prisma::source;
#[derive(Serialize)]
pub struct Source {
pub id: i32,
pub name: String
pub name: String,
}
impl From<source::Data> for Source
{
impl From<source::Data> for Source {
fn from(val: source::Data) -> Self {
let source::Data {
id,
name,
..
} = val;
let source::Data { id, name, .. } = val;
Source {
id,
name
}
Source { id, name }
}
}

View File

@@ -2,7 +2,11 @@ use serde::Serialize;
use crate::prisma::book;
use super::{author::Author, sequence::Sequence, utils::{get_available_types, get_authors, get_sequences}};
use super::{
author::Author,
sequence::Sequence,
utils::{get_authors, get_available_types, get_sequences},
};
#[derive(Serialize)]
pub struct TranslatorBook {

View File

@@ -1,6 +1,6 @@
use crate::prisma::{translator, book_sequence, book_author, book_genre};
use crate::prisma::{book_author, book_genre, book_sequence, translator};
use super::{author::Author, sequence::Sequence, genre::Genre};
use super::{author::Author, genre::Genre, sequence::Sequence};
pub fn get_available_types(file_type: String, source_name: String) -> Vec<String> {
if file_type == "fb2" && source_name == "flibusta" {
@@ -15,9 +15,7 @@ pub fn get_available_types(file_type: String, source_name: String) -> Vec<String
}
}
pub fn get_authors(
book_authors: Option<Vec<book_author::Data>>
) -> Vec<Author> {
pub fn get_authors(book_authors: Option<Vec<book_author::Data>>) -> Vec<Author> {
book_authors
.unwrap()
.iter()
@@ -25,9 +23,7 @@ pub fn get_authors(
.collect()
}
pub fn get_translators(
translations: Option<Vec<translator::Data>>
) -> Vec<Author> {
pub fn get_translators(translations: Option<Vec<translator::Data>>) -> Vec<Author> {
translations
.unwrap()
.iter()
@@ -35,9 +31,7 @@ pub fn get_translators(
.collect()
}
pub fn get_sequences(
book_sequences: Option<Vec<book_sequence::Data>>
) -> Vec<Sequence> {
pub fn get_sequences(book_sequences: Option<Vec<book_sequence::Data>>) -> Vec<Sequence> {
book_sequences
.unwrap()
.iter()
@@ -45,9 +39,7 @@ pub fn get_sequences(
.collect()
}
pub fn get_genres(
book_genres: Option<Vec<book_genre::Data>>
) -> Vec<Genre> {
pub fn get_genres(book_genres: Option<Vec<book_genre::Data>>) -> Vec<Genre> {
book_genres
.unwrap()
.iter()

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