Rewrite to rust init

This commit is contained in:
2023-08-11 01:11:27 +02:00
parent 22d8b33bf4
commit 9acdb20463
84 changed files with 28739 additions and 4196 deletions

View File

@@ -0,0 +1,6 @@
use serde::Deserialize;
#[derive(Deserialize)]
pub struct AllowedLangs {
pub allowed_langs: Vec<String>
}

78
src/serializers/author.rs Normal file
View File

@@ -0,0 +1,78 @@
use serde::Serialize;
use crate::prisma::{author, book};
use super::{sequence::Sequence, utils::{get_available_types, get_translators, get_sequences}};
#[derive(Serialize)]
pub struct Author {
pub id: i32,
pub first_name: String,
pub last_name: String,
pub middle_name: String,
pub annotation_exists: bool,
}
impl From<author::Data> for Author {
fn from(val: author::Data) -> Self {
let author::Data {
id,
first_name,
last_name,
middle_name,
author_annotation,
..
} = val;
Author {
id,
first_name,
last_name,
middle_name: middle_name.unwrap_or("".to_string()),
annotation_exists: author_annotation.unwrap().is_some(),
}
}
}
#[derive(Serialize)]
pub struct AuthorBook {
pub id: i32,
pub title: String,
pub lang: String,
pub file_type: String,
pub available_types: Vec<String>,
pub uploaded: String,
pub translators: Vec<Author>,
pub sequences: Vec<Sequence>,
pub annotation_exists: bool,
}
impl From<book::Data> for AuthorBook {
fn from(val: book::Data) -> Self {
let book::Data {
id,
title,
lang,
file_type,
uploaded,
translations,
book_sequences,
book_annotation,
source,
..
} = val;
AuthorBook {
id,
title,
lang,
file_type: file_type.clone(),
available_types: get_available_types(file_type, source.unwrap().name),
uploaded: uploaded.format("%Y-%m-%d").to_string(),
translators: get_translators(translations),
sequences: get_sequences(book_sequences),
annotation_exists: book_annotation.unwrap().is_some(),
}
}
}

View File

@@ -0,0 +1,24 @@
use serde::Serialize;
use crate::prisma::author_annotation;
#[derive(Serialize)]
pub struct AuthorAnnotation {
pub id: i32,
pub title: String,
pub text: 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;
AuthorAnnotation {
id,
title,
text,
file
}
}
}

248
src/serializers/book.rs Normal file
View File

@@ -0,0 +1,248 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
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};
#[derive(Deserialize)]
pub struct BookFilter {
pub allowed_langs: Vec<String>,
pub is_deleted: Option<bool>,
pub uploaded_gte: Option<DateTime<Utc>>,
pub uploaded_lte: Option<DateTime<Utc>>,
pub id_gte: Option<i32>,
pub id_lte: Option<i32>,
}
impl BookFilter {
pub fn get_filter_vec(self) -> Vec<book::WhereParam> {
let mut result = vec![];
result.push(
book::lang::in_vec(self.allowed_langs)
);
match self.is_deleted {
Some(v) => {
result.push(
book::is_deleted::equals(v)
);
},
None => {
result.push(
book::is_deleted::equals(false)
);
},
};
if let Some(uploaded_gte) = self.uploaded_gte {
result.push(
book::uploaded::gte(uploaded_gte.into())
);
};
if let Some(uploaded_lte) = self.uploaded_lte {
result.push(
book::uploaded::lte(uploaded_lte.into())
);
};
if let Some(id_gte) = self.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
}
}
#[derive(Serialize)]
pub struct RemoteBook {
pub id: i32,
pub title: String,
pub lang: String,
pub file_type: String,
pub available_types: Vec<String>,
pub uploaded: String,
pub authors: Vec<Author>,
pub translators: Vec<Author>,
pub sequences: Vec<Sequence>,
pub annotation_exists: bool,
pub source: Source,
pub remote_id: i32,
}
impl From<book::Data> for RemoteBook {
fn from(value: book::Data) -> Self {
let book::Data {
id,
title,
lang,
file_type,
uploaded,
book_authors,
translations,
book_sequences,
book_annotation,
source,
remote_id,
..
} = value;
Self {
id,
title,
lang,
file_type: file_type.clone(),
available_types: get_available_types(file_type, source.clone().unwrap().name),
uploaded: uploaded.format("%Y-%m-%d").to_string(),
authors: get_authors(book_authors),
translators: get_translators(translations),
sequences: get_sequences(book_sequences),
annotation_exists: book_annotation.unwrap().is_some(),
source: source.unwrap().as_ref().clone().into(),
remote_id
}
}
}
#[derive(Serialize)]
pub struct BaseBook {
pub id: i32,
pub available_types: Vec<String>,
}
impl From<book::Data> for BaseBook {
fn from(value: book::Data) -> Self {
let book::Data {
id,
file_type,
source,
..
} = value;
Self {
id,
available_types: get_available_types(file_type, source.clone().unwrap().name),
}
}
}
#[derive(Serialize)]
pub struct DetailBook {
pub id: i32,
pub title: String,
pub lang: String,
pub file_type: String,
pub available_types: Vec<String>,
pub uploaded: String,
pub authors: Vec<Author>,
pub translators: Vec<Author>,
pub sequences: Vec<Sequence>,
pub annotation_exists: bool,
pub source: Source,
pub remote_id: i32,
pub genres: Vec<Genre>,
pub is_deleted: bool,
pub pages: Option<i32>
}
impl From<book::Data> for DetailBook {
fn from(value: book::Data) -> Self {
let book::Data {
id,
title,
lang,
file_type,
uploaded,
book_authors,
translations,
book_sequences,
book_annotation,
source,
remote_id,
book_genres,
is_deleted,
pages,
..
} = value;
Self {
id,
title,
lang,
file_type: file_type.clone(),
available_types: get_available_types(file_type, source.clone().unwrap().name),
uploaded: uploaded.format("%Y-%m-%d").to_string(),
authors: get_authors(book_authors),
translators: get_translators(translations),
sequences: get_sequences(book_sequences),
annotation_exists: book_annotation.unwrap().is_some(),
source: source.unwrap().as_ref().clone().into(),
remote_id,
genres: get_genres(book_genres),
is_deleted,
pages,
}
}
}
#[derive(Deserialize)]
pub struct RandomBookFilter {
pub allowed_langs: Vec<String>,
pub genre: Option<i32>
}
#[derive(Serialize)]
pub struct Book {
pub id: i32,
pub title: String,
pub lang: String,
pub file_type: String,
pub available_types: Vec<String>,
pub uploaded: String,
pub authors: Vec<Author>,
pub translators: Vec<Author>,
pub sequences: Vec<Sequence>,
pub annotation_exists: bool,
}
impl From<book::Data> for Book {
fn from(value: book::Data) -> Self {
let book::Data {
id,
title,
lang,
file_type,
uploaded,
book_authors,
translations,
book_sequences,
book_annotation,
source,
..
} = value;
Self {
id,
title,
lang,
file_type: file_type.clone(),
available_types: get_available_types(file_type, source.clone().unwrap().name),
uploaded: uploaded.format("%Y-%m-%d").to_string(),
authors: get_authors(book_authors),
translators: get_translators(translations),
sequences: get_sequences(book_sequences),
annotation_exists: book_annotation.unwrap().is_some(),
}
}
}

View File

@@ -0,0 +1,31 @@
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>
}
impl From<book_annotation::Data> for BookAnnotation {
fn from(value: book_annotation::Data) -> Self {
let book_annotation::Data {
id,
title,
text,
file,
..
} = value;
Self {
id,
title,
text,
file
}
}
}

44
src/serializers/genre.rs Normal file
View File

@@ -0,0 +1,44 @@
use serde::{Serialize, Deserialize};
use crate::prisma::genre;
use super::source::Source;
#[derive(Serialize)]
pub struct Genre {
pub id: i32,
pub source: Source,
pub remote_id: i32,
pub code: String,
pub description: String,
pub meta: String
}
impl From<genre::Data> for Genre {
fn from(val: genre::Data) -> Self {
let genre::Data {
id,
remote_id,
code,
description,
meta,
source,
..
} = val;
Genre {
id,
remote_id,
code,
description,
meta,
source: source.unwrap().as_ref().clone().into()
}
}
}
#[derive(Deserialize)]
pub struct GenreFilter {
pub meta: Option<String>,
}

11
src/serializers/mod.rs Normal file
View File

@@ -0,0 +1,11 @@
pub mod pagination;
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;

View File

@@ -0,0 +1,63 @@
use serde::{Deserialize, Serialize};
fn default_page() -> i64 {
1
}
fn default_size() -> i64 {
50
}
#[derive(Deserialize)]
pub struct Pagination {
#[serde(default = "default_page")]
pub page: i64,
#[serde(default = "default_size")]
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
}
#[derive(Serialize)]
pub struct PageWithParent<T, P> {
pub items: Vec<T>,
pub total: i64,
pub page: i64,
pub size: i64,
pub pages: i64,
pub parent_item: P
}
impl<T> Page<T> {
pub fn new(items: Vec<T>, total: i64, pagination: &Pagination) -> Self {
Self {
items,
total,
page: pagination.page,
size: pagination.size,
pages: (total + pagination.size - 1) / pagination.size
}
}
}
impl<T, P> PageWithParent<T, P> {
pub fn new(parent_item: P, items: Vec<T>, total: i64, pagination: &Pagination) -> Self {
Self {
items,
total,
page: pagination.page,
size: pagination.size,
pages: (total + pagination.size - 1) / pagination.size,
parent_item
}
}
}

View File

@@ -0,0 +1,62 @@
use serde::Serialize;
use crate::prisma::{sequence, book};
use super::{author::Author, utils::{get_available_types, get_authors, get_translators}};
#[derive(Serialize)]
pub struct Sequence {
pub id: i32,
pub name: String,
}
impl From<sequence::Data> for Sequence {
fn from(val: sequence::Data) -> Self {
let sequence::Data { id, name, .. } = val;
Sequence { id, name }
}
}
#[derive(Serialize)]
pub struct SequenceBook {
pub id: i32,
pub title: String,
pub lang: String,
pub file_type: String,
pub available_types: Vec<String>,
pub uploaded: String,
pub authors: Vec<Author>,
pub translators: Vec<Author>,
pub annotation_exists: bool,
}
impl From<book::Data> for SequenceBook {
fn from(value: book::Data) -> Self {
let book::Data {
id,
title,
lang,
file_type,
uploaded,
book_authors,
translations,
book_annotation,
source,
..
} = value;
Self {
id,
title,
lang,
file_type: file_type.clone(),
available_types: get_available_types(file_type, source.clone().unwrap().name),
uploaded: uploaded.format("%Y-%m-%d").to_string(),
authors: get_authors(book_authors),
translators: get_translators(translations),
annotation_exists: book_annotation.unwrap().is_some(),
}
}
}

25
src/serializers/source.rs Normal file
View File

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

View File

@@ -0,0 +1,47 @@
use serde::Serialize;
use crate::prisma::book;
use super::{author::Author, sequence::Sequence, utils::{get_available_types, get_authors, get_sequences}};
#[derive(Serialize)]
pub struct TranslatorBook {
pub id: i32,
pub title: String,
pub lang: String,
pub file_type: String,
pub available_types: Vec<String>,
pub uploaded: String,
pub authors: Vec<Author>,
pub sequences: Vec<Sequence>,
pub annotation_exists: bool,
}
impl From<book::Data> for TranslatorBook {
fn from(val: book::Data) -> Self {
let book::Data {
id,
title,
lang,
file_type,
uploaded,
book_authors,
book_sequences,
book_annotation,
source,
..
} = val;
TranslatorBook {
id,
title,
lang,
file_type: file_type.clone(),
available_types: get_available_types(file_type.clone(), source.unwrap().name),
uploaded: uploaded.format("%Y-%m-%d").to_string(),
authors: get_authors(book_authors),
sequences: get_sequences(book_sequences),
annotation_exists: book_annotation.unwrap().is_some(),
}
}
}

56
src/serializers/utils.rs Normal file
View File

@@ -0,0 +1,56 @@
use crate::prisma::{translator, book_sequence, book_author, book_genre};
use super::{author::Author, sequence::Sequence, genre::Genre};
pub fn get_available_types(file_type: String, source_name: String) -> Vec<String> {
if file_type == "fb2" && source_name == "flibusta" {
vec![
"fb2".to_string(),
"fb2zip".to_string(),
"epub".to_string(),
"mobi".to_string(),
]
} else {
vec![file_type]
}
}
pub fn get_authors(
book_authors: Option<Vec<book_author::Data>>
) -> Vec<Author> {
book_authors
.unwrap()
.iter()
.map(|item| item.author.clone().unwrap().as_ref().clone().into())
.collect()
}
pub fn get_translators(
translations: Option<Vec<translator::Data>>
) -> Vec<Author> {
translations
.unwrap()
.iter()
.map(|item| item.author.clone().unwrap().as_ref().clone().into())
.collect()
}
pub fn get_sequences(
book_sequences: Option<Vec<book_sequence::Data>>
) -> Vec<Sequence> {
book_sequences
.unwrap()
.iter()
.map(|item| item.sequence.clone().unwrap().as_ref().clone().into())
.collect()
}
pub fn get_genres(
book_genres: Option<Vec<book_genre::Data>>
) -> Vec<Genre> {
book_genres
.unwrap()
.iter()
.map(|item| item.genre.clone().unwrap().as_ref().clone().into())
.collect()
}