feat: manga metadata with status, authors, genres, tags, and search filters (0.15.0)
Adds first-class manga metadata across the stack: - **Status** (ongoing / completed), **alternative titles**, normalized **multi-author** support, **curated genres** (13 seeded), and **free-form user tags** (case-insensitive, globally shared). Each is modelled as its own table joined to mangas; `mangas.author` is backfilled into `authors` + `manga_authors` and dropped. - New endpoints: `PATCH /v1/mangas/:id` (three-state `description`), `POST/DELETE /v1/mangas/:id/tags[/:tag_id]`, `GET /v1/genres`, `GET /v1/tags?search=`. - `GET /v1/mangas` now returns `MangaCard` (with authors + genres batched in) and supports `?status=`, `?author_id=`, `?genre_id=`, `?tag_id=` filters — AND across facets, with empty-array no-op semantics for the unnest primitive. - `GET /v1/mangas/:id` returns the enriched `MangaDetail` with tags. - Frontend: reusable `Chip` component; manga detail page renders authors as chips linking to `/authors/:id` (Phase 2), a status badge, alt titles, genres, and tags with inline add/remove (only the attacher sees remove); upload form supports multi-author / multi-genre / alt titles / status; search page gets a collapsible URL-synced filter panel with keyboard-navigable tag autocomplete. - 126 backend tests (incl. AND-across-facets primitive, case-insens author/tag de-dup, transactional create rollback, PATCH semantics for missing / null / set on description); 72 frontend tests + svelte-check clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
16
backend/src/api/genres.rs
Normal file
16
backend/src/api/genres.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
use axum::extract::State;
|
||||
use axum::routing::get;
|
||||
use axum::{Json, Router};
|
||||
|
||||
use crate::app::AppState;
|
||||
use crate::domain::genre::Genre;
|
||||
use crate::error::AppResult;
|
||||
use crate::repo;
|
||||
|
||||
pub fn routes() -> Router<AppState> {
|
||||
Router::new().route("/genres", get(list))
|
||||
}
|
||||
|
||||
async fn list(State(state): State<AppState>) -> AppResult<Json<Vec<Genre>>> {
|
||||
Ok(Json(repo::genre::list_all(&state.db).await?))
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use axum::extract::{Multipart, Path, Query, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::get;
|
||||
use axum::routing::{delete, get, post};
|
||||
use axum::{Json, Router};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
@@ -9,7 +9,8 @@ use uuid::Uuid;
|
||||
use crate::api::pagination::PagedResponse;
|
||||
use crate::app::AppState;
|
||||
use crate::auth::extractor::CurrentUser;
|
||||
use crate::domain::manga::{Manga, NewManga};
|
||||
use crate::domain::manga::{MangaCard, MangaDetail, MangaPatch, NewManga, Patch};
|
||||
use crate::domain::tag::TagRef;
|
||||
use crate::error::{AppError, AppResult};
|
||||
use crate::repo;
|
||||
use crate::upload::{parse_image, UploadedImage};
|
||||
@@ -17,13 +18,25 @@ use crate::upload::{parse_image, UploadedImage};
|
||||
pub fn routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/mangas", get(list).post(create))
|
||||
.route("/mangas/:id", get(get_one))
|
||||
.route("/mangas/:id", get(get_one).patch(update))
|
||||
.route("/mangas/:id/tags", post(attach_tag))
|
||||
.route("/mangas/:id/tags/:tag_id", delete(detach_tag))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Deserialize, Default)]
|
||||
pub struct ListParams {
|
||||
#[serde(default)]
|
||||
pub search: Option<String>,
|
||||
#[serde(default)]
|
||||
pub status: Option<String>,
|
||||
/// Comma-separated UUIDs. AND across the list — every id must be
|
||||
/// attached to the manga for it to match.
|
||||
#[serde(default)]
|
||||
pub author_id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub genre_id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub tag_id: Option<String>,
|
||||
#[serde(default = "default_limit")]
|
||||
pub limit: i64,
|
||||
#[serde(default)]
|
||||
@@ -36,41 +49,78 @@ fn default_limit() -> i64 {
|
||||
50
|
||||
}
|
||||
|
||||
fn parse_uuid_csv(field: &str, raw: Option<&str>) -> AppResult<Vec<Uuid>> {
|
||||
let Some(raw) = raw else { return Ok(Vec::new()) };
|
||||
let mut out = Vec::new();
|
||||
for piece in raw.split(',') {
|
||||
let s = piece.trim();
|
||||
if s.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let id = Uuid::parse_str(s).map_err(|_| AppError::ValidationFailed {
|
||||
message: format!("{field} contained an invalid UUID"),
|
||||
details: json!({ field: "comma-separated UUIDs" }),
|
||||
})?;
|
||||
out.push(id);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
async fn list(
|
||||
State(state): State<AppState>,
|
||||
Query(params): Query<ListParams>,
|
||||
) -> AppResult<Json<PagedResponse<Manga>>> {
|
||||
) -> AppResult<Json<PagedResponse<MangaCard>>> {
|
||||
let limit = params.limit.clamp(1, 200);
|
||||
let offset = params.offset.max(0);
|
||||
let status = match params.status.as_deref().map(str::trim) {
|
||||
None | Some("") => None,
|
||||
Some(s) => {
|
||||
if !repo::manga::STATUSES.contains(&s) {
|
||||
return Err(AppError::ValidationFailed {
|
||||
message: "invalid status filter".into(),
|
||||
details: json!({ "status": format!("must be one of {:?}", repo::manga::STATUSES) }),
|
||||
});
|
||||
}
|
||||
Some(s.to_string())
|
||||
}
|
||||
};
|
||||
let q = repo::manga::ListQuery {
|
||||
search: params.search.filter(|s| !s.trim().is_empty()),
|
||||
status,
|
||||
author_ids: parse_uuid_csv("author_id", params.author_id.as_deref())?,
|
||||
genre_ids: parse_uuid_csv("genre_id", params.genre_id.as_deref())?,
|
||||
tag_ids: parse_uuid_csv("tag_id", params.tag_id.as_deref())?,
|
||||
limit,
|
||||
offset,
|
||||
sort: params.sort,
|
||||
};
|
||||
let (items, total) = repo::manga::list(&state.db, &q).await?;
|
||||
let (items, total) = repo::manga::list_cards(&state.db, &q).await?;
|
||||
Ok(Json(PagedResponse::with_total(items, limit, offset, total)))
|
||||
}
|
||||
|
||||
async fn get_one(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> AppResult<Json<Manga>> {
|
||||
Ok(Json(repo::manga::get(&state.db, id).await?))
|
||||
) -> AppResult<Json<MangaDetail>> {
|
||||
Ok(Json(repo::manga::get_detail(&state.db, id).await?))
|
||||
}
|
||||
|
||||
/// `POST /api/v1/mangas` is multipart/form-data. Parts:
|
||||
///
|
||||
/// - `metadata` (required): JSON body matching `NewManga`.
|
||||
/// - `metadata` (required): JSON body matching `NewManga` — title, optional
|
||||
/// status, optional authors (array of names), optional alt_titles
|
||||
/// (array), optional description, optional genre_ids (array of UUIDs).
|
||||
/// - `cover` (optional): image bytes. MIME is sniffed from magic bytes
|
||||
/// (jpeg/png/webp/gif/avif); size capped at `upload.max_file_bytes`.
|
||||
///
|
||||
/// Anything else is ignored.
|
||||
/// Anything else is ignored. Authors are upserted into the global
|
||||
/// authors table (case-insensitive de-dup); genre_ids are validated
|
||||
/// against the curated genres list.
|
||||
async fn create(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(_user): CurrentUser,
|
||||
mut multipart: Multipart,
|
||||
) -> AppResult<(StatusCode, Json<Manga>)> {
|
||||
) -> AppResult<(StatusCode, Json<MangaDetail>)> {
|
||||
let mut metadata: Option<NewManga> = None;
|
||||
let mut cover: Option<UploadedImage> = None;
|
||||
|
||||
@@ -93,15 +143,36 @@ async fn create(
|
||||
details: json!({ "metadata": "required" }),
|
||||
})?;
|
||||
validate_new_manga(&metadata)?;
|
||||
validate_genre_ids(&state, &metadata.genre_ids).await?;
|
||||
|
||||
// Transactional create. If the cover put or the cover_image_path
|
||||
// UPDATE fails, the manga row is rolled back so a half-uploaded
|
||||
// cover doesn't leave a manga without one stuck pointing at it.
|
||||
// Bytes already written to storage on a rolled-back transaction
|
||||
// become orphans on disk — a future reaper can sweep them; we
|
||||
// prioritise DB consistency over storage tidiness here.
|
||||
let status = metadata
|
||||
.status
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or(repo::manga::DEFAULT_STATUS)
|
||||
.to_string();
|
||||
let alt_titles = normalize_alt_titles(metadata.alt_titles);
|
||||
let authors = normalize_string_list(metadata.authors);
|
||||
|
||||
// Transactional create. If the cover put or any relation write
|
||||
// fails, the manga row and its relations are rolled back so a
|
||||
// half-uploaded cover doesn't leave a manga without one stuck
|
||||
// pointing at it. Bytes already written to storage on a rolled-back
|
||||
// transaction become orphans on disk — a future reaper can sweep
|
||||
// them; we prioritise DB consistency over storage tidiness here.
|
||||
let mut tx = state.db.begin().await?;
|
||||
let mut manga = repo::manga::create(&mut *tx, metadata).await?;
|
||||
let mut manga = repo::manga::create(
|
||||
&mut *tx,
|
||||
metadata.title.trim(),
|
||||
&status,
|
||||
metadata.description.as_deref(),
|
||||
&alt_titles,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let author_refs = repo::author::set_for_manga(&mut *tx, manga.id, &authors).await?;
|
||||
repo::genre::set_for_manga(&mut *tx, manga.id, &metadata.genre_ids).await?;
|
||||
|
||||
if let Some(img) = cover {
|
||||
let key = format!("mangas/{}/cover.{}", manga.id, img.ext);
|
||||
@@ -112,7 +183,135 @@ async fn create(
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
Ok((StatusCode::CREATED, Json(manga)))
|
||||
// Re-read so the genres list reflects the canonical sort order
|
||||
// and any future trigger-driven defaults.
|
||||
let detail = repo::manga::get_detail(&state.db, manga.id).await?;
|
||||
let _ = author_refs; // returned for free via detail
|
||||
Ok((StatusCode::CREATED, Json(detail)))
|
||||
}
|
||||
|
||||
async fn update(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(_user): CurrentUser,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(patch): Json<MangaPatch>,
|
||||
) -> AppResult<Json<MangaDetail>> {
|
||||
// TODO(auth): until uploaders are tracked (Phase 5), any signed-in
|
||||
// user can edit any manga. Restrict to uploader + admin once that
|
||||
// column lands.
|
||||
if !repo::manga::exists(&state.db, id).await? {
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
if let Some(ref status) = patch.status {
|
||||
let trimmed = status.trim();
|
||||
if !repo::manga::STATUSES.contains(&trimmed) {
|
||||
return Err(AppError::ValidationFailed {
|
||||
message: "invalid status".into(),
|
||||
details: json!({ "status": format!("must be one of {:?}", repo::manga::STATUSES) }),
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(ref title) = patch.title {
|
||||
if title.trim().is_empty() {
|
||||
return Err(AppError::ValidationFailed {
|
||||
message: "title cannot be blank".into(),
|
||||
details: json!({ "title": "non-empty" }),
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(ref ids) = patch.genre_ids {
|
||||
validate_genre_ids(&state, ids).await?;
|
||||
}
|
||||
|
||||
let alt_titles_owned: Option<Vec<String>> =
|
||||
patch.alt_titles.map(normalize_alt_titles);
|
||||
let authors_owned: Option<Vec<String>> =
|
||||
patch.authors.map(normalize_string_list);
|
||||
|
||||
let description_provided = patch.description.is_provided();
|
||||
let description_value: Option<&str> = match &patch.description {
|
||||
Patch::Set(s) => Some(s.as_str()),
|
||||
Patch::Clear | Patch::Unchanged => None,
|
||||
};
|
||||
|
||||
let mut tx = state.db.begin().await?;
|
||||
let _updated = repo::manga::update_basics(
|
||||
&mut *tx,
|
||||
id,
|
||||
patch.title.as_deref().map(str::trim),
|
||||
patch.status.as_deref().map(str::trim),
|
||||
description_provided,
|
||||
description_value,
|
||||
alt_titles_owned.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
if let Some(ref names) = authors_owned {
|
||||
repo::author::set_for_manga(&mut *tx, id, names).await?;
|
||||
}
|
||||
if let Some(ref ids) = patch.genre_ids {
|
||||
repo::genre::set_for_manga(&mut *tx, id, ids).await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Json(repo::manga::get_detail(&state.db, id).await?))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AttachTagBody {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
async fn attach_tag(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(user): CurrentUser,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(body): Json<AttachTagBody>,
|
||||
) -> AppResult<(StatusCode, Json<TagRef>)> {
|
||||
if !repo::manga::exists(&state.db, id).await? {
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
let outcome = repo::tag::attach_to_manga(&state.db, id, &body.name, user.id).await?;
|
||||
let status = if outcome.created_attachment {
|
||||
StatusCode::CREATED
|
||||
} else {
|
||||
StatusCode::OK
|
||||
};
|
||||
// `added_by` reflects who currently owns the attachment — which is
|
||||
// the first attacher, not the caller. Look it up so the response
|
||||
// matches what subsequent list_for_manga reads will show.
|
||||
let added_by = repo::tag::find_attacher(&state.db, id, outcome.tag.id)
|
||||
.await?
|
||||
.flatten();
|
||||
Ok((
|
||||
status,
|
||||
Json(TagRef {
|
||||
id: outcome.tag.id,
|
||||
name: outcome.tag.name,
|
||||
added_by,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn detach_tag(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(user): CurrentUser,
|
||||
Path((manga_id, tag_id)): Path<(Uuid, Uuid)>,
|
||||
) -> AppResult<StatusCode> {
|
||||
match repo::tag::find_attacher(&state.db, manga_id, tag_id).await? {
|
||||
None => Err(AppError::NotFound),
|
||||
Some(added_by) => {
|
||||
// `added_by` is None if the attaching user was deleted; in
|
||||
// that case nobody owns it, so we refuse rather than let
|
||||
// any user clear orphan tags (an admin role can do that
|
||||
// later).
|
||||
if added_by != Some(user.id) {
|
||||
return Err(AppError::Forbidden);
|
||||
}
|
||||
repo::tag::detach(&state.db, manga_id, tag_id).await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_new_manga(input: &NewManga) -> AppResult<()> {
|
||||
@@ -122,9 +321,75 @@ fn validate_new_manga(input: &NewManga) -> AppResult<()> {
|
||||
details: json!({ "title": "required" }),
|
||||
});
|
||||
}
|
||||
if let Some(ref status) = input.status {
|
||||
let trimmed = status.trim();
|
||||
if !trimmed.is_empty() && !repo::manga::STATUSES.contains(&trimmed) {
|
||||
return Err(AppError::ValidationFailed {
|
||||
message: "invalid status".into(),
|
||||
details: json!({ "status": format!("must be one of {:?}", repo::manga::STATUSES) }),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn validate_genre_ids(state: &AppState, ids: &[Uuid]) -> AppResult<()> {
|
||||
if ids.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
// Dedup before counting — Postgres' `id = ANY(...)` is set-based,
|
||||
// so a request with `[g1, g1]` would match one row and trip the
|
||||
// "unknown id" branch even though the only id is real. The DB-side
|
||||
// insert path also dedupes via `ON CONFLICT DO NOTHING`, so we
|
||||
// simply silently accept duplicates from the client.
|
||||
let unique: std::collections::HashSet<Uuid> = ids.iter().copied().collect();
|
||||
let (matched,): (i64,) =
|
||||
sqlx::query_as("SELECT count(*) FROM genres WHERE id = ANY($1)")
|
||||
.bind(ids)
|
||||
.fetch_one(&state.db)
|
||||
.await?;
|
||||
if (matched as usize) != unique.len() {
|
||||
return Err(AppError::ValidationFailed {
|
||||
message: "unknown genre id".into(),
|
||||
details: json!({ "genre_ids": "all ids must reference an existing genre" }),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn normalize_alt_titles(raw: Vec<String>) -> Vec<String> {
|
||||
let mut out = Vec::with_capacity(raw.len());
|
||||
for t in raw {
|
||||
let trimmed = t.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let s = trimmed.to_string();
|
||||
if !out.contains(&s) {
|
||||
out.push(s);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn normalize_string_list(raw: Vec<String>) -> Vec<String> {
|
||||
let mut out = Vec::with_capacity(raw.len());
|
||||
for s in raw {
|
||||
let trimmed = s.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// Case-insensitive de-dup so ["Miura", "miura"] becomes one entry.
|
||||
if !out
|
||||
.iter()
|
||||
.any(|existing: &String| existing.eq_ignore_ascii_case(trimmed))
|
||||
{
|
||||
out.push(trimmed.to_string());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn parse_metadata_json(bytes: &[u8]) -> AppResult<NewManga> {
|
||||
serde_json::from_slice(bytes).map_err(|e| AppError::ValidationFailed {
|
||||
message: "metadata is not valid JSON".into(),
|
||||
|
||||
@@ -2,9 +2,11 @@ pub mod auth;
|
||||
pub mod bookmarks;
|
||||
pub mod chapters;
|
||||
pub mod files;
|
||||
pub mod genres;
|
||||
pub mod health;
|
||||
pub mod mangas;
|
||||
pub mod pagination;
|
||||
pub mod tags;
|
||||
|
||||
use axum::Router;
|
||||
|
||||
@@ -18,4 +20,6 @@ pub fn routes() -> Router<AppState> {
|
||||
.merge(files::routes())
|
||||
.merge(auth::routes())
|
||||
.merge(bookmarks::routes())
|
||||
.merge(genres::routes())
|
||||
.merge(tags::routes())
|
||||
}
|
||||
|
||||
37
backend/src/api/tags.rs
Normal file
37
backend/src/api/tags.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use axum::extract::{Query, State};
|
||||
use axum::routing::get;
|
||||
use axum::{Json, Router};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::app::AppState;
|
||||
use crate::domain::tag::Tag;
|
||||
use crate::error::AppResult;
|
||||
use crate::repo;
|
||||
|
||||
pub fn routes() -> Router<AppState> {
|
||||
Router::new().route("/tags", get(list))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ListParams {
|
||||
#[serde(default)]
|
||||
pub search: Option<String>,
|
||||
#[serde(default = "default_limit")]
|
||||
pub limit: i64,
|
||||
}
|
||||
|
||||
fn default_limit() -> i64 {
|
||||
10
|
||||
}
|
||||
|
||||
async fn list(
|
||||
State(state): State<AppState>,
|
||||
Query(params): Query<ListParams>,
|
||||
) -> AppResult<Json<Vec<Tag>>> {
|
||||
let limit = params.limit.clamp(1, 50);
|
||||
let search = params.search.as_deref().and_then(|s| {
|
||||
let t = s.trim();
|
||||
if t.is_empty() { None } else { Some(t) }
|
||||
});
|
||||
Ok(Json(repo::tag::list(&state.db, search, limit).await?))
|
||||
}
|
||||
Reference in New Issue
Block a user