Revert "Update books query"

This reverts commit abee7403b7.
This commit is contained in:
2025-01-05 14:17:04 +01:00
parent abee7403b7
commit 91afa29862
4 changed files with 123 additions and 200 deletions

1
.gitignore vendored
View File

@@ -2,4 +2,3 @@
.env .env
.vscode .vscode
.idea

8
.idea/.gitignore generated vendored
View File

@@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -20,7 +20,7 @@ pub struct BookFilter {
pub id_lte: Option<i32>, pub id_lte: Option<i32>,
} }
#[derive(Serialize, sqlx::FromRow)] #[derive(Serialize)]
pub struct RemoteBook { pub struct RemoteBook {
pub id: i32, pub id: i32,
pub title: String, pub title: String,

View File

@@ -27,198 +27,130 @@ 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 = { let books_count = sqlx::query_scalar!(
let mut query_builder = r#"
sqlx::query_builder::QueryBuilder::new("SELECT COUNT(*) FROM books"); 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();
query_builder.push(" WHERE lang = ANY(?)"); let books = sqlx::query_as!(
query_builder.push_bind(&book_filter.allowed_langs); RemoteBook,
r#"
if let Some(is_deleted) = book_filter.is_deleted { SELECT
query_builder.push(" AND is_deleted = ?"); b.id,
query_builder.push_bind(is_deleted); b.title,
} b.lang,
b.file_type,
match (book_filter.uploaded_gte, book_filter.uploaded_lte) { b.year,
(Some(uploaded_gte), Some(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>",
query_builder.push(" AND uploaded BETWEEN ? AND ?"); b.uploaded,
query_builder.push_bind(uploaded_gte); COALESCE(
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
b.id, ARRAY_AGG(
b.title, ROW(
b.lang, authors.id,
b.file_type, authors.first_name,
b.year, authors.last_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>", authors.middle_name,
b.uploaded, 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 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 translations
authors.id, JOIN authors ON authors.id = translations.author
authors.first_name, WHERE translations.book = b.id
authors.last_name, ),
authors.middle_name, ARRAY[]::author_type[]
EXISTS( ) AS "translators!: 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 sequences.id,
WHERE translations.book = b.id sequences.name
), )::sequence_type
ARRAY[]::author_type[] )
) AS "translators!: Vec<Author>", FROM book_sequences
COALESCE( JOIN sequences ON sequences.id = book_sequences.sequence
( WHERE book_sequences.book = b.id
SELECT ),
ARRAY_AGG( ARRAY[]::sequence_type[]
ROW( ) AS "sequences!: Vec<Sequence>",
sequences.id, EXISTS(
sequences.name SELECT * FROM book_annotations WHERE book = b.id
)::sequence_type ) AS "annotation_exists!: bool",
) (
FROM book_sequences SELECT
JOIN sequences ON sequences.id = book_sequences.sequence ROW(
WHERE book_sequences.book = b.id sources.id,
), sources.name
ARRAY[]::sequence_type[] )::source_type
) AS "sequences!: Vec<Sequence>", FROM sources
EXISTS( WHERE sources.id = b.source
SELECT * FROM book_annotations WHERE book = b.id ) AS "source!: Source",
) AS "annotation_exists!: bool", b.remote_id
( FROM books b
SELECT WHERE lang = ANY($1) AND
ROW( ($2::boolean IS NULL OR is_deleted = $2) AND
sources.id, ($3::date IS NULL OR uploaded >= $3) AND
sources.name ($4::date IS NULL OR uploaded <= $4) AND
)::source_type ($5::integer IS NULL OR id >= $5) AND
FROM sources ($6::integer IS NULL OR id <= $6)
WHERE sources.id = b.source ORDER BY b.id ASC
) AS "source!: Source", OFFSET $7
b.remote_id LIMIT $8
FROM books b "#,
"#, &book_filter.allowed_langs,
); book_filter.is_deleted,
book_filter.uploaded_gte,
query_builder.push(" WHERE lang = ANY(?)"); book_filter.uploaded_lte,
query_builder.push_bind(&book_filter.allowed_langs); book_filter.id_gte,
book_filter.id_lte,
if let Some(is_deleted) = book_filter.is_deleted { (pagination.page - 1) * pagination.size,
query_builder.push(" AND is_deleted = ?"); pagination.size,
query_builder.push_bind(is_deleted); )
} .fetch_all(&db.0)
.await
match (book_filter.uploaded_gte, book_filter.uploaded_lte) { .unwrap();
(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);