Files
Meal-Planner/backend/app/api/profile.py
T
admin c735d21661 feat: implement Phase 2 - Alembic migrations, Pydantic schemas, and real API endpoints
- Add initial Alembic migration with full PostgreSQL schema (enums, tables, indexes, constraints)
- Add seed data migration with basic ingredients (70+) and family profile
- Add Pydantic schemas for all models (FamilyProfile, Recipe, MealPlan, etc.)
- Implement /api/profile endpoints (CRUD, family member management)
- Implement /api/recipes endpoints (CRUD, ingredients, filtering)
- Implement /api/meals endpoints (meal plans, voting, approval tokens)
- Implement /api/pantry endpoints (CRUD for home pantry)
- Implement /api/shopping-list endpoints (aggregation, print-ready HTML)
- Implement /api/admin endpoints (scrape trigger, logs, stats)
- Update ORIENTATION.md with Phase 2 progress
2026-05-04 20:21:20 -07:00

80 lines
2.8 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import FamilyProfile, FamilyMember, DayOfWeek, MealType, MealPlanStatus, MealPlanItemStatus, ApprovalTokenStatus, FamilyMemberRole, DenialReason, NeverSuggestReason, ScrapeStatus, EmailStatus
from app.schemas import (
FamilyProfileResponse, FamilyProfileUpdate, FamilyProfileCreate,
FamilyMemberResponse, FamilyMemberCreate
)
from uuid import UUID
from typing import List
router = APIRouter()
@router.get("/", response_model=FamilyProfileResponse)
def get_profile(db: Session = Depends(get_db)):
profile = db.query(FamilyProfile).first()
if not profile:
raise HTTPException(status_code=404, detail="Family profile not found")
return profile
@router.put("/", response_model=FamilyProfileResponse)
def update_profile(update: FamilyProfileUpdate, db: Session = Depends(get_db)):
profile = db.query(FamilyProfile).first()
if not profile:
raise HTTPException(status_code=404, detail="Family profile not found")
update_data = update.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(profile, field, value)
db.commit()
db.refresh(profile)
return profile
@router.get("/members", response_model=List[FamilyMemberResponse])
def get_members(db: Session = Depends(get_db)):
profile = db.query(FamilyProfile).first()
if not profile:
raise HTTPException(status_code=404, detail="Family profile not found")
return profile.members
@router.post("/members", response_model=FamilyMemberResponse)
def add_member(member: FamilyMemberCreate, db: Session = Depends(get_db)):
profile = db.query(FamilyProfile).first()
if not profile:
raise HTTPException(status_code=404, detail="Family profile not found")
existing = db.query(FamilyMember).filter(
FamilyMember.family_profile_id == profile.id,
FamilyMember.email == member.email
).first()
if existing:
raise HTTPException(status_code=400, detail="Member with this email already exists")
db_member = FamilyMember(
family_profile_id=profile.id,
name=member.name,
email=member.email,
role=FamilyMemberRole[member.role.value.upper()],
likes_mushrooms=member.likes_mushrooms
)
db.add(db_member)
db.commit()
db.refresh(db_member)
return db_member
@router.delete("/members/{member_id}")
def delete_member(member_id: UUID, db: Session = Depends(get_db)):
member = db.query(FamilyMember).filter(FamilyMember.id == member_id).first()
if not member:
raise HTTPException(status_code=404, detail="Family member not found")
db.delete(member)
db.commit()
return {"message": "Member deleted"}