File size: 1,875 Bytes
cc7f27e
867c92e
 
cc7f27e
867c92e
 
 
 
 
 
cc7f27e
 
 
adb221d
 
 
 
 
 
 
 
 
 
 
 
 
cc7f27e
 
 
 
 
867c92e
adb221d
 
 
 
 
 
 
 
 
 
 
 
cc7f27e
 
 
867c92e
cc7f27e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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))