async def articles_feed(
    limit: int = Query(20, gt=0),
    offset: int = Query(0, ge=0),
    user: User = Depends(get_current_user_authorizer()),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        dbarticles = await get_user_articles(conn, user.username, limit, offset)
        return create_aliased_response(
            ManyArticlesInResponse(articles=dbarticles, articles_count=len(dbarticles))
        )
Пример #2
0
async def get_comment_from_article(
        slug: str = Path(..., min_length=1),
        user: User = Depends(get_current_user_authorizer(required=False)),
        db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        await get_article_or_404(conn, slug, user.username)

        dbcomments = await get_comments_for_article(conn, slug, user.username)
        return create_aliased_response(
            ManyCommentsInResponse(comments=dbcomments))
async def update_article(
    slug: str = Path(..., min_length=1),
    article: ArticleInUpdate = Body(..., embed=True),
    user: User = Depends(get_current_user_authorizer()),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        await check_article_for_existence_and_modifying_permissions(
            conn, slug, user.username
        )

        async with conn.transaction():
            dbarticle = await update_article_by_slug(conn, slug, article, user.username)
            return create_aliased_response(ArticleInResponse(article=dbarticle))
Пример #4
0
async def create_comment_for_article(
        *,
        slug: str = Path(..., min_length=1),
        comment: CommentInCreate = Body(..., embed=True),
        user: User = Depends(get_current_user_authorizer()),
        db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        await get_article_or_404(conn, slug, user.username)

        async with conn.transaction():
            dbcomment = await create_comment(conn, slug, comment,
                                             user.username)
            return create_aliased_response(
                CommentInResponse(comment=dbcomment))
async def get_article(
    slug: str = Path(..., min_length=1),
    user: Optional[User] = Depends(get_current_user_authorizer(required=False)),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        dbarticle = await get_article_by_slug(
            conn, slug, user.username if user else None
        )
        if not dbarticle:
            raise HTTPException(
                status_code=HTTP_404_NOT_FOUND,
                detail=f"Article with slug '{slug}' not found",
            )

        return create_aliased_response(ArticleInResponse(article=dbarticle))
async def create_new_article(
    article: ArticleInCreate = Body(..., embed=True),
    user: User = Depends(get_current_user_authorizer()),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        article_by_slug = await get_article_by_slug(
            conn, slugify(article.title), user.username
        )
        if article_by_slug:
            raise HTTPException(
                status_code=HTTP_422_UNPROCESSABLE_ENTITY,
                detail=f"Article with slug '{article_by_slug.slug}' already exists",
            )

        async with conn.transaction():
            dbarticle = await create_article_by_slug(conn, article, user.username)
            return create_aliased_response(ArticleInResponse(article=dbarticle))
async def get_articles(
    tag: str = "",
    author: str = "",
    favorited: str = "",
    limit: int = Query(20, gt=0),
    offset: int = Query(0, ge=0),
    user: User = Depends(get_current_user_authorizer(required=False)),
    db: DataBase = Depends(get_database),
):
    filters = ArticleFilterParams(
        tag=tag, author=author, favorited=favorited, limit=limit, offset=offset
    )
    async with db.pool.acquire() as conn:
        dbarticles = await get_articles_with_filters(
            conn, filters, user.username if user else None
        )
        return create_aliased_response(
            ManyArticlesInResponse(articles=dbarticles, articles_count=len(dbarticles))
        )
async def favorite_article(
    slug: str = Path(..., min_length=1),
    user: User = Depends(get_current_user_authorizer()),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        dbarticle = await get_article_or_404(conn, slug, user.username)
        if dbarticle.favorited:
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST,
                detail="You already added this article to favorites",
            )

        dbarticle.favorited = True
        dbarticle.favorites_count += 1

        async with conn.transaction():
            await add_article_to_favorites(conn, slug, user.username)
            return create_aliased_response(ArticleInResponse(article=dbarticle))
async def delete_article_from_favorites(
    slug: str = Path(..., min_length=1),
    user: User = Depends(get_current_user_authorizer()),
    db: DataBase = Depends(get_database),
):
    async with db.pool.acquire() as conn:
        dbarticle = await get_article_or_404(conn, slug, user.username)

        if not dbarticle.favorited:
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST,
                detail="You don't have this article in favorites",
            )

        dbarticle.favorited = False
        dbarticle.favorites_count -= 1

        async with conn.transaction():
            await remove_article_from_favorites(conn, slug, user.username)
            return create_aliased_response(ArticleInResponse(article=dbarticle))