mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 07:05:36 +01:00
Update books query
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
.env
|
.env
|
||||||
.vscode
|
.vscode
|
||||||
|
.idea
|
||||||
|
|||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
@@ -20,7 +20,7 @@ pub struct BookFilter {
|
|||||||
pub id_lte: Option<i32>,
|
pub id_lte: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, sqlx::FromRow)]
|
||||||
pub struct RemoteBook {
|
pub struct RemoteBook {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
|||||||
@@ -27,130 +27,198 @@ pub async fn get_books(
|
|||||||
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<BookFilter>,
|
axum_extra::extract::Query(book_filter): axum_extra::extract::Query<BookFilter>,
|
||||||
pagination: Query<Pagination>,
|
pagination: Query<Pagination>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let books_count = sqlx::query_scalar!(
|
let books_count = {
|
||||||
r#"
|
let mut query_builder =
|
||||||
SELECT COUNT(*) FROM books
|
sqlx::query_builder::QueryBuilder::new("SELECT COUNT(*) FROM books");
|
||||||
WHERE lang = ANY($1) AND
|
|
||||||
($2::boolean IS NULL OR is_deleted = $2) AND
|
|
||||||
($3::date IS NULL OR uploaded >= $3) AND
|
|
||||||
($4::date IS NULL OR uploaded <= $4) AND
|
|
||||||
($5::integer IS NULL OR id >= $5) AND
|
|
||||||
($6::integer IS NULL OR id <= $6)
|
|
||||||
"#,
|
|
||||||
&book_filter.allowed_langs,
|
|
||||||
book_filter.is_deleted,
|
|
||||||
book_filter.uploaded_gte,
|
|
||||||
book_filter.uploaded_lte,
|
|
||||||
book_filter.id_gte,
|
|
||||||
book_filter.id_lte,
|
|
||||||
)
|
|
||||||
.fetch_one(&db.0)
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let books = sqlx::query_as!(
|
query_builder.push(" WHERE lang = ANY(?)");
|
||||||
RemoteBook,
|
query_builder.push_bind(&book_filter.allowed_langs);
|
||||||
r#"
|
|
||||||
SELECT
|
if let Some(is_deleted) = book_filter.is_deleted {
|
||||||
b.id,
|
query_builder.push(" AND is_deleted = ?");
|
||||||
b.title,
|
query_builder.push_bind(is_deleted);
|
||||||
b.lang,
|
}
|
||||||
b.file_type,
|
|
||||||
b.year,
|
match (book_filter.uploaded_gte, book_filter.uploaded_lte) {
|
||||||
CASE WHEN b.file_type = 'fb2' THEN ARRAY['fb2', 'epub', 'mobi', 'fb2zip']::text[] ELSE ARRAY[b.file_type]::text[] END AS "available_types!: Vec<String>",
|
(Some(uploaded_gte), Some(uploaded_lte)) => {
|
||||||
b.uploaded,
|
query_builder.push(" AND uploaded BETWEEN ? AND ?");
|
||||||
COALESCE(
|
query_builder.push_bind(uploaded_gte);
|
||||||
(
|
query_builder.push_bind(uploaded_lte);
|
||||||
|
}
|
||||||
|
(Some(uploaded_gte), None) => {
|
||||||
|
query_builder.push(" AND uploaded >= ?");
|
||||||
|
query_builder.push_bind(uploaded_gte);
|
||||||
|
}
|
||||||
|
(None, Some(uploaded_lte)) => {
|
||||||
|
query_builder.push(" AND uploaded <= ?");
|
||||||
|
query_builder.push_bind(uploaded_lte);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
match (book_filter.id_gte, book_filter.id_lte) {
|
||||||
|
(Some(id_gte), Some(id_lte)) => {
|
||||||
|
query_builder.push(" AND id BETWEEN ? AND ?");
|
||||||
|
query_builder.push_bind(id_gte);
|
||||||
|
query_builder.push_bind(id_lte);
|
||||||
|
}
|
||||||
|
(Some(id_gte), None) => {
|
||||||
|
query_builder.push(" AND id >= ?");
|
||||||
|
query_builder.push_bind(id_gte);
|
||||||
|
}
|
||||||
|
(None, Some(id_lte)) => {
|
||||||
|
query_builder.push(" AND id <= ?");
|
||||||
|
query_builder.push_bind(id_lte);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
query_builder
|
||||||
|
.build_query_scalar()
|
||||||
|
.fetch_one(&db.0)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
let books = {
|
||||||
|
let mut query_builder = sqlx::query_builder::QueryBuilder::new(
|
||||||
|
r#"
|
||||||
SELECT
|
SELECT
|
||||||
ARRAY_AGG(
|
b.id,
|
||||||
ROW(
|
b.title,
|
||||||
authors.id,
|
b.lang,
|
||||||
authors.first_name,
|
b.file_type,
|
||||||
authors.last_name,
|
b.year,
|
||||||
authors.middle_name,
|
CASE WHEN b.file_type = 'fb2' THEN ARRAY['fb2', 'epub', 'mobi', 'fb2zip']::text[] ELSE ARRAY[b.file_type]::text[] END AS "available_types!: Vec<String>",
|
||||||
EXISTS(
|
b.uploaded,
|
||||||
SELECT * FROM author_annotations WHERE author = authors.id
|
COALESCE(
|
||||||
)
|
(
|
||||||
)::author_type
|
SELECT
|
||||||
)
|
ARRAY_AGG(
|
||||||
FROM book_authors
|
ROW(
|
||||||
JOIN authors ON authors.id = book_authors.author
|
authors.id,
|
||||||
WHERE book_authors.book = b.id
|
authors.first_name,
|
||||||
),
|
authors.last_name,
|
||||||
ARRAY[]::author_type[]
|
authors.middle_name,
|
||||||
) AS "authors!: Vec<Author>",
|
EXISTS(
|
||||||
COALESCE(
|
SELECT * FROM author_annotations WHERE author = authors.id
|
||||||
(
|
)
|
||||||
SELECT
|
)::author_type
|
||||||
ARRAY_AGG(
|
)
|
||||||
ROW(
|
FROM book_authors
|
||||||
authors.id,
|
JOIN authors ON authors.id = book_authors.author
|
||||||
authors.first_name,
|
WHERE book_authors.book = b.id
|
||||||
authors.last_name,
|
),
|
||||||
authors.middle_name,
|
ARRAY[]::author_type[]
|
||||||
EXISTS(
|
) AS "authors!: Vec<Author>",
|
||||||
SELECT * FROM author_annotations WHERE author = authors.id
|
COALESCE(
|
||||||
)
|
(
|
||||||
)::author_type
|
SELECT
|
||||||
)
|
ARRAY_AGG(
|
||||||
FROM translations
|
ROW(
|
||||||
JOIN authors ON authors.id = translations.author
|
authors.id,
|
||||||
WHERE translations.book = b.id
|
authors.first_name,
|
||||||
),
|
authors.last_name,
|
||||||
ARRAY[]::author_type[]
|
authors.middle_name,
|
||||||
) AS "translators!: Vec<Author>",
|
EXISTS(
|
||||||
COALESCE(
|
SELECT * FROM author_annotations WHERE author = authors.id
|
||||||
(
|
)
|
||||||
SELECT
|
)::author_type
|
||||||
ARRAY_AGG(
|
)
|
||||||
ROW(
|
FROM translations
|
||||||
sequences.id,
|
JOIN authors ON authors.id = translations.author
|
||||||
sequences.name
|
WHERE translations.book = b.id
|
||||||
)::sequence_type
|
),
|
||||||
)
|
ARRAY[]::author_type[]
|
||||||
FROM book_sequences
|
) AS "translators!: Vec<Author>",
|
||||||
JOIN sequences ON sequences.id = book_sequences.sequence
|
COALESCE(
|
||||||
WHERE book_sequences.book = b.id
|
(
|
||||||
),
|
SELECT
|
||||||
ARRAY[]::sequence_type[]
|
ARRAY_AGG(
|
||||||
) AS "sequences!: Vec<Sequence>",
|
ROW(
|
||||||
EXISTS(
|
sequences.id,
|
||||||
SELECT * FROM book_annotations WHERE book = b.id
|
sequences.name
|
||||||
) AS "annotation_exists!: bool",
|
)::sequence_type
|
||||||
(
|
)
|
||||||
SELECT
|
FROM book_sequences
|
||||||
ROW(
|
JOIN sequences ON sequences.id = book_sequences.sequence
|
||||||
sources.id,
|
WHERE book_sequences.book = b.id
|
||||||
sources.name
|
),
|
||||||
)::source_type
|
ARRAY[]::sequence_type[]
|
||||||
FROM sources
|
) AS "sequences!: Vec<Sequence>",
|
||||||
WHERE sources.id = b.source
|
EXISTS(
|
||||||
) AS "source!: Source",
|
SELECT * FROM book_annotations WHERE book = b.id
|
||||||
b.remote_id
|
) AS "annotation_exists!: bool",
|
||||||
FROM books b
|
(
|
||||||
WHERE lang = ANY($1) AND
|
SELECT
|
||||||
($2::boolean IS NULL OR is_deleted = $2) AND
|
ROW(
|
||||||
($3::date IS NULL OR uploaded >= $3) AND
|
sources.id,
|
||||||
($4::date IS NULL OR uploaded <= $4) AND
|
sources.name
|
||||||
($5::integer IS NULL OR id >= $5) AND
|
)::source_type
|
||||||
($6::integer IS NULL OR id <= $6)
|
FROM sources
|
||||||
ORDER BY b.id ASC
|
WHERE sources.id = b.source
|
||||||
OFFSET $7
|
) AS "source!: Source",
|
||||||
LIMIT $8
|
b.remote_id
|
||||||
"#,
|
FROM books b
|
||||||
&book_filter.allowed_langs,
|
"#,
|
||||||
book_filter.is_deleted,
|
);
|
||||||
book_filter.uploaded_gte,
|
|
||||||
book_filter.uploaded_lte,
|
query_builder.push(" WHERE lang = ANY(?)");
|
||||||
book_filter.id_gte,
|
query_builder.push_bind(&book_filter.allowed_langs);
|
||||||
book_filter.id_lte,
|
|
||||||
(pagination.page - 1) * pagination.size,
|
if let Some(is_deleted) = book_filter.is_deleted {
|
||||||
pagination.size,
|
query_builder.push(" AND is_deleted = ?");
|
||||||
)
|
query_builder.push_bind(is_deleted);
|
||||||
.fetch_all(&db.0)
|
}
|
||||||
.await
|
|
||||||
.unwrap();
|
match (book_filter.uploaded_gte, book_filter.uploaded_lte) {
|
||||||
|
(Some(uploaded_gte), Some(uploaded_lte)) => {
|
||||||
|
query_builder.push(" AND uploaded BETWEEN ? AND ?");
|
||||||
|
query_builder.push_bind(uploaded_gte);
|
||||||
|
query_builder.push_bind(uploaded_lte);
|
||||||
|
}
|
||||||
|
(Some(uploaded_gte), None) => {
|
||||||
|
query_builder.push(" AND uploaded >= ?");
|
||||||
|
query_builder.push_bind(uploaded_gte);
|
||||||
|
}
|
||||||
|
(None, Some(uploaded_lte)) => {
|
||||||
|
query_builder.push(" AND uploaded <= ?");
|
||||||
|
query_builder.push_bind(uploaded_lte);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
match (book_filter.id_gte, book_filter.id_lte) {
|
||||||
|
(Some(id_gte), Some(id_lte)) => {
|
||||||
|
query_builder.push(" AND id BETWEEN ? AND ?");
|
||||||
|
query_builder.push_bind(id_gte);
|
||||||
|
query_builder.push_bind(id_lte);
|
||||||
|
}
|
||||||
|
(Some(id_gte), None) => {
|
||||||
|
query_builder.push(" AND id >= ?");
|
||||||
|
query_builder.push_bind(id_gte);
|
||||||
|
}
|
||||||
|
(None, Some(id_lte)) => {
|
||||||
|
query_builder.push(" AND id <= ?");
|
||||||
|
query_builder.push_bind(id_lte);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
query_builder.push(" ORDER BY b.id ASC");
|
||||||
|
|
||||||
|
query_builder.push(" OFFSET ?");
|
||||||
|
query_builder.push_bind((pagination.page - 1) * pagination.size);
|
||||||
|
|
||||||
|
query_builder.push(" LIMIT ?");
|
||||||
|
query_builder.push_bind(pagination.size);
|
||||||
|
|
||||||
|
query_builder
|
||||||
|
.build_query_as()
|
||||||
|
.fetch_all(&db.0)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
let page: Page<RemoteBook> = Page::new(books, books_count, &pagination);
|
let page: Page<RemoteBook> = Page::new(books, books_count, &pagination);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user