Files
diary-news/backend/app/api/bookmarks.py
Mavis 60b062daf2 feat: initial MVP - FastAPI backend + Vue3 frontend + docker-compose
- backend: FastAPI + SQLAlchemy 2.0(async) + asyncpg + Alembic
- 7 API routes: auth/me/articles/sources/bookmarks/subscriptions/admin
- models: User/Source/Article/Bookmark/Subscription/ApiToken
- services: RSS fetcher (feedparser) + Tencent TMT translator with quota + cache + local NLLB fallback
- workers: APScheduler + asyncio pipeline (fetch -> dedupe -> insert -> translate)
- seed scripts: create_user, seed_sources (5 RSS: Reuters/BBC/Al Jazeera/NHK/DW)
- frontend: Vue 3 + Vite + Naive UI + Pinia + vue-router
- pages: Login, Feed (24h), ArticleDetail, Sources, Bookmarks, AdminSources
- deploy: docker-compose (postgres/redis/api/worker/frontend/caddy)
- docs: README, DEPLOY, architecture, acceptance
2026-06-07 21:51:01 +08:00

74 lines
2.3 KiB
Python

"""/bookmarks 收藏。"""
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_current_user
from app.database import get_session
from app.models.article import Article
from app.models.bookmark import Bookmark
from app.models.user import User
from app.schemas.misc import BookmarkIn, BookmarkOut
router = APIRouter(prefix="/bookmarks", tags=["bookmarks"])
@router.post("", response_model=BookmarkOut, status_code=status.HTTP_201_CREATED)
async def add(
body: BookmarkIn,
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
):
art = (await session.execute(select(Article).where(Article.id == body.article_id))).scalar_one_or_none()
if not art:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Article not found")
# 已存在则直接返回
existing = (
await session.execute(
select(Bookmark).where(
Bookmark.user_id == user.id, Bookmark.article_id == body.article_id
)
)
).scalar_one_or_none()
if existing:
return BookmarkOut.model_validate(existing)
bm = Bookmark(user_id=user.id, article_id=body.article_id, note=body.note)
session.add(bm)
await session.commit()
await session.refresh(bm)
return BookmarkOut.model_validate(bm)
@router.delete("/{article_id}", status_code=status.HTTP_204_NO_CONTENT)
async def remove(
article_id: int,
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
):
bm = (
await session.execute(
select(Bookmark).where(
Bookmark.user_id == user.id, Bookmark.article_id == article_id
)
)
).scalar_one_or_none()
if bm:
await session.delete(bm)
await session.commit()
return None
@router.get("", response_model=list[BookmarkOut])
async def list_mine(
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
):
rows = (
await session.execute(
select(Bookmark).where(Bookmark.user_id == user.id).order_by(Bookmark.created_at.desc())
)
).scalars()
return [BookmarkOut.model_validate(b) for b in rows]