async def create_comment_for_article( comment_create: CommentInCreate = Body(..., embed=True, alias="comment"), article: Article = Depends(get_article_by_slug_from_path), user: User = Depends(get_current_user_authorizer()), comments_repo: CommentsRepository = Depends( get_repository(CommentsRepository)), ) -> CommentInResponse: comment = await comments_repo.create_comment_for_article( body=comment_create.body, article=article, user=user) return CommentInResponse(comment=comment)
async def list_comments_for_article( article: Article = Depends(get_article_by_slug_from_path), user: Optional[User] = Depends( get_current_user_authorizer(required=False)), comments_repo: CommentsRepository = Depends( get_repository(CommentsRepository)), ) -> ListOfCommentsInResponse: comments = await comments_repo.get_comments_for_article(article=article, user=user) return ListOfCommentsInResponse(comments=comments)
async def retrieve_current_user( user: User = Depends(get_current_user_authorizer()), ) -> UserInResponse: token = jwt.create_access_token_for_user(user, str(config.SECRET_KEY)) return UserInResponse(user=UserWithToken( name=user.name, email=user.email, bio=user.bio, image=user.image, token=token, ), )
async def get_articles_for_user_feed( limit: int = Query(DEFAULT_ARTICLES_LIMIT, ge=1), offset: int = Query(DEFAULT_ARTICLES_OFFSET, ge=0), user: User = Depends(get_current_user_authorizer()), articles_repo: ArticlesRepository = Depends( get_repository(ArticlesRepository)), ) -> ListOfArticlesInResponse: articles = await articles_repo.get_articles_for_user_feed(user=user, limit=limit, offset=offset) return ListOfArticlesInResponse(articles=articles, articles_count=len(articles))
async def create_comment_for_announcement( comment_create: CommentInCreate = Body(..., embed=True, alias="comment"), user: User = Depends(get_current_user_authorizer()), comments_repo: CommentsRepository = Depends( get_repository(CommentsRepository)), ) -> CommentInResponse: comment = await comments_repo.create_comment_for_announcement( body=comment_create.body, announcement=None, user=user, ) return CommentInResponse(comment=comment)
async def get_profile_by_username_from_path( username: str = Path(..., min_length=1), user: Optional[User] = Depends(get_current_user_authorizer(required=False)), profiles_repo: ProfilesRepository = Depends(get_repository(ProfilesRepository)), ) -> Profile: try: return await profiles_repo.get_profile_by_username( username=username, requested_user=user, ) except EntityDoesNotExist: raise HTTPException( status_code=HTTP_404_NOT_FOUND, detail=strings.USER_DOES_NOT_EXIST_ERROR, )
async def update_current_user( user_update: UserInUpdate = Body(..., embed=True, alias="user"), current_user: User = Depends(get_current_user_authorizer()), ) -> UserInResponse: if user_update.username and user_update.username != current_user.username: if await check_username_is_taken(user_update.username): raise HTTP_400_BAD_REQUEST_Exception(strings.USERNAME_TAKEN) if user_update.email and user_update.email != current_user.email: if await check_email_is_taken(user_update.email): raise HTTP_400_BAD_REQUEST_Exception(strings.EMAIL_TAKEN) user = update_user(current_user, user_update) return UserInResponse.from_user(user)
async def list_articles( articles_filters: ArticlesFilters = Depends(get_articles_filters), user: Optional[User] = Depends(get_current_user_authorizer(required=False)), articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)), ) -> ListOfArticlesInResponse: articles = await articles_repo.filter_articles( tag=articles_filters.tag, author=articles_filters.author, favorited=articles_filters.favorited, limit=articles_filters.limit, offset=articles_filters.offset, requested_user=user, ) return ListOfArticlesInResponse(articles=articles, articles_count=len(articles))
async def get_article_by_slug_from_path( slug: str = Path(..., min_length=1), user: Optional[User] = Depends( get_current_user_authorizer(required=False)), articles_repo: ArticlesRepository = Depends( get_repository(ArticlesRepository)), ) -> Article: try: return await articles_repo.get_article_by_slug(slug=slug, requested_user=user) except EntityDoesNotExist: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=strings.ARTICLE_DOES_NOT_EXIST_ERROR, )
async def retrieve_current_user( user: User = Depends(get_current_user_authorizer()), settings: AppSettings = Depends(get_app_settings), ) -> UserInResponse: token = jwt.create_access_token_for_user( user, str(settings.secret_key.get_secret_value()), ) return UserInResponse(user=UserWithToken( username=user.username, email=user.email, bio=user.bio, image=user.image, token=token, ), )
async def get_comment_by_id_from_path( comment_id: int = Path(..., ge=1), user: Optional[User] = Depends( authentication.get_current_user_authorizer(required=False), ), comments_repo: CommentsRepository = Depends( database.get_repository(CommentsRepository), ), ) -> Comment: try: return await comments_repo.get_comment_by_id( comment_id=comment_id, user=user, ) except EntityDoesNotExist: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=strings.COMMENT_DOES_NOT_EXIST, )
async def follow_for_user( profile: Profile = Depends(get_profile_by_username_from_path), user: User = Depends(get_current_user_authorizer()), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfileInResponse: if user.username == profile.username: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_FOLLOW_YOURSELF) if profile.following: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_ALREADY_FOLLOWED) await profiles_repo.add_user_into_followers(target_user=profile, requested_user=user) return ProfileInResponse(profile=profile.copy(update={"following": True}))
async def remove_article_from_favorites( article: Article = Depends(get_article_by_slug_from_path), user: User = Depends(get_current_user_authorizer()), articles_repo: ArticlesRepository = Depends( get_repository(ArticlesRepository)), ) -> ArticleInResponse: if article.favorited: await articles_repo.remove_article_from_favorites(article=article, user=user) return ArticleInResponse(article=article.copy( update={ "favorited": False, "favorites_count": article.favorites_count - 1, })) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=strings.ARTICLE_IS_NOT_FAVORITED)
async def unsubscribe_from_user( profile: Profile = Depends(get_profile_by_username_from_path), user: User = Depends(get_current_user_authorizer()), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfileInResponse: if user.username == profile.username: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_UNSUBSCRIBE_FROM_YOURSELF, ) if not profile.following: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_NOT_FOLLOWED) await profiles_repo.remove_user_from_followers(target_user=profile, requested_user=user) return ProfileInResponse(profile=profile.copy(update={"following": False}))
async def mark_article_as_favorite( article: Article = Depends(get_article_by_slug_from_path), user: User = Depends(get_current_user_authorizer()), articles_repo: ArticlesRepository = Depends( get_repository(ArticlesRepository)), ) -> ArticleInResponse: if not article.favorited: await articles_repo.add_article_into_favorites(article=article, user=user) return ArticleInResponse(article=article.copy( update={ "favorited": True, "favorites_count": article.favorites_count + 1, })) raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=strings.ARTICLE_IS_ALREADY_FAVORITED, )
async def update_current_user( user_update: UserInUpdate = Body(..., embed=True, alias="user"), current_user: User = Depends(get_current_user_authorizer()), users_repo: UsersRepository = Depends(get_repository(UsersRepository)), ) -> UserInResponse: if user_update.username and user_update.username != current_user.username: if await check_username_is_taken(users_repo, user_update.username): raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USERNAME_TAKEN) if user_update.email and user_update.email != current_user.email: if await check_email_is_taken(users_repo, user_update.email): raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.EMAIL_TAKEN) user = await users_repo.update_user(user=current_user, **user_update.dict()) token = jwt.create_access_token_for_user(user, str(config.SECRET_KEY)) return UserInResponse(user=UserWithToken(**user.dict(), token=token))
async def create_new_article( article_create: ArticleInCreate = Body(..., embed=True, alias="article"), user: User = Depends(get_current_user_authorizer()), articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)), ) -> ArticleInResponse: slug = get_slug_for_article(article_create.title) if await check_article_exists(articles_repo, slug): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=strings.ARTICLE_ALREADY_EXISTS, ) article = await articles_repo.create_article( slug=slug, title=article_create.title, description=article_create.description, body=article_create.body, author=user, tags=article_create.tags, ) return ArticleInResponse(article=article)
async def retrieve_current_user(user: UserBase = Depends( get_current_user_authorizer())) -> UserInResponse: return UserInResponse.from_user(user)
async def retrieve_current_user(user: User = Depends( get_current_user_authorizer())) -> UserInResponse: token = jwt.create_access_token_for_user(user, str(config.SECRET_KEY)) return UserInResponse(user=UserWithToken(**user.dict(), token=token))