Skip to content

Commit

Permalink
feat: Reviews 에 CRUD 관련 라우터 추가 및 리팩토링 (#19)
Browse files Browse the repository at this point in the history
* style: import 코드 재정렬(Optimize)

* refactor: 사용하지 않는 exceptions.py 삭제

* feat: CRUD 관련 엔드포인트 추가 (GET /reviews/ 만 제외) 및 코드 전반 리팩토링
  • Loading branch information
heumsi authored Feb 11, 2021
1 parent ca49efb commit 2525a73
Show file tree
Hide file tree
Showing 22 changed files with 462 additions and 299 deletions.
1 change: 1 addition & 0 deletions app/auth/application/dtos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import BaseModel

from shared_kernel.application.dtos import SuccessOutputDto


Expand Down
51 changes: 14 additions & 37 deletions app/auth/application/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Union

from jose import JWTError, jwt

from auth.application.dtos import (
GetTokenDataInputDto,
GetTokenDataOutputDto,
Expand All @@ -9,46 +11,31 @@
VerifyTokenOutputDto,
)
from auth.domain.value_objects import TokenPayload
from jose import JWTError, jwt
from settings import Settings
from shared_kernel.application.dtos import FailedOutputDto
from users.application.dtos import LoginInputDto
from users.application.service import UserApplicationService


class AuthApplicationService:
def __init__(
self, user_application_service: UserApplicationService, settings: Settings
) -> None:
def __init__(self, user_application_service: UserApplicationService, settings: Settings) -> None:
self._user_application_service = user_application_service
self._JWT_SECRET_KEY = settings.JWT_SECRET_KEY
self._JWT_ALGORITHM = settings.JWT_ALGORITHM

def get_token(
self, input_dto: GetTokenInputDto
) -> Union[GetTokenOutputDto, FailedOutputDto]:
def get_token(self, input_dto: GetTokenInputDto) -> Union[GetTokenOutputDto, FailedOutputDto]:
try:
login_input_dto = LoginInputDto(
user_id=input_dto.user_id, password=input_dto.password
)
login_output_dto = self._user_application_service.login(
input_dto=login_input_dto
)
login_input_dto = LoginInputDto(user_id=input_dto.user_id, password=input_dto.password)
login_output_dto = self._user_application_service.login(input_dto=login_input_dto)

if login_output_dto.status is True:
access_token = self._create_access_token(
data=TokenPayload(user_id=input_dto.user_id).dict()
)
access_token = self._create_access_token(data=TokenPayload(user_id=input_dto.user_id).dict())
return GetTokenOutputDto(access_token=access_token)
return FailedOutputDto.build_resource_error(
message=login_output_dto.message
)
return FailedOutputDto.build_resource_error(message=login_output_dto.message)
except Exception as e:
return FailedOutputDto.build_system_error(message=str(e))

def get_token_data(
self, input_dto: GetTokenDataInputDto
) -> Union[GetTokenDataOutputDto, FailedOutputDto]:
def get_token_data(self, input_dto: GetTokenDataInputDto) -> Union[GetTokenDataOutputDto, FailedOutputDto]:
try:
decoded_jwt = jwt.decode(
token=input_dto.access_token,
Expand All @@ -58,13 +45,9 @@ def get_token_data(
token_payload = TokenPayload(**decoded_jwt)
return GetTokenDataOutputDto(user_id=token_payload.user_id)
except JWTError:
return FailedOutputDto.build_unauthorized_error(
message="올바른 access-token이 아닙니다."
)
return FailedOutputDto.build_unauthorized_error(message="올바른 access-token이 아닙니다.")

def verify_token(
self, input_dto: VerifyTokenInputDto
) -> Union[VerifyTokenOutputDto, FailedOutputDto]:
def verify_token(self, input_dto: VerifyTokenInputDto) -> Union[VerifyTokenOutputDto, FailedOutputDto]:
"""
현재 사용하지 않음.
"""
Expand All @@ -76,21 +59,15 @@ def verify_token(
)
if TokenPayload(**decoded_jwt) == TokenPayload(user_id=input_dto.user_id):
return VerifyTokenOutputDto()
return FailedOutputDto.build_unauthorized_error(
message="access-token이 유효하지 않습니다."
)
return FailedOutputDto.build_unauthorized_error(message="access-token이 유효하지 않습니다.")
except JWTError:
return FailedOutputDto.build_unauthorized_error(
message="올바른 access-token이 아닙니다."
)
return FailedOutputDto.build_unauthorized_error(message="올바른 access-token이 아닙니다.")
except Exception as e:
return FailedOutputDto.build_system_error(message=str(e))

def _create_access_token(self, data: dict) -> str:
try:
encoded_jwt = jwt.encode(
data, self._JWT_SECRET_KEY, algorithm=self._JWT_ALGORITHM
)
encoded_jwt = jwt.encode(data, self._JWT_SECRET_KEY, algorithm=self._JWT_ALGORITHM)
return encoded_jwt
except Exception as e:
return FailedOutputDto.build_system_error(message=str(e))
7 changes: 4 additions & 3 deletions app/auth/external_interface/routers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends
from starlette import status

from auth.application.dtos import GetTokenInputDto
from auth.external_interface.json_dto import GetTokenJsonRequest, GetTokenJsonResponse
from container import Container
from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends
from shared_kernel.external_interface.json_dto import FailedJsonResponse
from starlette import status

router = APIRouter(
prefix="/auth",
Expand Down
15 changes: 8 additions & 7 deletions app/container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from auth.application.service import AuthApplicationService
from dependency_injector import containers, providers

from auth.application.service import AuthApplicationService
from reviews.application.service import ReviewApplicationService
from reviews.infra_structure.in_memory_repository import InMemoryReviewRepository
from settings import Settings
from users.application.service import UserApplicationService
from users.infra_structure.in_memory_repository import InMemoryUserRepository
Expand All @@ -11,13 +14,11 @@ class Container(containers.DeclarativeContainer):

# repository
user_repository = providers.Singleton(InMemoryUserRepository)
review_repository = providers.Singleton(InMemoryReviewRepository)

# application service
user_application_service = providers.Singleton(
UserApplicationService, user_repository=user_repository
)
user_application_service = providers.Singleton(UserApplicationService, user_repository=user_repository)
auth_application_service = providers.Singleton(
AuthApplicationService,
user_application_service=user_application_service,
settings=settings,
AuthApplicationService, user_application_service=user_application_service, settings=settings
)
review_application_service = providers.Singleton(ReviewApplicationService, review_repository=review_repository)
11 changes: 7 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import uvicorn
from fastapi import FastAPI

import auth.external_interface.routers
import health.external_interface.routers
import reviews.external_interface.routers
import users.external_interface.routers
import uvicorn
from container import Container
from fastapi import FastAPI


def create_app():
router_modules = [
users.external_interface.routers,
health.external_interface.routers,
auth.external_interface.routers,
health.external_interface.routers,
users.external_interface.routers,
reviews.external_interface.routers,
]
container = Container()
container.wire(modules=router_modules)
Expand Down
30 changes: 21 additions & 9 deletions app/reviews/application/dtos.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
from datetime import datetime
from typing import List
from uuid import UUID

from pydantic import BaseModel, Field
from reviews.domain.value_objects import ReviewRating
from users.domain.value_objects import UserId
from pydantic import BaseModel

from shared_kernel.application.dtos import SuccessOutputDto


class FindReviewInputDto(BaseModel):
review_id: str


class FindReviewOutputDto(BaseModel):
class FindReviewOutputDto(SuccessOutputDto):
review_id: str
drink_id: str
user_id: str
rating: int
comment: str
created_at: float
updated_at: float


class CreateReviewInputDto(BaseModel):
drink_id: str
user_id: str
rating: int
comment: str
created_at: float


class UpdateReviewInputDto(BaseModel):
review_id: str
class CreateReviewOutputDto(SuccessOutputDto):
drink_id: str
user_id: str
rating: int
comment: str
created_at: float
updated_at: float


class UpdateReviewInputDto(BaseModel):
review_id: str
rating: int
comment: str


class UpdateReviewOutputDto(SuccessOutputDto):
pass


class DeleteReviewInputDto(BaseModel):
review_id: str


class DeleteReviewOutputDto(SuccessOutputDto):
pass


class FindReviewsByUserIdInputDto(BaseModel):
user_id: str

Expand Down
8 changes: 0 additions & 8 deletions app/reviews/application/exceptions.py

This file was deleted.

Loading

0 comments on commit 2525a73

Please sign in to comment.