From ecf14bda69e060bee01824ac0e3688e47de51322 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Tue, 27 Jan 2026 22:27:41 +1100 Subject: [PATCH] Add chores API endpoints with CRUD operations --- backend/app/api/v1/chores.py | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 backend/app/api/v1/chores.py diff --git a/backend/app/api/v1/chores.py b/backend/app/api/v1/chores.py new file mode 100644 index 0000000..7c09fe3 --- /dev/null +++ b/backend/app/api/v1/chores.py @@ -0,0 +1,105 @@ +"""Chores API endpoints.""" +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session +from typing import List +from datetime import datetime + +from app.core.database import get_db +from app.core.security import get_current_user +from app.models.user import User +from app.models.chore import Chore, ChoreStatus +from app.schemas import chore as chore_schemas + + +router = APIRouter() + + +@router.get("", response_model=List[chore_schemas.Chore]) +def get_chores( + skip: int = 0, + limit: int = 100, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Get all chores.""" + chores = db.query(Chore).offset(skip).limit(limit).all() + return chores + + +@router.get("/{chore_id}", response_model=chore_schemas.Chore) +def get_chore( + chore_id: int, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Get a specific chore by ID.""" + chore = db.query(Chore).filter(Chore.id == chore_id).first() + if not chore: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Chore not found" + ) + return chore + + +@router.post("", response_model=chore_schemas.Chore, status_code=status.HTTP_201_CREATED) +def create_chore( + chore_in: chore_schemas.ChoreCreate, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Create a new chore.""" + chore = Chore(**chore_in.model_dump()) + db.add(chore) + db.commit() + db.refresh(chore) + return chore + + +@router.put("/{chore_id}", response_model=chore_schemas.Chore) +def update_chore( + chore_id: int, + chore_in: chore_schemas.ChoreUpdate, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Update a chore.""" + chore = db.query(Chore).filter(Chore.id == chore_id).first() + if not chore: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Chore not found" + ) + + # Update fields + update_data = chore_in.model_dump(exclude_unset=True) + + # If status is being changed to completed, set completed_at + if update_data.get("status") == ChoreStatus.COMPLETED and chore.status != ChoreStatus.COMPLETED: + update_data["completed_at"] = datetime.utcnow() + + for field, value in update_data.items(): + setattr(chore, field, value) + + db.commit() + db.refresh(chore) + return chore + + +@router.delete("/{chore_id}", status_code=status.HTTP_204_NO_CONTENT) +def delete_chore( + chore_id: int, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user) +): + """Delete a chore.""" + chore = db.query(Chore).filter(Chore.id == chore_id).first() + if not chore: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Chore not found" + ) + + db.delete(chore) + db.commit() + return None