public IHttpActionResult AddScore(ItemScore score) { Question question = db.Questions.Find(score.id); string userId = User.Identity.GetUserId(); var scoreitems = question.ScoreItems.Where(x => x.UserID == userId).ToList(); if (scoreitems.Count == 0) { int addedScore = Math.Sign(score.score); question.ScoreItems.Add(new ScoreItem() { Added = DateTime.Now, Score = addedScore, UserID = userId, ItemID = question.ID, }); question.Score += addedScore; } else { int addedScore = Math.Sign(score.score); var scoreItem = scoreitems.First(); if (scoreItem.Score != addedScore) { question.Score -= scoreItem.Score; scoreItem.Score = addedScore; question.Score += addedScore; db.Entry(scoreItem).State = EntityState.Modified; } else { question.Score -= scoreItem.Score; db.Entry(scoreItem).State = EntityState.Deleted; } } db.Entry(question).State = EntityState.Modified; db.SaveChanges(); int currentScore = question.Score; int currentBadgeRarity = PointsUtils.getCurrentScoreBadgeRarity(currentScore); UserLevel userLevel = question.AspNetUser.UserLevels.Single(x => x.CategoryID == question.CategoryID); UserBadge userBadge = userLevel.UserBadges.SingleOrDefault(x => x.ItemID == question.ID); if (userBadge == null) { PointsUtils.giveBadge(question.AspNetUser.Id, question.CategoryID, "score", currentBadgeRarity, question.ID); } else if (userBadge.Badge.Rarity < currentBadgeRarity) { db.UserBadges.Remove(userBadge); PointsUtils.giveBadge(question.AspNetUser.Id, question.CategoryID, "score", currentBadgeRarity, question.ID); } db.SaveChanges(); return(Ok(new QuestionDTO(question))); }
public void getCurrentBadgeRarityTest4() { actualRarity = PointsUtils.getCurrentScoreBadgeRarity(1024); expectedRarity = 3; Assert.AreEqual(expectedRarity, actualRarity); }