public HttpResponseMessage PostProduct(UserQuestions item) { item.question = db.Questions.Find(item.QuestionId); item.user = db.UserProfiles.Find(item.UserId); if (item.question.Correct == item.answered) { item.correct = true; } else { item.correct = false; } item = db.UserQuestions.Add(item); db.SaveChanges(); var response = Request.CreateResponse<UserQuestions>(HttpStatusCode.Created, item); return response; }
public JsonResult SubmitBet(int questionId, int betAmt) { HttpResponseMessage res; int uid = (int)Session["currentUser"]; new LogEvent("questionId | SubmitBet " + questionId).Raise(); new LogEvent("betAmt | SubmitBet " + betAmt).Raise(); new LogEvent("uid | SubmitBet " + uid).Raise(); if (questionId != null && betAmt != null && uid != null) { new LogEvent("In If | SubmitBet ").Raise(); UserProfile up = (from u in db.UserProfiles where u.UserId == uid select u).FirstOrDefault(); //UserProfile up = db.UserProfiles.Find(uid); new LogEvent("up | SubmitBet " + up.UserName).Raise(); //Check if the question is already in UserQuestion table UserQuestions uq = (from u in db.UserQuestions where u.UserId == uid && u.QuestionId == questionId select u).FirstOrDefault(); new LogEvent("uq | SubmitBet " + uq).Raise(); //If not, create the UserQuestion if (uq == null) { new LogEvent("Creating uq | SubmitBet ").Raise(); uq = new UserQuestions { UserId = uid, QuestionId = questionId, betAmount = betAmt }; uq.answered = 0; uq.correct = false; uq.question = db.Questions.Find(uq.QuestionId); uq.question.CategoryId = uq.question.Category.CategoryId; uq.user = up; db.UserQuestions.Add(uq); if (uq.question == null) { uq.question = db.Questions.Find(uq.QuestionId); } new LogEvent("Created uq | SubmitBet " + uq.QuestionId).Raise(); } if (uq.question == null) { uq.question = db.Questions.Find(uq.QuestionId); } //check if the User and Category combo is already in the table var query = from u in db.UserCategories where u.UserId == uid && u.CategoryId == uq.question.CategoryId select u; UserCategories uc = query.FirstOrDefault(); //if not, add the user and category combo into the table if (uc == null) { uc = new UserCategories { UserId = uid, totalQuestionsAnswered = 0 }; uc.category = uq.question.Category; uc.CategoryId = uq.question.CategoryId; uc.user = up; uc.totalQuestionsAnswered = 0; db.UserCategories.Add(uc); // db.SaveChanges(); } //increment the answered questions in the in the user category combo uc.totalQuestionsAnswered += 1; //get the UserBet and subtract the betAmt for the user UserBet ub = db.UserBet.Find(uid); ub.BetAmt -= betAmt; db.SaveChanges(); res = new HttpResponseMessage(); res.StatusCode = HttpStatusCode.Accepted; new LogEvent("Working | SubmitBet").Raise(); } else { res = new HttpResponseMessage(); res.StatusCode = HttpStatusCode.ExpectationFailed; new LogEvent("Failure | SubmitBet").Raise(); } //return RedirectToAction("ShowQuestion", "Game"); new LogEvent("coming to an end | SubmitBet").Raise(); return Json(new { redirectToUrl = Url.Action("ShowQuestion", "Game") }); //return Json(new { redirectToUrl = "http://quiz-6.apphb.com/Game/ShowQuestion" }); }