Automated Action 980400187c Create FastAPI backend for communications agency website
- Set up project structure with FastAPI application
- Implement SQLAlchemy models for users, services, projects, team members, contacts
- Create API endpoints for website functionality
- Implement JWT authentication system with user roles
- Add file upload functionality for media
- Configure CORS and health check endpoints
- Add database migrations with Alembic
- Create comprehensive README with setup instructions
2025-06-10 11:00:53 +00:00

114 lines
2.9 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app import crud, models, schemas
from app.api import deps
router = APIRouter()
@router.get("/", response_model=List[schemas.Project])
def read_projects(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
) -> Any:
"""
Retrieve projects.
"""
projects = crud.project.get_active(db, skip=skip, limit=limit)
return projects
@router.get("/featured", response_model=List[schemas.Project])
def read_featured_projects(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
) -> Any:
"""
Retrieve featured projects.
"""
projects = crud.project.get_featured(db, skip=skip, limit=limit)
return projects
@router.post("/", response_model=schemas.Project)
def create_project(
*,
db: Session = Depends(deps.get_db),
project_in: schemas.ProjectCreate,
current_user: models.User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Create new project.
"""
project = crud.project.get_by_slug(db, slug=project_in.slug)
if project:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="A project with this slug already exists",
)
project = crud.project.create(db, obj_in=project_in)
return project
@router.get("/{slug}", response_model=schemas.Project)
def read_project(
*,
db: Session = Depends(deps.get_db),
slug: str,
) -> Any:
"""
Get project by slug.
"""
project = crud.project.get_by_slug(db, slug=slug)
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found",
)
return project
@router.put("/{id}", response_model=schemas.Project)
def update_project(
*,
db: Session = Depends(deps.get_db),
id: int,
project_in: schemas.ProjectUpdate,
current_user: models.User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Update a project.
"""
project = crud.project.get(db, id=id)
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found",
)
project = crud.project.update(db, db_obj=project, obj_in=project_in)
return project
@router.delete("/{id}", response_model=schemas.Project)
def delete_project(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: models.User = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Delete a project.
"""
project = crud.project.get(db, id=id)
if not project:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found",
)
project = crud.project.remove(db, id=id)
return project