企業內部新聞彙整與分析系統 - 自動新聞抓取 (Digitimes, 經濟日報, 工商時報) - AI 智慧摘要 (OpenAI/Claude/Ollama) - 群組管理與訂閱通知 - 已清理 Python 快取檔案 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
2.4 KiB
Python
92 lines
2.4 KiB
Python
"""
|
|
訂閱管理 API 端點
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
|
|
from app.db.session import get_db
|
|
from app.models import User, Group, Subscription
|
|
from app.api.v1.endpoints.auth import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class SubscriptionResponse(BaseModel):
|
|
group_id: int
|
|
group_name: str
|
|
category: str
|
|
email_notify: bool
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class SubscriptionItem(BaseModel):
|
|
group_id: int
|
|
subscribed: bool
|
|
email_notify: Optional[bool] = True
|
|
|
|
|
|
class SubscriptionUpdateRequest(BaseModel):
|
|
subscriptions: list[SubscriptionItem]
|
|
|
|
|
|
@router.get("", response_model=list[SubscriptionResponse])
|
|
def get_my_subscriptions(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""取得我的訂閱列表"""
|
|
subs = db.query(Subscription).filter(Subscription.user_id == current_user.id).all()
|
|
|
|
result = []
|
|
for s in subs:
|
|
group = db.query(Group).filter(Group.id == s.group_id).first()
|
|
if group:
|
|
result.append(SubscriptionResponse(
|
|
group_id=group.id,
|
|
group_name=group.name,
|
|
category=group.category.value,
|
|
email_notify=s.email_notify
|
|
))
|
|
|
|
return result
|
|
|
|
|
|
@router.put("")
|
|
def update_subscriptions(
|
|
request: SubscriptionUpdateRequest,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""批次更新訂閱"""
|
|
for item in request.subscriptions:
|
|
# 檢查群組是否存在
|
|
group = db.query(Group).filter(Group.id == item.group_id, Group.is_active == True).first()
|
|
if not group:
|
|
continue
|
|
|
|
existing = db.query(Subscription).filter(
|
|
Subscription.user_id == current_user.id,
|
|
Subscription.group_id == item.group_id
|
|
).first()
|
|
|
|
if item.subscribed:
|
|
if existing:
|
|
existing.email_notify = item.email_notify
|
|
else:
|
|
sub = Subscription(
|
|
user_id=current_user.id,
|
|
group_id=item.group_id,
|
|
email_notify=item.email_notify
|
|
)
|
|
db.add(sub)
|
|
else:
|
|
if existing:
|
|
db.delete(existing)
|
|
|
|
db.commit()
|
|
return {"message": "訂閱更新成功"}
|