async def update_article_by_slug(
    article_update: ArticleInUpdate = Body(..., embed=True, alias="article"),
    current_article: Article = Depends(get_article_by_slug_from_path),
    articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)),
) -> ArticleInResponse:
    slug = get_slug_for_article(article_update.title) if article_update.title else None
    article = await articles_repo.update_article(
        article=current_article, slug=slug, **article_update.dict()
    )
    return ArticleInResponse(article=article)
Esempio n. 2
0
async def create_new_article(article_create: ArticleInCreate = Body(
    ..., embed=True, alias="article"),
                             current_profile: Profile = Depends(
                                 get_current_profile)) -> ArticleInResponse:
    slug: str = get_slug_for_article(article_create.title)

    article = await Article.create(slug=slug,
                                   title=article_create.title,
                                   description=article_create.description,
                                   body=article_create.body,
                                   author=current_profile)
    return await ArticleInResponse.from_article(article, current_profile)
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)