def create_wish( self, input_dto: CreateWishInputDto, drink_application_service: DrinkApplicationService, ) -> Union[CreateWishOutputDto, FailedOutputDto]: try: wish = Wish( id=WishId.build(user_id=str(input_dto.user_id), drink_id=str(input_dto.drink_id)), user_id=UserId(value=input_dto.user_id), drink_id=DrinkId.from_str(input_dto.drink_id), created_at=time.time(), ) self._wish_repository.add(wish) add_drink_wish_input_dto = AddDrinkWishInputDto( drink_id=input_dto.drink_id) add_drink_wish_output_dto = drink_application_service.add_drink_wish( input_dto=add_drink_wish_input_dto) if not add_drink_wish_output_dto.status: return add_drink_wish_output_dto return CreateWishOutputDto(id=str(wish.id), user_id=str(wish.user_id), drink_id=str(wish.drink_id), created_at=wish.created_at) except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_review( self, input_dto: DeleteReviewInputDto, drink_application_service: DrinkApplicationService, ) -> Union[DeleteReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) review = self._review_repository.find_by_review_id(review_id) if review is None: return FailedOutputDto.build_resource_not_found_error( f"{str(input_dto.review_id)}의 리뷰를 찾을 수 없습니다.") self._review_repository.delete_by_review_id(review_id) drinks_input_dto = DeleteDrinkReviewInputDto( drink_id=str(review.drink_id), drink_rating=int(review.rating)) drink_delete_review_output_dto = drink_application_service.delete_drink_review( input_dto=drinks_input_dto) if not drink_delete_review_output_dto.status: return drink_delete_review_output_dto return DeleteReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) 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) return encoded_jwt except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_user( self, input_dto: DeleteUserInputDto ) -> Union[DeleteUserOutputDto, FailedOutputDto]: try: user_id = UserId(value=input_dto.user_id) self._user_repository.delete_by_user_id(user_id) return DeleteUserOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def add_drink_wish(self, input_dto: AddDrinkWishInputDto) -> Union[AddDrinkWishOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) drink.add_wish() self._drink_repository.update(drink) return AddDrinkWishOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_drink(self, input_dto: DeleteDrinkInputDto) -> Union[DeleteDrinkOutputDto, FailedOutputDto]: try: drink_id = DrinkId.from_str(input_dto.drink_id) if self._drink_repository.find_by_drink_id(drink_id) is None: return FailedOutputDto.build_resource_not_found_error(f"{str(drink_id)}의 술을 찾을 수 없습니다.") self._drink_repository.delete_by_drink_id(drink_id) return DeleteDrinkOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_wishes( self, input_dto: FindWishesInputDto ) -> Union[FindWishesOutputDto, FailedOutputDto]: try: wishes = self._wish_repository.find_all(input_dto.query_param) return FindWishesOutputDto(items=[ FindWishesOutputDto.Item( id=str(wish.id), user_id=str(wish.user_id), drink_id=str(wish.drink_id), created_at=wish.created_at, ) for wish in wishes ]) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def login( self, input_dto: LoginInputDto ) -> Union[LoginOutputDto, FailedOutputDto]: try: user_id = UserId(value=input_dto.user_id) user = self._user_repository.find_by_user_id(user_id=user_id) if not self._verify_password(input_dto.password, user.password): return FailedOutputDto.build_unauthorized_error( f"잘못된 비밀번호 입니다.") return LoginOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_drink_review( self, input_dto: DeleteDrinkReviewInputDto ) -> Union[DeleteDrinkReviewOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) drink.delete_rating(input_dto.drink_rating) self._drink_repository.update(drink) return DeleteDrinkReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def update_user( self, input_dto: UpdateUserInputDto ) -> Union[UpdateUserOutputDto, FailedOutputDto]: try: user = User( id=UserId(value=input_dto.user_id), name=UserName(value=input_dto.user_name), description=input_dto.description, password=input_dto.password, ) self._user_repository.update(user) return UpdateUserOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_user( self, input_dto: FindUserInputDto ) -> Union[FindUserOutputDto, FailedOutputDto]: try: user = self._user_repository.find_by_user_id(user_id=UserId( value=input_dto.user_id)) return FindUserOutputDto( user_id=str(user.id), user_name=str(user.name), description=user.description, image_url=user.image_url, ) except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
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) if login_output_dto.status is True: 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) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def create_drink(self, input_dto: CreateDrinkInputDto) -> Union[CreateDrinkOutputDto, FailedOutputDto]: try: drink = Drink( id=DrinkId.build(drink_name=input_dto.drink_name, created_at=time.time()), name=input_dto.drink_name, image_url=input_dto.drink_image_url, type=DrinkType.from_str(input_dto.drink_type), ) self._drink_repository.add(drink) return CreateDrinkOutputDto() except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_review( self, input_dto: FindReviewInputDto ) -> Union[FindReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) review = self._review_repository.find_by_review_id(review_id) return FindReviewOutputDto( review_id=str(review.id), drink_id=str(review.drink_id), user_id=str(review.user_id), rating=int(review.rating), comment=review.comment, created_at=review.created_at, updated_at=review.updated_at, ) except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def create_user( self, input_dto: CreateUserInputDto ) -> Union[CreateUserOutputDto, FailedOutputDto]: try: user = User( id=UserId(value=input_dto.user_id), name=UserName(value=input_dto.user_name), password=self._get_password_hash(input_dto.password), ) self._user_repository.add(user) return CreateUserOutputDto( user_id=str(user.id), user_name=str(user.name), description=user.description, image_url=user.image_url, ) except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_reviews( self, input_dto: FindReviewsInputDto ) -> Union[FailedOutputDto, FindReviewsOutputDto]: try: reviews = self._review_repository.find_all( query_param=input_dto.query_param) return FindReviewsOutputDto(items=[ FindReviewsOutputDto.Item( review_id=str(review.id), drink_id=str(review.drink_id), user_id=str(review.user_id), rating=int(review.rating), comment=review.comment, created_at=review.created_at, updated_at=review.updated_at, ) for review in reviews ]) except InvalidParamInputError as e: return FailedOutputDto.build_parameters_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_drinks(self, input_dto: FindDrinksInputDto) -> Union[FindDrinksOutputDto, FailedOutputDto]: try: drinks = self._drink_repository.find_all(input_dto.query_param) return FindDrinksOutputDto( items=[ FindDrinksOutputDto.Item( drink_id=str(drink.id), drink_name=drink.name, drink_image_url=drink.image_url, drink_type=drink.type.value, avg_rating=float(drink.avg_rating), num_of_reviews=drink.num_of_reviews, num_of_wish=drink.num_of_wish, ) for drink in drinks ] ) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_wish( self, input_dto: DeleteWishInputDto, drink_application_service: DrinkApplicationService, ) -> Union[DeleteWishOutputDto, FailedOutputDto]: try: wish = self._wish_repository.delete_by_wish_id( WishId.from_str(input_dto.wish_id)) delete_drink_wish_input_dto = DeleteDrinkWishInputDto( drink_id=str(wish.drink_id)) delete_drink_wish_output_dto = drink_application_service.delete_drink_wish( input_dto=delete_drink_wish_input_dto) if not delete_drink_wish_output_dto.status: return delete_drink_wish_output_dto return DeleteWishOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def update_drink_review( self, input_dto: UpdateDrinkReviewInputDto ) -> Union[UpdateDrinkReviewOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) if drink is None: return FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.drink_id)}의 술을 찾을 수 없습니다." ) drink.update_rating( old_rating=input_dto.old_drink_rating, new_rating=input_dto.new_drink_rating, ) self._drink_repository.update(drink) return UpdateDrinkReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_drink(self, input_dto: FindDrinkInputDto) -> Union[FindDrinkOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(drink_id=DrinkId.from_str(input_dto.drink_id)) if drink is None: return FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.drink_id)}의 술을 찾을 수 없습니다." ) return FindDrinkOutputDto( drink_id=str(drink.id), drink_name=drink.name, drink_image_url=drink.image_url, drink_type=drink.type.value, avg_rating=float(drink.avg_rating), num_of_reviews=drink.num_of_reviews, num_of_wish=drink.num_of_wish, ) except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def verify_token( self, input_dto: VerifyTokenInputDto ) -> Union[VerifyTokenOutputDto, FailedOutputDto]: """ 현재 사용하지 않음. """ try: decoded_jwt = jwt.decode( token=input_dto.access_token, key=self._JWT_SECRET_KEY, algorithms=self._JWT_ALGORITHM, ) if TokenPayload(**decoded_jwt) == TokenPayload( user_id=input_dto.user_id): return VerifyTokenOutputDto() return FailedOutputDto.build_unauthorized_error( message="access-token이 유효하지 않습니다.") except JWTError: return FailedOutputDto.build_unauthorized_error( message="올바른 access-token이 아닙니다.") except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def create_review( self, input_dto: CreateReviewInputDto, drink_application_service: DrinkApplicationService, ) -> Union[CreateReviewOutputDto, FailedOutputDto]: try: review = Review( id=ReviewId.build(user_id=input_dto.user_id, drink_id=input_dto.drink_id), drink_id=DrinkId(value=input_dto.drink_id), user_id=UserId(value=input_dto.user_id), rating=ReviewRating(value=input_dto.rating), comment=input_dto.comment, created_at=time.time(), updated_at=time.time(), ) self._review_repository.add(review) input_dto = AddDrinkReviewInputDto(drink_id=input_dto.drink_id, drink_rating=input_dto.rating) drink_add_review_output_dto = drink_application_service.add_drink_review( input_dto=input_dto) if not drink_add_review_output_dto.status: return drink_add_review_output_dto return CreateReviewOutputDto( review_id=str(review.id), drink_id=str(review.drink_id), user_id=str(review.user_id), rating=int(review.rating), comment=review.comment, created_at=review.created_at, updated_at=review.updated_at, ) except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def update_review( self, input_dto: UpdateReviewInputDto, drink_application_service: DrinkApplicationService, ) -> Union[UpdateReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) old_review = self._review_repository.find_by_review_id(review_id) old_rating = int(old_review.rating) new_review = Review( id=old_review.id, drink_id=old_review.drink_id, user_id=old_review.user_id, rating=ReviewRating(value=input_dto.rating), comment=input_dto.comment, created_at=old_review.created_at, updated_at=time.time(), ) self._review_repository.update(new_review) drinks_input_dto = UpdateDrinkReviewInputDto( drink_id=str(new_review.drink_id), old_drink_rating=old_rating, new_drink_rating=input_dto.rating, ) drink_update_review_output_dto = drink_application_service.update_drink_review( input_dto=drinks_input_dto) if not drink_update_review_output_dto.status: return drink_update_review_output_dto return UpdateReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def update_drink(self, input_dto: UpdateDrinkInputDto) -> Union[UpdateDrinkOutputDto, FailedOutputDto]: try: drink_id = DrinkId.from_str(input_dto.drink_id) if self._drink_repository.find_by_drink_id(drink_id) is None: return FailedOutputDto.build_resource_not_found_error(f"{str(drink_id)}의 술을 찾을 수 없습니다.") drink = Drink( id=drink_id, name=input_dto.drink_name, image_url=input_dto.drink_image_url, type=input_dto.drink_type, avg_rating=DrinkRating(value=input_dto.avg_rating), num_of_reviews=input_dto.num_of_reviews, num_of_wish=input_dto.num_of_wish, ) self._drink_repository.update(drink) return UpdateDrinkOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))