from fastapi import APIRouter, HTTPException from pydantic import BaseModel from src.services import UserService from src.utils import logger class UserRegisterRequest(BaseModel): email_id: str name: str class UserController: """ UserController handles user-related operations such as registration. Attributes: service (UserService): The service used to handle user operations. router (APIRouter): The router for handling user-related API routes. Methods: registerUser(user: UserRegisterRequest): Registers a new user and returns a success message along with the user details. Logs the received event and handles exceptions that may occur during the process. """ def __init__(self): self.service = UserService self.router = APIRouter(prefix="/users", tags=["User"]) self.router.add_api_route("/", self.registerUser, methods=["POST"]) async def registerUser(self, user: UserRegisterRequest): """ Registers a new user and returns a success message along with the user details. Args: user (UserRegisterRequest): The user details to be registered. Returns: dict: A dictionary containing a success message and the user details. Raises: HTTPException: If an error occurs during the registration process. """ logger.info(f"Received event: {user}") try: async with self.service() as service: user = await service.register_user(user.model_dump()) return {"message": "User registered successfully", "user": user} except HTTPException as e: logger.warning(e) raise e except Exception as e: logger.error(e) raise HTTPException(status_code=500, detail=str(e))