
Features: - User registration and authentication with JWT tokens - Multi-level admin access (Admin and Super Admin) - Gym management with membership plans - Subscription management with payment integration - Stripe and Paystack payment gateway support - Role-based access control - SQLite database with Alembic migrations - Comprehensive API endpoints with FastAPI - Database models for users, gyms, memberships, subscriptions, and transactions - Admin endpoints for user management and financial reporting - Health check and documentation endpoints Core Components: - FastAPI application with CORS support - SQLAlchemy ORM with relationship mapping - JWT-based authentication with bcrypt password hashing - Payment service abstraction for multiple gateways - Pydantic schemas for request/response validation - Alembic database migration system - Admin dashboard functionality - Environment variable configuration
103 lines
3.0 KiB
Python
103 lines
3.0 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.core.deps import get_current_active_user, get_current_admin_user
|
|
from app.models.gym import Gym
|
|
from app.models.membership import GymMembership, MembershipPlan
|
|
from app.models.user import User
|
|
from app.schemas.gym import Gym as GymSchema, GymCreate, GymUpdate
|
|
from app.schemas.membership import GymMembership as GymMembershipSchema
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[GymSchema])
|
|
def read_gyms(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
gyms = db.query(Gym).filter(Gym.is_active).offset(skip).limit(limit).all()
|
|
return gyms
|
|
|
|
|
|
@router.get("/{gym_id}", response_model=GymSchema)
|
|
def read_gym(gym_id: int, db: Session = Depends(get_db)):
|
|
gym = db.query(Gym).filter(Gym.id == gym_id, Gym.is_active).first()
|
|
if not gym:
|
|
raise HTTPException(status_code=404, detail="Gym not found")
|
|
return gym
|
|
|
|
|
|
@router.get("/{gym_id}/membership-plans")
|
|
def get_gym_membership_plans(gym_id: int, db: Session = Depends(get_db)):
|
|
gym = db.query(Gym).filter(Gym.id == gym_id, Gym.is_active).first()
|
|
if not gym:
|
|
raise HTTPException(status_code=404, detail="Gym not found")
|
|
|
|
plans = (
|
|
db.query(MembershipPlan)
|
|
.filter(MembershipPlan.gym_id == gym_id, MembershipPlan.is_active)
|
|
.all()
|
|
)
|
|
return plans
|
|
|
|
|
|
@router.post("/{gym_id}/join", response_model=GymMembershipSchema)
|
|
def join_gym(
|
|
gym_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
):
|
|
gym = db.query(Gym).filter(Gym.id == gym_id, Gym.is_active).first()
|
|
if not gym:
|
|
raise HTTPException(status_code=404, detail="Gym not found")
|
|
|
|
existing_membership = (
|
|
db.query(GymMembership)
|
|
.filter(
|
|
GymMembership.user_id == current_user.id, GymMembership.gym_id == gym_id
|
|
)
|
|
.first()
|
|
)
|
|
|
|
if existing_membership:
|
|
raise HTTPException(status_code=400, detail="Already a member of this gym")
|
|
|
|
membership = GymMembership(user_id=current_user.id, gym_id=gym_id)
|
|
db.add(membership)
|
|
db.commit()
|
|
db.refresh(membership)
|
|
return membership
|
|
|
|
|
|
@router.post("/", response_model=GymSchema)
|
|
def create_gym(
|
|
gym_data: GymCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_admin_user),
|
|
):
|
|
gym = Gym(**gym_data.dict())
|
|
db.add(gym)
|
|
db.commit()
|
|
db.refresh(gym)
|
|
return gym
|
|
|
|
|
|
@router.put("/{gym_id}", response_model=GymSchema)
|
|
def update_gym(
|
|
gym_id: int,
|
|
gym_update: GymUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_admin_user),
|
|
):
|
|
gym = db.query(Gym).filter(Gym.id == gym_id).first()
|
|
if not gym:
|
|
raise HTTPException(status_code=404, detail="Gym not found")
|
|
|
|
update_data = gym_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(gym, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(gym)
|
|
return gym
|