Initial commit: HabitForge - habit tracking application with FastAPI backend, React frontend, and Zepp integration

This commit is contained in:
2026-07-31 08:58:53 -04:00
commit c7fce339c2
124 changed files with 17558 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
"""Services module for business logic."""
+52
View File
@@ -0,0 +1,52 @@
"""Authentication service for business logic."""
from typing import Optional
from sqlalchemy.orm import Session
from backend.models.user import User
from backend import auth
from backend.schemas.user import UserCreate
class AuthService:
"""Service for authentication operations."""
@staticmethod
def register_user(db: Session, user_data: UserCreate) -> User:
"""Register a new user."""
db_user = db.query(User).filter(
User.username == user_data.username
).first()
if db_user:
raise ValueError("Username already registered")
hashed_password = auth.get_password_hash(user_data.password)
new_user = models.User(
username=user_data.username,
hashed_password=hashed_password
)
db.add(new_user)
db.commit()
db.refresh(new_user)
return new_user
@staticmethod
def authenticate_user(db: Session, username: str, password: str) -> Optional[User]:
"""Authenticate a user with username and password."""
user = db.query(User).filter(
User.username == username
).first()
if not user or not auth.verify_password(password, user.hashed_password):
return None
return user
@staticmethod
def create_access_token(username: str) -> str:
"""Create a JWT access token for a user."""
from datetime import timedelta
from backend.core.config import settings
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = auth.create_access_token(
data={"sub": username},
expires_delta=access_token_expires
)
return access_token
+86
View File
@@ -0,0 +1,86 @@
"""Challenge service for business logic."""
from typing import List, Optional
from sqlalchemy.orm import Session
from sqlalchemy import func
from backend.models.challenge import Challenge
from backend.schemas.challenge import ChallengeCreate
class ChallengeService:
"""Service for challenge operations."""
@staticmethod
def create_challenge(
db: Session,
challenge_data: ChallengeCreate,
user_id: int
) -> Challenge:
"""Create a new challenge for a user."""
# Calculate next display_order for THIS user
max_order = db.query(func.max(Challenge.display_order)).filter(
Challenge.user_id == user_id
).scalar()
new_order = (max_order if max_order is not None else -1) + 1
db_challenge = Challenge(
**challenge_data.dict(),
display_order=new_order,
user_id=user_id
)
db.add(db_challenge)
db.commit()
db.refresh(db_challenge)
return db_challenge
@staticmethod
def get_user_challenges(
db: Session,
user_id: int
) -> List[Challenge]:
"""Get all challenges for a user."""
challenges = db.query(Challenge).filter(
Challenge.user_id == user_id
).order_by(Challenge.display_order.asc()).all()
return challenges
@staticmethod
def reorder_challenges(
db: Session,
ordered_ids: List[int],
user_id: int
) -> None:
"""Reorder challenges for a user."""
for index, challenge_id in enumerate(ordered_ids):
db.query(Challenge).filter(
Challenge.id == challenge_id,
Challenge.user_id == user_id
).update({"display_order": index})
db.commit()
@staticmethod
def delete_challenge(
db: Session,
challenge_id: int,
user_id: int
) -> Optional[Challenge]:
"""Delete a challenge for a user."""
db_challenge = db.query(Challenge).filter(
Challenge.id == challenge_id,
Challenge.user_id == user_id
).first()
if db_challenge:
db.delete(db_challenge)
db.commit()
return db_challenge
@staticmethod
def get_challenge_by_id(
db: Session,
challenge_id: int,
user_id: int
) -> Optional[Challenge]:
"""Get a challenge by ID for a user."""
return db.query(Challenge).filter(
Challenge.id == challenge_id,
Challenge.user_id == user_id
).first()
+26
View File
@@ -0,0 +1,26 @@
"""Health service for business logic."""
from sqlalchemy.orm import Session
from sqlalchemy import text
class HealthService:
"""Service for health check operations."""
@staticmethod
def check_database(db: Session) -> bool:
"""Check if database connection is healthy."""
try:
db.execute(text("SELECT 1"))
return True
except Exception:
return False
@staticmethod
def get_health_status(db: Session) -> dict:
"""Get the health status of the application."""
db_healthy = HealthService.check_database(db)
return {
"status": "healthy" if db_healthy else "unhealthy",
"database": "connected" if db_healthy else "disconnected"
}