コード例 #1
0
        public IHttpActionResult PutMyPerformance(UserPerformance userPerformance)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (User.Identity.Name != userPerformance.UserId)
            {
                return(BadRequest());
            }

            UserPerformance currentPerformance = db.UserPerformances.First(x => x.UserId == userPerformance.UserId);

            if (currentPerformance != null)
            {
                userPerformance.CorrectAnswerCount += currentPerformance.CorrectAnswerCount;
                userPerformance.TotalGamesCount     = currentPerformance.TotalGamesCount + 1;
                userPerformance.TotalQuestions     += currentPerformance.TotalQuestions;
            }


            //db.Entry(userPerformance).State = EntityState.Modified;

            try
            {
                db.Set <UserPerformance>().AddOrUpdate(userPerformance);
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserPerformanceExists(userPerformance.UserId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }