67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
"""Artifact endpoints."""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.database import get_db
|
|
from app.schemas.artifact import ArtifactCreate, ArtifactResponse
|
|
from app.models.artifact import Artifact
|
|
from app.api.deps import get_current_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter(prefix="/artifacts")
|
|
|
|
|
|
@router.get("", response_model=List[ArtifactResponse])
|
|
def list_artifacts(
|
|
task_id: int = None,
|
|
project_id: int = None,
|
|
skip: int = 0, limit: int = 100,
|
|
user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
q = db.query(Artifact).filter(Artifact.tenant_id == user.tenant_id)
|
|
if task_id:
|
|
q = q.filter(Artifact.task_id == task_id)
|
|
if project_id:
|
|
q = q.filter(Artifact.project_id == project_id)
|
|
return q.order_by(Artifact.created_at.desc()).offset(skip).limit(limit).all()
|
|
|
|
|
|
@router.get("/{artifact_id}", response_model=ArtifactResponse)
|
|
def get_artifact(
|
|
artifact_id: int,
|
|
user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
a = db.query(Artifact).filter(
|
|
Artifact.id == artifact_id,
|
|
Artifact.tenant_id == user.tenant_id,
|
|
).first()
|
|
if not a:
|
|
raise HTTPException(status_code=404, detail="Artifact not found")
|
|
return a
|
|
|
|
|
|
@router.post("", response_model=ArtifactResponse, status_code=201)
|
|
def create_artifact(
|
|
req: ArtifactCreate,
|
|
task_id: int,
|
|
project_id: int,
|
|
user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
artifact = Artifact(
|
|
tenant_id=user.tenant_id,
|
|
task_id=task_id,
|
|
project_id=project_id,
|
|
title=req.title,
|
|
content=req.content,
|
|
artifact_type=req.artifact_type,
|
|
sources=req.sources,
|
|
created_by_user_id=user.id,
|
|
)
|
|
db.add(artifact)
|
|
db.commit()
|
|
db.refresh(artifact)
|
|
return artifact
|