123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- """
- 研究历史API路由
- """
- from fastapi import APIRouter, Depends, HTTPException, status
- from sqlalchemy.orm import Session
- from pydantic import BaseModel
- from typing import List, Dict, Any, Optional
- from datetime import datetime
- from backend.core.database import get_db
- from backend.core.models import User, ResearchHistory
- from backend.core.auth import get_current_user
- router = APIRouter(tags=["research"], prefix="/research-history")
- class ResearchHistoryCreate(BaseModel):
- title: str
- research_intent: str
- keywords: List[str]
- directions: List[Dict[str, Any]]
- papers: List[Dict[str, Any]]
-
- class ResearchHistoryResponse(BaseModel):
- id: int
- title: str
- research_intent: str
- keywords: List[str]
- directions: List[Dict[str, Any]]
- papers: List[Dict[str, Any]]
- created_at: datetime
- updated_at: datetime
-
- class Config:
- orm_mode = True
-
- class ResearchHistorySummary(BaseModel):
- id: int
- title: str
- created_at: datetime
- updated_at: datetime
-
- class Config:
- orm_mode = True
- @router.post("/", response_model=ResearchHistoryResponse)
- async def create_research_history(
- research_data: ResearchHistoryCreate,
- current_user: User = Depends(get_current_user),
- db: Session = Depends(get_db)
- ):
- """创建新的研究历史"""
- research = ResearchHistory(
- user_id=current_user.id,
- title=research_data.title,
- research_intent=research_data.research_intent,
- keywords=research_data.keywords,
- directions=research_data.directions,
- papers=research_data.papers
- )
-
- db.add(research)
- db.commit()
- db.refresh(research)
-
- return research
- @router.get("/", response_model=List[ResearchHistorySummary])
- async def get_research_histories(
- current_user: User = Depends(get_current_user),
- db: Session = Depends(get_db)
- ):
- """获取用户的所有研究历史摘要"""
- researches = db.query(ResearchHistory)\
- .filter(ResearchHistory.user_id == current_user.id)\
- .order_by(ResearchHistory.updated_at.desc())\
- .all()
-
- return researches
- @router.get("/{research_id}", response_model=ResearchHistoryResponse)
- async def get_research_history(
- research_id: int,
- current_user: User = Depends(get_current_user),
- db: Session = Depends(get_db)
- ):
- """获取特定研究历史的详情"""
- research = db.query(ResearchHistory)\
- .filter(ResearchHistory.id == research_id, ResearchHistory.user_id == current_user.id)\
- .first()
-
- if not research:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="研究历史不存在或无权访问"
- )
-
- return research
- @router.delete("/{research_id}")
- async def delete_research_history(
- research_id: int,
- current_user: User = Depends(get_current_user),
- db: Session = Depends(get_db)
- ):
- """删除研究历史"""
- research = db.query(ResearchHistory)\
- .filter(ResearchHistory.id == research_id, ResearchHistory.user_id == current_user.id)\
- .first()
-
- if not research:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="研究历史不存在或无权访问"
- )
-
- db.delete(research)
- db.commit()
-
- return {"message": "研究历史已删除"}
|