Both endpoints are public reads — anyone browsing can see a manga's
table of contents and chapter metadata. Uploads land in feat/uploads.
- GET /api/v1/mangas/{id}/chapters returns the paged envelope
({items, page}) ordered by chapter number ASC. Surfaces 404 if the
parent manga doesn't exist so an empty result can't be mistaken for
"no chapters yet" on a real manga.
- GET /api/v1/mangas/{id}/chapters/{number} returns a single chapter,
404 if either manga or chapter is missing.
repo::chapter exposes list_for_manga, find_by_manga_and_number, and
create. create translates the (manga_id, number) unique violation into
AppError::Conflict so the upload handler can later return a clean 409.
Frontend lib/api/chapters.ts mirrors the shape with listChapters and
getChapter; Vitest asserts the URL shape, paged response handling, and
404 envelope propagation.
Lockstep version bump to 0.4.0.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
//! Chapter list + get. Reads are public — anyone can browse a manga's
|
|
//! table of contents and individual chapter metadata. Uploads land in
|
|
//! feat/uploads under POST /api/v1/mangas/{id}/chapters.
|
|
|
|
use axum::extract::{Path, Query, State};
|
|
use axum::routing::get;
|
|
use axum::{Json, Router};
|
|
use serde::Deserialize;
|
|
use uuid::Uuid;
|
|
|
|
use crate::api::pagination::PagedResponse;
|
|
use crate::app::AppState;
|
|
use crate::domain::Chapter;
|
|
use crate::error::AppResult;
|
|
use crate::repo;
|
|
|
|
pub fn routes() -> Router<AppState> {
|
|
Router::new()
|
|
.route("/mangas/:manga_id/chapters", get(list))
|
|
.route("/mangas/:manga_id/chapters/:number", get(get_one))
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct ListParams {
|
|
#[serde(default = "default_limit")]
|
|
pub limit: i64,
|
|
#[serde(default)]
|
|
pub offset: i64,
|
|
}
|
|
|
|
fn default_limit() -> i64 {
|
|
50
|
|
}
|
|
|
|
async fn list(
|
|
State(state): State<AppState>,
|
|
Path(manga_id): Path<Uuid>,
|
|
Query(params): Query<ListParams>,
|
|
) -> AppResult<Json<PagedResponse<Chapter>>> {
|
|
// Surface 404 when the parent manga doesn't exist so an empty result
|
|
// can't be mistaken for "no chapters yet" on a real manga.
|
|
repo::manga::get(&state.db, manga_id).await?;
|
|
|
|
let limit = params.limit.clamp(1, 200);
|
|
let offset = params.offset.max(0);
|
|
let items = repo::chapter::list_for_manga(&state.db, manga_id, limit, offset).await?;
|
|
Ok(Json(PagedResponse::new(items, limit, offset)))
|
|
}
|
|
|
|
async fn get_one(
|
|
State(state): State<AppState>,
|
|
Path((manga_id, number)): Path<(Uuid, i32)>,
|
|
) -> AppResult<Json<Chapter>> {
|
|
repo::manga::get(&state.db, manga_id).await?;
|
|
let chapter = repo::chapter::find_by_manga_and_number(&state.db, manga_id, number)
|
|
.await?
|
|
.ok_or(crate::error::AppError::NotFound)?;
|
|
Ok(Json(chapter))
|
|
}
|