Adjusted sql to the way the tables are now defined.

Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
2025-08-23 21:14:30 -04:00
parent 60dc653e8c
commit 442de8036a
2 changed files with 15 additions and 33 deletions

View File

@@ -5,14 +5,12 @@ from typing import cast
from sqlalchemy import delete
from sqlalchemy import select
from sqlalchemy.exc import NoResultFound
from sqlalchemy.ext.asyncio import AsyncSession
from backend.models import Booking
from backend.models import Invitee
from backend.models import Room
from backend.models import User
from backend.services.users import get_user_by_email
logger = logging.getLogger(__name__)
@@ -43,29 +41,24 @@ async def get_invitees_for_booking(session: AsyncSession, booking_id: int) -> Us
async def add_invitee_to_booking(
session: AsyncSession, booking_id: int, email: str
session: AsyncSession, booking_id: int, user_email: str
) -> Invitee:
"""Add an invitee to a booking."""
logger.debug(
f"Entering add_invitee_to_booking with booking_id: {booking_id}, email: {email}"
f"Entering add_invitee_to_booking with booking_id: {booking_id}, email: {user_email}"
)
try:
user = await get_user_by_email(session, email)
invitee = Invitee(booking_id=booking_id, user_id=user.id)
invitee = Invitee(booking_id=booking_id, user_email=user_email)
session.add(invitee)
await session.commit()
logger.info(
f"Successfully added invitee with email {email} to booking_id: {booking_id}"
f"Successfully added invitee with email {user_email} to booking_id: {booking_id}"
)
return invitee
except NoResultFound as e:
logger.error(
f"User with email {email} does not exist for booking_id {booking_id}"
)
raise ValueError(f"User with email {email} does not exist.") from e
except Exception as e:
logger.error(
f"Failed to add invitee with email {email} to booking_id {booking_id}: {str(e)}"
f"Failed to add invitee with email {user_email} to"
f" booking_id {booking_id}: {str(e)}"
)
await session.rollback()
raise
@@ -74,37 +67,26 @@ async def add_invitee_to_booking(
async def remove_invitee_from_booking(
session: AsyncSession, booking_id: int, email: str
session: AsyncSession, booking_id: int, user_email: str
) -> None:
"""Remove an invitee from a booking."""
logger.debug(
f"Entering remove_invitee_from_booking with booking_id: {booking_id}, email: {email}"
f"Entering remove_invitee_from_booking with"
f" booking_id: {booking_id}, email: {user_email}"
)
try:
user = await get_user_by_email(session, email)
stmt = delete(Invitee).where(
Invitee.booking_id == booking_id, Invitee.user_id == user.id
Invitee.booking_id == booking_id, Invitee.user_email == user_email
)
result = await session.execute(stmt)
if result.rowcount == 0:
logger.error(
f"No invitee with email {email} found for booking_id {booking_id}"
)
raise ValueError(
f"No invitee with email {email} found for booking_id {booking_id}"
)
await session.execute(stmt)
await session.commit()
logger.info(
f"Successfully removed invitee with email {email} from booking_id: {booking_id}"
f"Successfully removed invitee with email {user_email} from"
f" booking_id: {booking_id}"
)
except NoResultFound as e:
logger.error(
f"User with email {email} does not exist for booking_id {booking_id}"
)
raise ValueError(f"User with email {email} does not exist.") from e
except Exception as e:
logger.error(
f"Failed to remove invitee with email {email}"
f"Failed to remove invitee with email {user_email}"
f" from booking_id {booking_id}: {str(e)}"
)
await session.rollback()

View File

@@ -35,7 +35,7 @@ async def get_users(session: AsyncSession) -> UserList:
logger.debug("Exiting get_users")
async def get_user_by_email(session: AsyncSession, email: str) -> User:
async def get_user(session: AsyncSession, email: str) -> User:
"""Retrieve a user by their email address."""
logger.debug(f"Entering get_user_by_email with email: {email}")
try: