feat: Update endpoint project
This commit is contained in:
parent
0c629b31b9
commit
5e63fe5bd7
@ -0,0 +1,85 @@
|
|||||||
|
from typing import Optional
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models import Project
|
||||||
|
from schemas import ProjectCreate, ProjectUpdate
|
||||||
|
from dependencies import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/projects", response_model=Project)
|
||||||
|
async def create_project(
|
||||||
|
project: ProjectCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: Optional[User] = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Create a new project.
|
||||||
|
"""
|
||||||
|
new_project = Project(**project.dict(), owner_id=current_user.id)
|
||||||
|
db.add(new_project)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(new_project)
|
||||||
|
return new_project
|
||||||
|
|
||||||
|
@router.get("/projects", response_model=List[Project])
|
||||||
|
async def get_projects(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: Optional[User] = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get all projects for the current user.
|
||||||
|
"""
|
||||||
|
projects = db.query(Project).filter(Project.owner_id == current_user.id).all()
|
||||||
|
return projects
|
||||||
|
|
||||||
|
@router.get("/projects/{project_id}", response_model=Project)
|
||||||
|
async def get_project(
|
||||||
|
project_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: Optional[User] = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get a specific project by ID.
|
||||||
|
"""
|
||||||
|
project = db.query(Project).filter(Project.id == project_id, Project.owner_id == current_user.id).first()
|
||||||
|
if not project:
|
||||||
|
raise HTTPException(status_code=404, detail="Project not found")
|
||||||
|
return project
|
||||||
|
|
||||||
|
@router.put("/projects/{project_id}", response_model=Project)
|
||||||
|
async def update_project(
|
||||||
|
project_id: int,
|
||||||
|
project: ProjectUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: Optional[User] = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Update a project by ID.
|
||||||
|
"""
|
||||||
|
db_project = db.query(Project).filter(Project.id == project_id, Project.owner_id == current_user.id).first()
|
||||||
|
if not db_project:
|
||||||
|
raise HTTPException(status_code=404, detail="Project not found")
|
||||||
|
update_data = project.dict(exclude_unset=True)
|
||||||
|
for key, value in update_data.items():
|
||||||
|
setattr(db_project, key, value)
|
||||||
|
db.add(db_project)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_project)
|
||||||
|
return db_project
|
||||||
|
|
||||||
|
@router.delete("/projects/{project_id}", response_model=Project)
|
||||||
|
async def delete_project(
|
||||||
|
project_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: Optional[User] = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Delete a project by ID.
|
||||||
|
"""
|
||||||
|
project = db.query(Project).filter(Project.id == project_id, Project.owner_id == current_user.id).first()
|
||||||
|
if not project:
|
||||||
|
raise HTTPException(status_code=404, detail="Project not found")
|
||||||
|
db.delete(project)
|
||||||
|
db.commit()
|
||||||
|
return project
|
Loading…
x
Reference in New Issue
Block a user