/// <summary>
        /// Creates a ranking in the leaderboard database.
        /// </summary>
        /// <param name="ranking">Ranking that you are creating.</param>
        public Ranking CreateRanking(PMRanking ranking)
        {
            try
            {
                Ranking newRanking = new Ranking();
                newRanking.Username = ranking.Username;
                newRanking.Rating   = ranking.Rating;
                Leaderboard leaderboard = LeaderboardDBEntities.Leaderboards.Where(x => x.LeaderboardName.Equals(ranking.LeaderboardName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                if (null == leaderboard)
                {
                    Leaderboard newLeaderboard = new Leaderboard();
                    newLeaderboard.LeaderboardName = ranking.LeaderboardName;
                    LeaderboardDBEntities.Leaderboards.Add(newLeaderboard);
                    leaderboard = newLeaderboard;
                }

                newRanking.Leaderboard   = leaderboard;
                newRanking.LeaderboardID = leaderboard.LeaderboardID;
                LeaderboardDBEntities.Rankings.Add(newRanking);
                LeaderboardDBEntities.SaveChanges();
                return(newRanking);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Creates or updates the ranking, if already exists, in the leaderboard database.
        /// </summary>
        /// <param name="ranking">Ranking that you intend to upsert.</param>
        public HttpResponseMessage UpsertRanking([FromBody] PMRanking ranking)
        {
            try
            {
                CheckArgumentExists(ranking.Username, nameof(ranking.Username));
                CheckArgumentExists(ranking.LeaderboardName, nameof(ranking.LeaderboardName));
                CheckRatingNegative(ranking.Rating);

                Ranking foundRanking = LeaderboardDataProvider.FindRanking(ranking.Username, ranking.LeaderboardName);
                if (null == foundRanking)
                {
                    Ranking newRanking = LeaderboardDataProvider.CreateRanking(ranking);
                    var     message    = Request.CreateResponse(HttpStatusCode.Created, ranking);
                    message.Headers.Location = new Uri(Request.RequestUri + newRanking.RankingID.ToString());
                    return(message);
                }
                else
                {
                    LeaderboardDataProvider.UpdateRanking(foundRanking, ranking);
                    return(Request.CreateResponse(HttpStatusCode.OK, ranking));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
 /// <summary>
 /// Updates a ranking in the leaderboard database.
 /// </summary>
 /// <param name="foundRanking">Ranking that you intend to update.</param>
 /// <param name="ranking">Values of the ranking you intend to change.</param>
 public Ranking UpdateRanking(Ranking foundRanking, PMRanking ranking)
 {
     try
     {
         Ranking newRanking = new Ranking();
         newRanking.RankingID = foundRanking.RankingID;
         newRanking.Username  = ranking.Username;
         newRanking.Rating    = ranking.Rating;
         Leaderboard leaderboard = LeaderboardDBEntities.Leaderboards.Where(x => x.LeaderboardName.Equals(ranking.LeaderboardName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
         newRanking.Leaderboard   = leaderboard;
         newRanking.LeaderboardID = leaderboard.LeaderboardID;
         LeaderboardDBEntities.Entry(foundRanking).CurrentValues.SetValues(newRanking);
         LeaderboardDBEntities.SaveChanges();
         return(foundRanking);
     }
     catch (Exception)
     {
         throw;
     }
 }