refactor: Introduce utility functions for exception handling and database operations; update auth and playlist services to use new exception methods
All checks were successful
Backend CI / test (push) Successful in 3m58s

This commit is contained in:
JSC
2025-07-31 13:28:06 +02:00
parent f24698e3ff
commit b8346ab667
9 changed files with 679 additions and 122 deletions

View File

@@ -20,6 +20,11 @@ from app.schemas.auth import (
)
from app.services.oauth import OAuthUserInfo
from app.utils.auth import JWTUtils, PasswordUtils, TokenUtils
from app.utils.exceptions import (
raise_bad_request,
raise_not_found,
raise_unauthorized,
)
logger = get_logger(__name__)
@@ -39,10 +44,7 @@ class AuthService:
# Check if email already exists
if await self.user_repo.email_exists(request.email):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Email address is already registered",
)
raise_bad_request("Email address is already registered")
# Hash the password
hashed_password = PasswordUtils.hash_password(request.password)
@@ -75,27 +77,18 @@ class AuthService:
# Get user by email
user = await self.user_repo.get_by_email(request.email)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid email or password",
)
raise_unauthorized("Invalid email or password")
# Check if user is active
if not user.is_active:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Account is deactivated",
)
raise_unauthorized("Account is deactivated")
# Verify password
if not user.password_hash or not PasswordUtils.verify_password(
request.password,
user.password_hash,
):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid email or password",
)
raise_unauthorized("Invalid email or password")
# Generate access token
token = self._create_access_token(user)
@@ -110,16 +103,10 @@ class AuthService:
"""Get the current authenticated user."""
user = await self.user_repo.get_by_id(user_id)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found",
)
raise_not_found("User")
if not user.is_active:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Account is deactivated",
)
raise_unauthorized("Account is deactivated")
return user

View File

@@ -10,6 +10,11 @@ from app.models.playlist import Playlist
from app.models.sound import Sound
from app.repositories.playlist import PlaylistRepository
from app.repositories.sound import SoundRepository
from app.utils.exceptions import (
raise_bad_request,
raise_internal_server_error,
raise_not_found,
)
logger = get_logger(__name__)
@@ -27,10 +32,7 @@ class PlaylistService:
"""Get a playlist by ID."""
playlist = await self.playlist_repo.get_by_id(playlist_id)
if not playlist:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Playlist not found",
)
raise_not_found("Playlist")
return playlist
@@ -47,9 +49,8 @@ class PlaylistService:
main_playlist = await self.playlist_repo.get_main_playlist()
if not main_playlist:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Main playlist not found. Make sure to run database seeding."
raise_internal_server_error(
"Main playlist not found. Make sure to run database seeding."
)
return main_playlist