Outline

Solution: Complete the Articles Controller Imlementation

In the Conduit.Api Project complete the implementation of the follow methods in the ArticlesController.cs file.

Get Article Feed

Modify the GetArticlesFeed method by adding an await call to the GetArticlesFeedAsync method in the IArticlesRepository Interface Class.

Return an Ok Response with a new object containing:

  • Article array
  • Article Count

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the GetArticleFeedAsync method:

[HttpGet("feed")]
public async Task<IActionResult> GetArticleFeedAsync([FromQuery] int limit = 20, int offset = 0)
{
    try
    {
        Article[] articles = await ArticlesRepo.GetArticlesFeedAsync(limit, offset);
        return Ok(new { articles, articlesCount = articles.Length });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Get Article by Slug

Modify the GetArticlesBySlug method by adding an await call to the GetArticlebySlugAsync method in the IArticlesRepository Interface Class.

Return an Ok Response with a new object containing:

  • A single Article

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Add an additional Catch Block to handle ArticleNotFoundException:

  • Log as a warning
  • Return a 422 Response

Here's the code to the GetArticleBySlugAsync method:

[HttpGet("{slug}")]
public async Task<IActionResult> GetArticleBySlugAsync([FromRoute] string slug)
{
    try
    {
        Article article = await ArticlesRepo.GetArticleBySlugAsync(slug);
        return Ok(new { article });
    }
    catch (ArticleNotFoundException e)
    {
        Logger.LogWarning(e.Message, e);
        return StatusCode(422, new { error = e.ToDictionary() });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Update Article

Modify the UpdateArticle method by adding an await call to the UpdateArticleAsync method in the IArticlesRepository Interface Class.

Return an Ok Response with a new object containing:

  • The new Article

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the UpdateArticleAsync method:

[HttpPut("{slug}")]
[Authorize]
public async Task<IActionResult> UpdateArticleAsync(
    [FromRoute] string slug,
    [FromBody] ArticleRequest<ArticleUpdateRequest> req
    )
{
    try
    {
        Article article = await ArticlesRepo.UpdateArticleAsync(req.Article);
        return Ok(new { article });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Delete Article

Modify the DeleteArticle method by adding an await call to the DeleteArticleAsync method in the IArticlesRepository Interface Class.

Return a NoContent() Response:

  • No Parameters are needed

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the DeleteArticleAsync method:

[HttpDelete("{slug}")]
[Authorize]
public async Task<IActionResult> DeleteArticleAsync([FromRoute] string slug)
{
    try
    {
        await ArticlesRepo.DeleteArticleAsync(slug);
        return NoContent();
    }
    catch (ArticleNotFoundException e)
    {
        Logger.LogWarning(e.Message, e);
        return StatusCode(422, new { error = e.ToDictionary() });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Get Comments from Article

Modify the GetCommentsFromArticle method by adding an await call to the GetCommentsFromArticleAsync method in the IArticlesRepository Interface Class.

Return an Ok Response with a new object containing:

  • A Comment array

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the AddCommentToArticleAsync method:

[HttpPost("{slug}/comments")]
[Authorize]
public async Task<IActionResult> AddCommentToArticleAsync(
    [FromRoute] string slug,
    [FromBody] CommentRequest<CommentAddRequest> req)
{
    try
    {
        Comment comment = await ArticlesRepo.AddCommenttoArticleAsync(slug, req.Comment);
        return Ok(new { comment });
    }
    catch (ArticleNotFoundException e)
    {
        Logger.LogWarning(e.Message, e);
        return StatusCode(422, new { error = e.ToDictionary() });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Delete Comment for Article

Modify the DeleteCommentsForArticle method by adding an await call to the DeleteCommentsForArticleAsync method in the IArticlesRepository Interface Class.

Return a NoContent() Response:

  • No Parameters are needed

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the DeleteCommentsFromArticleAsync method:

[HttpDelete("{slug}/comments/{id:int}")]
[Authorize]
public async Task<IActionResult> DeleteCommentsFromArticleAsync(
    [FromRoute] string slug,
    [FromRoute] int id)
{
    try
    {
        await ArticlesRepo.DeleteCommentForArticleAsync(slug, id);
        return NoContent();
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Favorite Article

Modify the FavoriteArticle method by adding an await call to the FavoriteArticleAsync method in the IArticlesRepository Interface Class.

Return an Ok Response with a new object containing:

  • A single Article

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the FavoriteArticleAsync method:

[HttpPost("{slug}/favorite")]
[Authorize]
public async Task<IActionResult> FavoriteArticleAsync([FromRoute] string slug)
{
    try
    {
        Article article = await ArticlesRepo.FavoriteArticleAsync(slug);
        return Ok(new { article });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}

Unfavorite Article

Modify the UnfavoriteArticle method by adding an await call to the UnfavoriteArticleAsync method in the IArticlesRepository Interface Class.

Return an Ok Response with a new object containing:

  • A single Article

Fix the errors:

  • Make the method async
  • Have Visual Studio generate the missing method for you

Here's the code to the UnfavoriteArticleAsync method:

[HttpDelete("{slug}/favorite")]
[Authorize]
public async Task<IActionResult> UnfavoriteArticleAsync([FromRoute] string slug)
{
    try
    {
        Article article = await ArticlesRepo.UnfavoriteArticleAsync(slug);
        return Ok(new { article });
    }
    catch (Exception e)
    {
        Logger.LogError(e.Message, e);
        return StatusCode(500, e);
    }
}
 

I finished! On to the next tutorial