
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
94 lines
2.7 KiB
Python
94 lines
2.7 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_admin_user
|
|
from app.models.membership import MembershipPlan
|
|
from app.models.gym import Gym
|
|
from app.schemas.membership import (
|
|
MembershipPlan as MembershipPlanSchema,
|
|
MembershipPlanCreate,
|
|
MembershipPlanUpdate,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/plans", response_model=List[MembershipPlanSchema])
|
|
def read_membership_plans(
|
|
skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
|
|
):
|
|
plans = (
|
|
db.query(MembershipPlan)
|
|
.filter(MembershipPlan.is_active)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return plans
|
|
|
|
|
|
@router.get("/plans/{plan_id}", response_model=MembershipPlanSchema)
|
|
def read_membership_plan(plan_id: int, db: Session = Depends(get_db)):
|
|
plan = (
|
|
db.query(MembershipPlan)
|
|
.filter(MembershipPlan.id == plan_id, MembershipPlan.is_active)
|
|
.first()
|
|
)
|
|
if not plan:
|
|
raise HTTPException(status_code=404, detail="Membership plan not found")
|
|
return plan
|
|
|
|
|
|
@router.post("/plans", response_model=MembershipPlanSchema)
|
|
def create_membership_plan(
|
|
plan_data: MembershipPlanCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user=Depends(get_current_admin_user),
|
|
):
|
|
gym = db.query(Gym).filter(Gym.id == plan_data.gym_id, Gym.is_active).first()
|
|
if not gym:
|
|
raise HTTPException(status_code=404, detail="Gym not found")
|
|
|
|
plan = MembershipPlan(**plan_data.dict())
|
|
db.add(plan)
|
|
db.commit()
|
|
db.refresh(plan)
|
|
return plan
|
|
|
|
|
|
@router.put("/plans/{plan_id}", response_model=MembershipPlanSchema)
|
|
def update_membership_plan(
|
|
plan_id: int,
|
|
plan_update: MembershipPlanUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user=Depends(get_current_admin_user),
|
|
):
|
|
plan = db.query(MembershipPlan).filter(MembershipPlan.id == plan_id).first()
|
|
if not plan:
|
|
raise HTTPException(status_code=404, detail="Membership plan not found")
|
|
|
|
update_data = plan_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(plan, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(plan)
|
|
return plan
|
|
|
|
|
|
@router.delete("/plans/{plan_id}")
|
|
def delete_membership_plan(
|
|
plan_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user=Depends(get_current_admin_user),
|
|
):
|
|
plan = db.query(MembershipPlan).filter(MembershipPlan.id == plan_id).first()
|
|
if not plan:
|
|
raise HTTPException(status_code=404, detail="Membership plan not found")
|
|
|
|
plan.is_active = False
|
|
db.commit()
|
|
return {"message": "Membership plan deactivated successfully"}
|