|
1 | | -from datetime import datetime |
2 | | - |
3 | | -from loguru import logger |
| 1 | +from dependency_injector.wiring import Provide, inject |
| 2 | +from fastapi import Depends |
| 3 | +from starlette import status |
4 | 4 |
|
| 5 | +from app.core.container import Container |
5 | 6 | from app.core.router import CoreAPIRouter |
6 | | -from app.exceptions.users import ( |
7 | | - InsufficientFunds, |
8 | | - InvalidInput, |
9 | | - UnauthorizedAccess, |
10 | | - UserNotFound, |
11 | | -) |
12 | | -from app.schemas.users import User |
| 7 | +from app.schemas.users import UserCreateRequest, UserCreateResponse |
| 8 | +from app.services.users import UserService |
13 | 9 |
|
14 | 10 | router = CoreAPIRouter(prefix="/user", tags=["user"]) |
15 | 11 |
|
16 | 12 |
|
17 | | -@router.get( |
| 13 | +@router.post( |
18 | 14 | "", |
19 | | - response_model=User, |
20 | | - status_code=200, |
21 | | - summary="Get User Test", |
22 | | - description="1 ~ 4: Error!", |
| 15 | + response_model=UserCreateResponse, |
| 16 | + status_code=status.HTTP_201_CREATED, |
| 17 | + summary="", |
| 18 | + description="", |
| 19 | +) |
| 20 | +@inject |
| 21 | +async def create_user( |
| 22 | + user: UserCreateRequest, |
| 23 | + service: UserService = Depends(Provide[Container.user_service]), |
| 24 | +): |
| 25 | + return service.create(user) |
| 26 | + |
| 27 | + |
| 28 | +@router.get( |
| 29 | + "/{id}", |
| 30 | + response_model=UserCreateResponse, |
| 31 | + status_code=status.HTTP_200_OK, |
| 32 | + summary="", |
| 33 | + description="", |
| 34 | +) |
| 35 | +@inject |
| 36 | +async def get_user( |
| 37 | + id: int, |
| 38 | + service: UserService = Depends(Provide[Container.user_service]), |
| 39 | +): |
| 40 | + return service.get_by_id(id) |
| 41 | + |
| 42 | + |
| 43 | +@router.put( |
| 44 | + "/{id}", |
| 45 | + response_model=UserCreateResponse, |
| 46 | + status_code=status.HTTP_200_OK, |
| 47 | + summary="", |
| 48 | + description="", |
| 49 | +) |
| 50 | +@inject |
| 51 | +async def put_user( |
| 52 | + id: int, |
| 53 | + user: UserCreateRequest, |
| 54 | + service: UserService = Depends(Provide[Container.user_service]), |
| 55 | +): |
| 56 | + return service.put_by_id(id=id, schema=user) |
| 57 | + |
| 58 | + |
| 59 | +@router.patch( |
| 60 | + "/{id}", |
| 61 | + response_model=UserCreateResponse, |
| 62 | + status_code=status.HTTP_200_OK, |
| 63 | + summary="", |
| 64 | + description="", |
| 65 | +) |
| 66 | +@inject |
| 67 | +async def patch_user( |
| 68 | + id: int, |
| 69 | + user: UserCreateRequest, |
| 70 | + service: UserService = Depends(Provide[Container.user_service]), |
| 71 | +): |
| 72 | + return service.patch_by_id(id=id, schema=user) |
| 73 | + |
| 74 | + |
| 75 | +@router.delete( |
| 76 | + "/{id}", |
| 77 | + response_model=UserCreateResponse, |
| 78 | + status_code=status.HTTP_200_OK, |
| 79 | + summary="", |
| 80 | + description="", |
23 | 81 | ) |
24 | | -async def get_user(user_id: int): |
25 | | - try: |
26 | | - if user_id == 1: |
27 | | - raise InsufficientFunds |
28 | | - if user_id == 2: |
29 | | - raise InvalidInput |
30 | | - if user_id == 3: |
31 | | - raise UnauthorizedAccess("Unauthorized.") |
32 | | - if user_id == 4: |
33 | | - raise UserNotFound("User not found.") |
34 | | - except Exception as error: |
35 | | - logger.exception(repr(error)) |
36 | | - raise error |
37 | | - return User(id=user_id, created_at=datetime.now(), updated_at=datetime.now()) |
| 82 | +@inject |
| 83 | +async def delete_user( |
| 84 | + id: int, |
| 85 | + service: UserService = Depends(Provide[Container.user_service]), |
| 86 | +): |
| 87 | + return service.delete_by_id(id) |
0 commit comments