Three layering cleanups from REVIEW.md §5 / §3:
- Drop the three private `is_unique_violation` helpers in
repo::{user,chapter,bookmark} in favour of sqlx 0.8's
`DatabaseError::is_unique_violation()` method (already used by
repo::collection).
- Remove the unreachable 23505 branch in repo::chapter::create — the
(manga_id, number) UNIQUE was dropped in 0013, so the defensive arm
could no longer fire. A doc note records what to do if uniqueness
is re-added.
- Move three inline SQL queries out of handlers/daemon into repo
functions: bookmarks' chapter-belongs-to-manga guard
(`repo::chapter::belongs_to_manga`), the daemon's dispatch lookup
(`repo::chapter::dispatch_target`), and the daemon's page_count
safety net (`repo::chapter::page_count`). Restores the
handlers→repo layering invariant in CLAUDE.md.
- New `crawler::url_utils` module consolidates host_of / origin_of /
registrable_domain — they used to live in three crawler submodules
with diverging edge-case behaviour. Tests moved with them.
- Doc cross-references on repo::author::set_for_manga and
repo::genre::set_for_manga pointing to the crawler's name-keyed
variants, so the intentional duplication is discoverable.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
154 lines
4.5 KiB
Rust
154 lines
4.5 KiB
Rust
//! Chapter persistence.
|
|
|
|
use sqlx::{PgExecutor, PgPool};
|
|
use uuid::Uuid;
|
|
|
|
use crate::domain::Chapter;
|
|
use crate::error::AppResult;
|
|
|
|
pub async fn list_for_manga(
|
|
pool: &PgPool,
|
|
manga_id: Uuid,
|
|
limit: i64,
|
|
offset: i64,
|
|
) -> AppResult<Vec<Chapter>> {
|
|
// Secondary sort by created_at gives duplicate-numbered chapters
|
|
// (multiple uploaders/translations of the same number) a stable
|
|
// order in lists and prev/next reader navigation.
|
|
let rows = sqlx::query_as::<_, Chapter>(
|
|
r#"
|
|
SELECT id, manga_id, number, title, page_count, created_at
|
|
FROM chapters
|
|
WHERE manga_id = $1
|
|
ORDER BY number ASC, created_at ASC
|
|
LIMIT $2 OFFSET $3
|
|
"#,
|
|
)
|
|
.bind(manga_id)
|
|
.bind(limit)
|
|
.bind(offset)
|
|
.fetch_all(pool)
|
|
.await?;
|
|
Ok(rows)
|
|
}
|
|
|
|
/// Look up a chapter by its UUID, scoped to its manga so a UUID guessed
|
|
/// from a different manga's URL doesn't accidentally resolve.
|
|
pub async fn find_by_id_in_manga(
|
|
pool: &PgPool,
|
|
manga_id: Uuid,
|
|
chapter_id: Uuid,
|
|
) -> AppResult<Option<Chapter>> {
|
|
let row = sqlx::query_as::<_, Chapter>(
|
|
r#"
|
|
SELECT id, manga_id, number, title, page_count, created_at
|
|
FROM chapters
|
|
WHERE manga_id = $1 AND id = $2
|
|
"#,
|
|
)
|
|
.bind(manga_id)
|
|
.bind(chapter_id)
|
|
.fetch_optional(pool)
|
|
.await?;
|
|
Ok(row)
|
|
}
|
|
|
|
/// Accepts any `PgExecutor` so the upload handler can run this inside a
|
|
/// transaction with the per-page inserts.
|
|
///
|
|
/// `uploaded_by` records who uploaded the chapter and feeds the
|
|
/// per-user upload history. `None` means "historical / API token with
|
|
/// no associated user" — kept nullable to support that case.
|
|
///
|
|
/// Chapter identity is the row UUID; the same (manga_id, number)
|
|
/// combination can repeat (multiple translations, re-uploads). The
|
|
/// 0013 migration dropped the (manga_id, number) UNIQUE, so duplicate
|
|
/// inserts succeed by design. If a future migration re-adds any
|
|
/// uniqueness, surface a 409 by adding a unique-violation arm here.
|
|
pub async fn create<'e, E: PgExecutor<'e>>(
|
|
executor: E,
|
|
manga_id: Uuid,
|
|
number: i32,
|
|
title: Option<&str>,
|
|
uploaded_by: Option<Uuid>,
|
|
) -> AppResult<Chapter> {
|
|
let row = sqlx::query_as::<_, Chapter>(
|
|
r#"
|
|
INSERT INTO chapters (manga_id, number, title, uploaded_by)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING id, manga_id, number, title, page_count, created_at
|
|
"#,
|
|
)
|
|
.bind(manga_id)
|
|
.bind(number)
|
|
.bind(title)
|
|
.bind(uploaded_by)
|
|
.fetch_one(executor)
|
|
.await?;
|
|
Ok(row)
|
|
}
|
|
|
|
/// Cross-link guard for `POST /bookmarks`: the bookmarks FK accepts
|
|
/// any valid chapter id, but a chapter must belong to the bookmark's
|
|
/// manga or the bookmark would dangle on a foreign manga. Handlers
|
|
/// call this before the insert and surface `NotFound` when it
|
|
/// returns `false`.
|
|
pub async fn belongs_to_manga(
|
|
pool: &PgPool,
|
|
chapter_id: Uuid,
|
|
manga_id: Uuid,
|
|
) -> AppResult<bool> {
|
|
let (exists,): (bool,) = sqlx::query_as(
|
|
"SELECT EXISTS(SELECT 1 FROM chapters WHERE id = $1 AND manga_id = $2)",
|
|
)
|
|
.bind(chapter_id)
|
|
.bind(manga_id)
|
|
.fetch_one(pool)
|
|
.await?;
|
|
Ok(exists)
|
|
}
|
|
|
|
/// Read just the page_count for a chapter. Used by the crawler
|
|
/// daemon's consumer-side dedup safety net so it can ack-done a job
|
|
/// whose chapter has already been fetched by a racing worker.
|
|
pub async fn page_count(pool: &PgPool, id: Uuid) -> sqlx::Result<Option<i32>> {
|
|
sqlx::query_scalar("SELECT page_count FROM chapters WHERE id = $1")
|
|
.bind(id)
|
|
.fetch_optional(pool)
|
|
.await
|
|
}
|
|
|
|
/// Look up the manga_id + most recent source_url for a chapter. Used
|
|
/// by the daemon's chapter dispatcher to resolve the URL it needs to
|
|
/// hand to `content::sync_chapter_content`. Returns `None` if the
|
|
/// chapter (or its source row) is gone.
|
|
pub async fn dispatch_target(
|
|
pool: &PgPool,
|
|
chapter_id: Uuid,
|
|
) -> sqlx::Result<Option<(Uuid, String)>> {
|
|
sqlx::query_as(
|
|
"SELECT c.manga_id, cs.source_url \
|
|
FROM chapters c \
|
|
JOIN chapter_sources cs ON cs.chapter_id = c.id \
|
|
WHERE c.id = $1 \
|
|
LIMIT 1",
|
|
)
|
|
.bind(chapter_id)
|
|
.fetch_optional(pool)
|
|
.await
|
|
}
|
|
|
|
pub async fn set_page_count<'e, E: PgExecutor<'e>>(
|
|
executor: E,
|
|
id: Uuid,
|
|
page_count: i32,
|
|
) -> AppResult<()> {
|
|
sqlx::query("UPDATE chapters SET page_count = $1 WHERE id = $2")
|
|
.bind(page_count)
|
|
.bind(id)
|
|
.execute(executor)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|