commit
9c5fef1a4d
412 files changed
+94582
No files matched your search
@@ -0,0 +1,520 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import hashlib
|
||||
from datetime import timedelta
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, HTTPException, Query, status
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import String, delete, func, or_, select
|
||||
|
||||
from sakura_cloud.api.deps import CurrentUser, DbSession
|
||||
from sakura_cloud.models.artwork import Artwork
|
||||
from sakura_cloud.models.feed_cache import FeedCache
|
||||
from sakura_cloud.models.mixins import utc_now
|
||||
from sakura_cloud.models.ocr import ArtworkOcrText
|
||||
from sakura_cloud.schemas.common import ApiResponse, PageResponse, Pagination
|
||||
from sakura_cloud.schemas.gallery import ArtworkCreate, ArtworkOut, ArtworkUpdate, ArtworkViewUpdate, TagsUpdate
|
||||
from sakura_cloud.schemas.ocr import ArtworkOcrTextOut
|
||||
from sakura_cloud.services.ocr import queue_ocr_for_artwork, run_ocr_for_artwork, run_ocr_for_artwork_id
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class ArtworkBatchDeleteRequest(BaseModel):
|
||||
ids: list[str] = Field(default_factory=list, min_length=1, max_length=200)
|
||||
|
||||
|
||||
class OnlineMangaImportRequest(BaseModel):
|
||||
source_platform: str = Field(max_length=20)
|
||||
title: str = Field(min_length=1, max_length=500)
|
||||
author: str | None = Field(default=None, max_length=200)
|
||||
detail_url: str = Field(min_length=1, max_length=2048)
|
||||
cover_url: str | None = Field(default=None, max_length=2048)
|
||||
page_urls: list[str] = Field(min_length=1, max_length=300)
|
||||
headers: dict[str, str] = Field(default_factory=dict)
|
||||
tags: list[str] = Field(default_factory=list, max_length=50)
|
||||
|
||||
|
||||
def _ensure_storage_quota(current_user: CurrentUser, incoming_size: int) -> None:
|
||||
size = max(0, incoming_size)
|
||||
if size and current_user.storage_used + size > current_user.storage_limit:
|
||||
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Storage quota exceeded")
|
||||
|
||||
|
||||
def _online_source_id(value: str) -> str:
|
||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()[:32]
|
||||
|
||||
|
||||
@router.get("", response_model=ApiResponse[PageResponse[ArtworkOut]])
|
||||
def list_gallery(
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
page: int = Query(default=1, ge=1),
|
||||
limit: int = Query(default=30, ge=1, le=100),
|
||||
source_platform: str | None = None,
|
||||
author: str | None = None,
|
||||
tag: str | None = None,
|
||||
content_type: str | None = None,
|
||||
is_favorite: bool | None = None,
|
||||
search: str | None = None,
|
||||
query: str | None = None,
|
||||
search_scope: str = Query(default="all", pattern="^(title|tags|ocr|all)$"),
|
||||
sort: str = Query(default="recent", pattern="^(recent|updated)$"),
|
||||
include_total: bool = Query(default=True),
|
||||
) -> ApiResponse[PageResponse[ArtworkOut]]:
|
||||
search_text = (query or search or "").strip()
|
||||
query = select(Artwork).where(Artwork.user_id == current_user.id)
|
||||
count_query = select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id)
|
||||
filters = []
|
||||
if source_platform:
|
||||
filters.append(Artwork.source_platform == source_platform)
|
||||
if author:
|
||||
filters.append(Artwork.author.ilike(f"%{author}%"))
|
||||
if content_type:
|
||||
filters.append(Artwork.content_type == content_type)
|
||||
if is_favorite is not None:
|
||||
filters.append(Artwork.is_favorite == is_favorite)
|
||||
if search_text:
|
||||
text_filters = []
|
||||
if search_scope in {"title", "all"}:
|
||||
text_filters.append(Artwork.title.ilike(f"%{search_text}%"))
|
||||
text_filters.append(Artwork.author.ilike(f"%{search_text}%"))
|
||||
if db.bind is not None and db.bind.dialect.name == "postgresql":
|
||||
vector = func.to_tsvector(
|
||||
"simple",
|
||||
func.concat_ws(" ", Artwork.title, Artwork.author, Artwork.source_platform),
|
||||
)
|
||||
text_filters.append(vector.op("@@")(func.plainto_tsquery("simple", search_text)))
|
||||
if search_scope in {"tags", "all"}:
|
||||
text_filters.append(Artwork.tags.cast(String).ilike(f"%{search_text}%"))
|
||||
if search_scope in {"ocr", "all"}:
|
||||
ocr_match = (
|
||||
select(ArtworkOcrText.id).where(
|
||||
ArtworkOcrText.user_id == current_user.id,
|
||||
ArtworkOcrText.artwork_id == Artwork.id,
|
||||
ArtworkOcrText.status == "done",
|
||||
ArtworkOcrText.ocr_text.ilike(f"%{search_text}%"),
|
||||
)
|
||||
).exists()
|
||||
if db.bind is not None and db.bind.dialect.name == "postgresql":
|
||||
ocr_match = (
|
||||
select(ArtworkOcrText.id).where(
|
||||
ArtworkOcrText.user_id == current_user.id,
|
||||
ArtworkOcrText.artwork_id == Artwork.id,
|
||||
ArtworkOcrText.status == "done",
|
||||
func.to_tsvector("simple", ArtworkOcrText.ocr_text).op("@@")(
|
||||
func.plainto_tsquery("simple", search_text)
|
||||
),
|
||||
)
|
||||
).exists()
|
||||
text_filters.append(ocr_match)
|
||||
if text_filters:
|
||||
filters.append(or_(*text_filters))
|
||||
elif db.bind is not None and db.bind.dialect.name == "postgresql":
|
||||
vector = func.to_tsvector(
|
||||
"simple",
|
||||
func.concat_ws(" ", Artwork.title, Artwork.author, Artwork.source_platform),
|
||||
)
|
||||
filters.append(vector.op("@@")(func.plainto_tsquery("simple", search_text)))
|
||||
if tag:
|
||||
filters.append(Artwork.tags.contains([tag]))
|
||||
for item in filters:
|
||||
query = query.where(item)
|
||||
count_query = count_query.where(item)
|
||||
order_by = (
|
||||
(Artwork.updated_at.desc(), Artwork.created_at.desc())
|
||||
if sort == "updated"
|
||||
else (Artwork.last_viewed_at.desc().nullslast(), Artwork.updated_at.desc())
|
||||
)
|
||||
rows = db.scalars(query.order_by(*order_by).offset((page - 1) * limit).limit(limit)).all()
|
||||
if include_total:
|
||||
total = int(db.scalar(count_query) or 0)
|
||||
pages = math.ceil(total / limit) if total else 0
|
||||
else:
|
||||
has_more = len(rows) >= limit
|
||||
total = (page - 1) * limit + len(rows) + (1 if has_more else 0)
|
||||
pages = page + 1 if has_more else page
|
||||
if search_text:
|
||||
_annotate_gallery_matches(db, rows, current_user.id, search_text, search_scope)
|
||||
data = PageResponse(
|
||||
items=[ArtworkOut.model_validate(row) for row in rows],
|
||||
pagination=Pagination(page=page, limit=limit, total=total, pages=pages),
|
||||
)
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/feed", response_model=ApiResponse[list[ArtworkOut]])
|
||||
def gallery_feed(
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
limit: int = Query(default=30, ge=1, le=100),
|
||||
mode: str = Query(default="created", pattern="^(created|recent)$"),
|
||||
) -> ApiResponse[list[ArtworkOut]]:
|
||||
cache_key = f"gallery_feed:{mode}:{limit}"
|
||||
now = utc_now()
|
||||
cached = db.scalar(
|
||||
select(FeedCache).where(
|
||||
FeedCache.user_id == current_user.id,
|
||||
FeedCache.cache_key == cache_key,
|
||||
FeedCache.expires_at > now,
|
||||
)
|
||||
)
|
||||
if cached is not None:
|
||||
return ApiResponse(data=[ArtworkOut.model_validate(item) for item in cached.payload])
|
||||
|
||||
order_by = Artwork.last_viewed_at.desc() if mode == "recent" else Artwork.created_at.desc()
|
||||
where = [Artwork.user_id == current_user.id]
|
||||
if mode == "recent":
|
||||
where.append(Artwork.last_viewed_at.is_not(None))
|
||||
rows = db.scalars(
|
||||
select(Artwork).where(*where).order_by(order_by).limit(limit)
|
||||
).all()
|
||||
data = [ArtworkOut.model_validate(row) for row in rows]
|
||||
payload = [item.model_dump(mode="json") for item in data]
|
||||
existing = db.scalar(
|
||||
select(FeedCache).where(
|
||||
FeedCache.user_id == current_user.id,
|
||||
FeedCache.cache_key == cache_key,
|
||||
)
|
||||
)
|
||||
if existing is None:
|
||||
db.add(
|
||||
FeedCache(
|
||||
user_id=current_user.id,
|
||||
cache_key=cache_key,
|
||||
payload=payload,
|
||||
expires_at=now + timedelta(minutes=5),
|
||||
)
|
||||
)
|
||||
else:
|
||||
existing.payload = payload
|
||||
existing.expires_at = now + timedelta(minutes=5)
|
||||
db.commit()
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/search", response_model=ApiResponse[PageResponse[ArtworkOut]])
|
||||
def search_gallery(
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
q: str = Query(min_length=1),
|
||||
page: int = Query(default=1, ge=1),
|
||||
limit: int = Query(default=30, ge=1, le=100),
|
||||
) -> ApiResponse[PageResponse[ArtworkOut]]:
|
||||
return list_gallery(current_user=current_user, db=db, page=page, limit=limit, search=q, search_scope="all")
|
||||
|
||||
|
||||
@router.post("/import-online", response_model=ApiResponse[list[ArtworkOut]], status_code=status.HTTP_201_CREATED)
|
||||
def import_online_manga(
|
||||
payload: OnlineMangaImportRequest,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[list[ArtworkOut]]:
|
||||
series_id = _online_source_id(payload.detail_url)
|
||||
existing = db.scalars(
|
||||
select(Artwork)
|
||||
.where(
|
||||
Artwork.user_id == current_user.id,
|
||||
Artwork.source_platform == payload.source_platform,
|
||||
Artwork.source_id.like(f"{series_id}:%"),
|
||||
Artwork.content_type == "manga",
|
||||
)
|
||||
.order_by(Artwork.title.asc())
|
||||
).all()
|
||||
if existing:
|
||||
return ApiResponse(data=[ArtworkOut.model_validate(item) for item in existing])
|
||||
|
||||
safe_headers = {
|
||||
key: value
|
||||
for key, value in payload.headers.items()
|
||||
if key.lower() in {"user-agent", "referer", "cookie", "accept", "accept-language"} and value
|
||||
}
|
||||
rows: list[Artwork] = []
|
||||
page_count = len(payload.page_urls)
|
||||
for index, page_url in enumerate(payload.page_urls, start=1):
|
||||
url = page_url.strip()
|
||||
if not url.startswith(("http://", "https://")):
|
||||
continue
|
||||
artwork = Artwork(
|
||||
user_id=current_user.id,
|
||||
source_id=f"{series_id}:{index:04d}",
|
||||
source_platform=payload.source_platform,
|
||||
content_type="manga",
|
||||
title=f"{payload.title.strip()} #{index:03d}",
|
||||
author=payload.author,
|
||||
page_count=page_count,
|
||||
file_path=url,
|
||||
file_size=0,
|
||||
thumbnail_path=payload.cover_url or url,
|
||||
mime_type="image/jpeg",
|
||||
tags=payload.tags,
|
||||
artwork_metadata={
|
||||
"online_import": True,
|
||||
"detail_url": payload.detail_url,
|
||||
"series_id": series_id,
|
||||
"page_index": index,
|
||||
"headers": safe_headers,
|
||||
},
|
||||
)
|
||||
db.add(artwork)
|
||||
rows.append(artwork)
|
||||
if not rows:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No valid manga page URL")
|
||||
db.commit()
|
||||
for row in rows:
|
||||
db.refresh(row)
|
||||
_clear_gallery_feed_cache(db, current_user.id)
|
||||
return ApiResponse(code=201, message="created", data=[ArtworkOut.model_validate(item) for item in rows])
|
||||
|
||||
|
||||
@router.get("/stat", response_model=ApiResponse[dict[str, int]])
|
||||
def gallery_stat(current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, int]]:
|
||||
total = int(db.scalar(select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id)) or 0)
|
||||
favorites = int(
|
||||
db.scalar(
|
||||
select(func.count()).select_from(Artwork).where(Artwork.user_id == current_user.id, Artwork.is_favorite == True)
|
||||
)
|
||||
or 0
|
||||
)
|
||||
return ApiResponse(data={"total": total, "favorites": favorites, "storage_used": current_user.storage_used})
|
||||
|
||||
|
||||
@router.post("", response_model=ApiResponse[ArtworkOut], status_code=status.HTTP_201_CREATED)
|
||||
def create_artwork(
|
||||
payload: ArtworkCreate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
background_tasks: BackgroundTasks,
|
||||
) -> ApiResponse[ArtworkOut]:
|
||||
_ensure_storage_quota(current_user, payload.file_size)
|
||||
artwork = Artwork(
|
||||
user_id=current_user.id,
|
||||
source_id=payload.source_id,
|
||||
source_platform=payload.source_platform,
|
||||
content_type=payload.content_type,
|
||||
title=payload.title,
|
||||
author=payload.author,
|
||||
author_id=payload.author_id,
|
||||
page_count=payload.page_count,
|
||||
file_path=payload.file_path,
|
||||
file_size=payload.file_size,
|
||||
thumbnail_path=payload.thumbnail_path,
|
||||
mime_type=payload.mime_type,
|
||||
width=payload.width,
|
||||
height=payload.height,
|
||||
tags=payload.tags,
|
||||
artwork_metadata=payload.metadata,
|
||||
)
|
||||
db.add(artwork)
|
||||
current_user.storage_used += max(0, payload.file_size)
|
||||
db.commit()
|
||||
db.refresh(artwork)
|
||||
if queue_ocr_for_artwork(db, artwork) is not None:
|
||||
background_tasks.add_task(run_ocr_for_artwork_id, artwork.id, current_user.id)
|
||||
_clear_gallery_feed_cache(db, current_user.id)
|
||||
return ApiResponse(code=201, message="created", data=ArtworkOut.model_validate(artwork))
|
||||
|
||||
|
||||
@router.get("/tags", response_model=ApiResponse[list[str]])
|
||||
def list_tags(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[str]]:
|
||||
rows = db.scalars(select(Artwork.tags).where(Artwork.user_id == current_user.id)).all()
|
||||
tags = sorted({tag for row in rows for tag in (row or [])})
|
||||
return ApiResponse(data=tags)
|
||||
|
||||
|
||||
@router.get("/{artwork_id}/ocr", response_model=ApiResponse[ArtworkOcrTextOut | None])
|
||||
def get_artwork_ocr(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOcrTextOut | None]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
row = db.scalar(
|
||||
select(ArtworkOcrText).where(
|
||||
ArtworkOcrText.user_id == current_user.id,
|
||||
ArtworkOcrText.artwork_id == artwork_id,
|
||||
)
|
||||
)
|
||||
return ApiResponse(data=ArtworkOcrTextOut.model_validate(row) if row is not None else None)
|
||||
|
||||
|
||||
@router.post("/{artwork_id}/ocr/retry", response_model=ApiResponse[ArtworkOcrTextOut])
|
||||
def retry_artwork_ocr(
|
||||
artwork_id: str,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
background_tasks: BackgroundTasks,
|
||||
) -> ApiResponse[ArtworkOcrTextOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
row = queue_ocr_for_artwork(db, artwork)
|
||||
if row is None:
|
||||
row = run_ocr_for_artwork(db, artwork)
|
||||
else:
|
||||
background_tasks.add_task(run_ocr_for_artwork_id, artwork.id, current_user.id)
|
||||
db.refresh(row)
|
||||
return ApiResponse(data=ArtworkOcrTextOut.model_validate(row))
|
||||
|
||||
|
||||
@router.get("/{artwork_id}", response_model=ApiResponse[ArtworkOut])
|
||||
def get_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
return ApiResponse(data=ArtworkOut.model_validate(artwork))
|
||||
|
||||
|
||||
def _annotate_gallery_matches(
|
||||
db: DbSession,
|
||||
rows: list[Artwork],
|
||||
user_id: str,
|
||||
search_text: str,
|
||||
search_scope: str,
|
||||
) -> None:
|
||||
lowered = search_text.lower()
|
||||
artwork_ids = [row.id for row in rows]
|
||||
ocr_hits: set[str] = set()
|
||||
if artwork_ids and search_scope in {"ocr", "all"}:
|
||||
ocr_hits = set(
|
||||
db.scalars(
|
||||
select(ArtworkOcrText.artwork_id).where(
|
||||
ArtworkOcrText.user_id == user_id,
|
||||
ArtworkOcrText.artwork_id.in_(artwork_ids),
|
||||
ArtworkOcrText.status == "done",
|
||||
ArtworkOcrText.ocr_text.ilike(f"%{search_text}%"),
|
||||
)
|
||||
).all()
|
||||
)
|
||||
for row in rows:
|
||||
title_hit = lowered in (row.title or "").lower() or lowered in (row.author or "").lower()
|
||||
tag_hit = any(lowered in str(tag).lower() for tag in (row.tags or []))
|
||||
if row.id in ocr_hits:
|
||||
row.matched_by = "ocr"
|
||||
elif title_hit:
|
||||
row.matched_by = "title"
|
||||
elif tag_hit:
|
||||
row.matched_by = "tags"
|
||||
else:
|
||||
row.matched_by = None
|
||||
|
||||
|
||||
def _clear_gallery_feed_cache(db: DbSession, user_id: str) -> None:
|
||||
db.execute(
|
||||
delete(FeedCache).where(
|
||||
FeedCache.user_id == user_id,
|
||||
FeedCache.cache_key.like("gallery_feed:%"),
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
@router.delete("/{artwork_id}", response_model=ApiResponse[dict[str, bool]])
|
||||
def delete_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
current_user.storage_used = max(0, current_user.storage_used - max(0, artwork.file_size))
|
||||
db.delete(artwork)
|
||||
db.commit()
|
||||
return ApiResponse(data={"ok": True})
|
||||
|
||||
|
||||
@router.post("/batch-delete", response_model=ApiResponse[dict[str, int]])
|
||||
def batch_delete_artworks(
|
||||
payload: ArtworkBatchDeleteRequest,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[dict[str, int]]:
|
||||
ids = sorted({item.strip() for item in payload.ids if item.strip()})
|
||||
if not ids:
|
||||
return ApiResponse(data={"deleted": 0})
|
||||
|
||||
rows = db.scalars(
|
||||
select(Artwork).where(
|
||||
Artwork.user_id == current_user.id,
|
||||
Artwork.id.in_(ids),
|
||||
)
|
||||
).all()
|
||||
deleted = 0
|
||||
released = 0
|
||||
for artwork in rows:
|
||||
released += max(0, artwork.file_size)
|
||||
db.delete(artwork)
|
||||
deleted += 1
|
||||
if deleted:
|
||||
current_user.storage_used = max(0, current_user.storage_used - released)
|
||||
db.commit()
|
||||
return ApiResponse(data={"deleted": deleted})
|
||||
|
||||
|
||||
@router.post("/{artwork_id}/favorite", response_model=ApiResponse[ArtworkOut])
|
||||
def favorite_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
artwork.is_favorite = True
|
||||
db.commit()
|
||||
db.refresh(artwork)
|
||||
return ApiResponse(data=ArtworkOut.model_validate(artwork))
|
||||
|
||||
|
||||
@router.delete("/{artwork_id}/favorite", response_model=ApiResponse[ArtworkOut])
|
||||
def unfavorite_artwork(artwork_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[ArtworkOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
artwork.is_favorite = False
|
||||
db.commit()
|
||||
db.refresh(artwork)
|
||||
return ApiResponse(data=ArtworkOut.model_validate(artwork))
|
||||
|
||||
|
||||
@router.put("/{artwork_id}/tags", response_model=ApiResponse[ArtworkOut])
|
||||
def update_artwork_tags(
|
||||
artwork_id: str,
|
||||
payload: TagsUpdate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[ArtworkOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
artwork.tags = sorted(set(payload.tags))
|
||||
db.commit()
|
||||
db.refresh(artwork)
|
||||
return ApiResponse(data=ArtworkOut.model_validate(artwork))
|
||||
|
||||
|
||||
@router.put("/{artwork_id}", response_model=ApiResponse[ArtworkOut])
|
||||
def update_artwork(
|
||||
artwork_id: str,
|
||||
payload: ArtworkUpdate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[ArtworkOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
artwork.title = payload.title.strip()
|
||||
artwork.tags = sorted({item.strip() for item in payload.tags if item.strip()})
|
||||
db.commit()
|
||||
db.refresh(artwork)
|
||||
return ApiResponse(data=ArtworkOut.model_validate(artwork))
|
||||
|
||||
|
||||
@router.post("/{artwork_id}/view", response_model=ApiResponse[ArtworkOut])
|
||||
def mark_artwork_viewed(
|
||||
artwork_id: str,
|
||||
payload: ArtworkViewUpdate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
) -> ApiResponse[ArtworkOut]:
|
||||
artwork = db.get(Artwork, artwork_id)
|
||||
if artwork is None or artwork.user_id != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Artwork not found")
|
||||
if payload.viewed:
|
||||
artwork.last_viewed_at = utc_now()
|
||||
db.commit()
|
||||
db.refresh(artwork)
|
||||
return ApiResponse(data=ArtworkOut.model_validate(artwork))
|
||||
Reference in new issue
Block a user