|
4 | 4 | for users.
|
5 | 5 | """
|
6 | 6 |
|
7 |
| -from fastapi import APIRouter |
| 7 | +import logging |
| 8 | +from typing import Annotated, Optional |
8 | 9 |
|
| 10 | +from fastapi import APIRouter, Body, Depends, HTTPException, Response, status |
| 11 | +from fastapi.params import Path, Query |
| 12 | +from pydantic import NonNegativeInt, PositiveInt |
| 13 | + |
| 14 | +from app.config.exceptions import ( |
| 15 | + DatabaseException, |
| 16 | + NotFoundException, |
| 17 | + ServiceException, |
| 18 | +) |
| 19 | +from app.config.init_settings import init_setting |
| 20 | +from app.crud.user import UserRepository, get_user_repository |
| 21 | +from app.schemas.user import User, UserCreate, UserUpdate, UsersResponse |
| 22 | + |
| 23 | +logger: logging.Logger = logging.getLogger(__name__) |
9 | 24 | router: APIRouter = APIRouter(prefix="/user", tags=["user"])
|
| 25 | + |
| 26 | + |
| 27 | +@router.get("", response_model=UsersResponse) |
| 28 | +def get_users( |
| 29 | + user_repository: Annotated[UserRepository, Depends(get_user_repository)], |
| 30 | + skip: Annotated[ |
| 31 | + NonNegativeInt, |
| 32 | + Query( |
| 33 | + annotation=Optional[NonNegativeInt], |
| 34 | + title="Skip", |
| 35 | + description="Skip users", |
| 36 | + example=0, |
| 37 | + openapi_examples=init_setting.SKIP_EXAMPLES, |
| 38 | + ), |
| 39 | + ] = 0, |
| 40 | + limit: Annotated[ |
| 41 | + PositiveInt | None, |
| 42 | + Query( |
| 43 | + annotation=Optional[PositiveInt], |
| 44 | + title="Limit", |
| 45 | + description="Limit pagination", |
| 46 | + ge=1, |
| 47 | + le=100, |
| 48 | + example=100, |
| 49 | + openapi_examples=init_setting.LIMIT_EXAMPLES, |
| 50 | + ), |
| 51 | + ] = 100, |
| 52 | +) -> UsersResponse: |
| 53 | + """ |
| 54 | + Retrieve all users' basic information from the system using |
| 55 | + pagination. |
| 56 | + ## Parameters: |
| 57 | + - `:param skip:` **Offset from where to start returning users** |
| 58 | + - `:type skip:` **NonNegativeInt** |
| 59 | + - `:param limit:` **Limit the number of results from query** |
| 60 | + - `:type limit:` **PositiveInt** |
| 61 | + ## Response: |
| 62 | + - `:return:` **List of Users retrieved from database** |
| 63 | + - `:rtype:` **UsersResponse** |
| 64 | + \f |
| 65 | + :param user_repository: Dependency method for user service layer |
| 66 | + :type user_repository: UserRepository |
| 67 | + """ |
| 68 | + try: |
| 69 | + found_users: list[User] = user_repository.read_users(skip, limit) |
| 70 | + except ServiceException as exc: |
| 71 | + logger.error(exc) |
| 72 | + raise HTTPException( |
| 73 | + status_code=status.HTTP_404_NOT_FOUND, detail=str(exc) |
| 74 | + ) from exc |
| 75 | + users: UsersResponse = UsersResponse(users=found_users) |
| 76 | + return users |
| 77 | + |
| 78 | + |
| 79 | +@router.post("", response_model=User, status_code=status.HTTP_201_CREATED) |
| 80 | +def create_user( |
| 81 | + user: Annotated[ |
| 82 | + UserCreate, |
| 83 | + Body( |
| 84 | + ..., |
| 85 | + title="User data", |
| 86 | + description="User data to create", |
| 87 | + openapi_examples=init_setting.USER_CREATE_EXAMPLES, |
| 88 | + ), |
| 89 | + ], |
| 90 | + user_repository: Annotated[UserRepository, Depends(get_user_repository)], |
| 91 | +) -> User: |
| 92 | + """ |
| 93 | + Register new user into the system. |
| 94 | + ## Parameter: |
| 95 | + - `:param user:` **Body Object for user creation.** |
| 96 | + - `:type user:` **UserCreate** |
| 97 | + ## Response: |
| 98 | + - `:return:` **User created with its data** |
| 99 | + - `:rtype:` **User** |
| 100 | + \f |
| 101 | + :param user_repository: Dependency method for user service layer |
| 102 | + :type user_repository: UserRepository |
| 103 | + """ |
| 104 | + try: |
| 105 | + new_user: User | None = user_repository.create_user(user) |
| 106 | + except ServiceException as exc: |
| 107 | + detail: str = "Error at creating user." |
| 108 | + logger.error(detail) |
| 109 | + raise HTTPException( |
| 110 | + status_code=status.HTTP_400_BAD_REQUEST, detail=detail |
| 111 | + ) from exc |
| 112 | + if not new_user: |
| 113 | + raise HTTPException( |
| 114 | + status_code=status.HTTP_404_NOT_FOUND, |
| 115 | + detail="User could not be created", |
| 116 | + ) |
| 117 | + return new_user |
| 118 | + |
| 119 | + |
| 120 | +@router.get("/{user_id}", response_model=User) |
| 121 | +def get_user_by_id( |
| 122 | + user_repository: Annotated[UserRepository, Depends(get_user_repository)], |
| 123 | + user_id: Annotated[ |
| 124 | + PositiveInt, |
| 125 | + Path( |
| 126 | + ..., |
| 127 | + title="User ID", |
| 128 | + annotation=PositiveInt, |
| 129 | + description="ID of the User to be searched", |
| 130 | + example=1, |
| 131 | + ), |
| 132 | + ], |
| 133 | +) -> User: |
| 134 | + """ |
| 135 | + Retrieve an existing user's information given their user ID. |
| 136 | + ## Parameter: |
| 137 | + - `:param user_id:` **Unique identifier of the user to be retrieved** |
| 138 | + - `:type user_id:` **PositiveInt** |
| 139 | + ## Response: |
| 140 | + - `:return:` **Found user with the given ID.** |
| 141 | + - `:rtype:` **User** |
| 142 | + \f |
| 143 | + :param user_repository: Dependency method for user service layer |
| 144 | + :type user_repository: UserRepository |
| 145 | + """ |
| 146 | + try: |
| 147 | + user: User = user_repository.read_by_id(user_id) |
| 148 | + except ServiceException as exc: |
| 149 | + detail: str = f"User with id {user_id} not found in the system." |
| 150 | + logger.error(detail) |
| 151 | + raise HTTPException( |
| 152 | + status_code=status.HTTP_404_NOT_FOUND, detail=detail |
| 153 | + ) from exc |
| 154 | + except NotFoundException as not_found_exc: |
| 155 | + logger.error(not_found_exc) |
| 156 | + raise HTTPException( |
| 157 | + status_code=status.HTTP_404_NOT_FOUND, detail=str(not_found_exc) |
| 158 | + ) from not_found_exc |
| 159 | + return user |
| 160 | + |
| 161 | + |
| 162 | +@router.put("/{user_id}", response_model=User) |
| 163 | +def update_user( |
| 164 | + user_repository: Annotated[UserRepository, Depends(get_user_repository)], |
| 165 | + user_id: Annotated[ |
| 166 | + PositiveInt, |
| 167 | + Path( |
| 168 | + ..., |
| 169 | + title="User ID", |
| 170 | + annotation=PositiveInt, |
| 171 | + description="ID of the User to be searched", |
| 172 | + example=1, |
| 173 | + ), |
| 174 | + ], |
| 175 | + user_in: Annotated[ |
| 176 | + UserUpdate, |
| 177 | + Body( |
| 178 | + ..., |
| 179 | + title="User data", |
| 180 | + description="New user data to update", |
| 181 | + openapi_examples=init_setting.USER_UPDATE_EXAMPLES, |
| 182 | + ), |
| 183 | + ], |
| 184 | +) -> User | None: |
| 185 | + """ |
| 186 | + Update an existing user's information given their user ID and new |
| 187 | + information. |
| 188 | + ## Parameters: |
| 189 | + - `:param user_id:` **Unique identifier of the user to be updated** |
| 190 | + - `:type user_id:` **PositiveInt** |
| 191 | + - `:param user_in:` **New user data to update that can include: |
| 192 | + username, email, first_name, middle_name, last_name, password, |
| 193 | + gender, birthdate, phone_number, city and country.** |
| 194 | + - `:type user_in:` **UserUpdate** |
| 195 | + ## Response: |
| 196 | + - `:return:` **Updated user with the given ID and its data** |
| 197 | + - `:rtype:` **User** |
| 198 | + \f |
| 199 | + :param user_repository: Dependency method for user service layer |
| 200 | + :type user_repository: UserRepository |
| 201 | + """ |
| 202 | + try: |
| 203 | + user: User | None = user_repository.update_user(user_id, user_in) |
| 204 | + except ServiceException as exc: |
| 205 | + detail: str = f"User with id {user_id} not found in the system." |
| 206 | + logger.error(detail) |
| 207 | + raise HTTPException( |
| 208 | + status_code=status.HTTP_400_BAD_REQUEST, detail=detail |
| 209 | + ) from exc |
| 210 | + return user |
| 211 | + |
| 212 | + |
| 213 | +@router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT) |
| 214 | +def delete_user( |
| 215 | + user_repository: Annotated[UserRepository, Depends(get_user_repository)], |
| 216 | + user_id: Annotated[ |
| 217 | + PositiveInt, |
| 218 | + Path( |
| 219 | + ..., |
| 220 | + title="User ID", |
| 221 | + annotation=PositiveInt, |
| 222 | + description="ID of the User to be searched", |
| 223 | + example=1, |
| 224 | + ), |
| 225 | + ], |
| 226 | +) -> Response: |
| 227 | + """ |
| 228 | + Delete an existing user given their user ID. |
| 229 | + ## Parameter: |
| 230 | + - `:param user_id:` **Unique identifier of the user to be deleted** |
| 231 | + - `:type user_id:` **PositiveInt** |
| 232 | + ## Response: |
| 233 | + - `:return:` **Json Response object with the deleted information** |
| 234 | + - `:rtype:` **Response** |
| 235 | + \f |
| 236 | + :param user_repository: Dependency method for user service layer |
| 237 | + :type user_repository: UserRepository |
| 238 | + """ |
| 239 | + try: |
| 240 | + delete_result = user_repository.delete_user(user_id) |
| 241 | + except DatabaseException as exc: |
| 242 | + logger.error(f"Failed to delete user with ID {user_id}: {exc}") |
| 243 | + raise HTTPException( |
| 244 | + status_code=status.HTTP_404_NOT_FOUND, |
| 245 | + detail=f"User with ID {user_id} not found.", |
| 246 | + ) from exc |
| 247 | + response: Response = Response(status_code=status.HTTP_204_NO_CONTENT) |
| 248 | + response.headers["deleted"] = delete_result["deleted"].lower() |
| 249 | + response.headers["deleted_at"] = ( |
| 250 | + delete_result["deleted_at"].isoformat() |
| 251 | + if delete_result["deleted_at"] |
| 252 | + else "null" |
| 253 | + ) |
| 254 | + return response |
0 commit comments