public async Task <IActionResult> RankArticle(ArticleRankingRequest model) { try { if (model == null || String.IsNullOrWhiteSpace(model.ArticleId)) { return(BadRequest(new { message = "Incomplete ranking information" })); } var ranking = await databaseService.GetArticleRankingAsync(model.ArticleId, currentUserId); if (ranking == null) { ranking = new ArticleRanking(model.ArticleId, currentUserId); } switch (model.Rank) { case RankEnum.Like: ranking.DidILike = !ranking.DidILike; if (ranking.DidILike) { ranking.DidIDislike = false; } break; case RankEnum.Dislike: ranking.DidIDislike = !ranking.DidIDislike; if (ranking.DidIDislike) { ranking.DidILike = false; } break; case RankEnum.Favor: ranking.DidIFavor = !ranking.DidIFavor; break; } await databaseService.SaveArticleRankingAsync(ranking); var rankings = await databaseService.GetArticleRankingsAsync(model.ArticleId); var rankingDetails = new ArticleRankingDetails(model.ArticleId, currentUserId, rankings); return(Ok(rankingDetails)); } catch (Exception ex) { logger.LogError(ex, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> GetArticleRanking(string articleId) { try { if (String.IsNullOrWhiteSpace(articleId)) { return(BadRequest(new { message = "Article ID is missing" })); } var rankings = await databaseService.GetArticleRankingsAsync(articleId); var articleRankingDetails = new ArticleRankingDetails(articleId, currentUserId, rankings); return(Ok(articleRankingDetails)); } catch (Exception ex) { logger.LogError(ex, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } }