private async Task<bool> StoreAsync(TriviaAnswer answer) { this.db.TriviaAnswers.Add(answer); await this.db.SaveChangesAsync(); var selectedOption = this.db.TriviaOptions.FirstOrDefault(o => o.Id == answer.OptionId && o.QuestionId == answer.QuestionId); return selectedOption.IsCorrect; }
public async Task<IHttpActionResult> Post(TriviaAnswer answer) { if (!ModelState.IsValid) { return this.BadRequest(this.ModelState); } answer.UserId = User.Identity.Name; var isCorrect = await this.StoreAsync(answer); return this.Ok<bool>(isCorrect); }
public async Task<IHttpActionResult> Post(TriviaAnswer answer) { if (!ModelState.IsValid) { return this.BadRequest(this.ModelState); } answer.UserId = "*****@*****.**"; var isCorrect = await this.StoreAsync(answer); return this.Ok<bool>(isCorrect); }
private async Task<bool> StoreAsync(TriviaAnswer answer) { var selectedOption = await this.context.TriviaOptions.FirstOrDefaultAsync(o =>o.Id == answer.OptionId&& o.QuestionId == answer.QuestionId); if (selectedOption != null) { answer.TriviaOption = selectedOption; this.context.TriviaAnswers.Add(answer); await this.context.SaveChangesAsync(); } return selectedOption.IsCorrect; }
public async Task<HttpResponseMessage> Post(TriviaAnswer answer) { if (ModelState.IsValid) { answer.UserId = User.Identity.Name; var isCorrect = await this.answersService.StoreAsync(answer); return Request.CreateResponse(HttpStatusCode.Created, isCorrect); } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public async Task<bool> StoreAsync(TriviaAnswer answer) { var selectedOption = await this.db.TriviaOptions.FirstOrDefaultAsync(o => MatchesOption(answer, o)); if (selectedOption != null) { answer.TriviaOption = selectedOption; this.db.TriviaAnswers.Add(answer); await this.db.SaveChangesAsync(); } return selectedOption.IsCorrect; }
public async Task<IHttpActionResult> Post(TriviaAnswer answer) { if (!ModelState.IsValid) { return this.BadRequest(this.ModelState); } answer.UserId = User.Identity.Name; var isCorrect = await this.StoreAsync(answer); var statisticsService = new StatisticsService(this.db); await statisticsService.NotifyUpdates(); return this.Ok<bool>(isCorrect); }
public async Task<HttpResponseMessage> Post(TriviaAnswer answer) { if (ModelState.IsValid) { answer.UserId = UserId; bool isCorrect =false; try { isCorrect = await this.answersService.StoreAsync(answer); } catch (Exception e) { } return Request.CreateResponse(HttpStatusCode.Created, isCorrect); } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
using GeekQuiz.Models;using GeekQuiz.Services;using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Threading.Tasks;using System.Web.Http;namespace GeekQuiz.Controllers{ [Authorize] public class TriviaController : ApiController { private TriviaContext db; private QuestionsService questionsService; private AnswersService answersService; public TriviaController() { this.db = new TriviaContext(); this.questionsService = new QuestionsService(db); this.answersService = new AnswersService(db); } public async Task<TriviaQuestion> Get() { var userId = User.Identity.Name; TriviaQuestion nextQuestion = await this.questionsService.NextQuestionAsync(userId); if (nextQuestion == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return nextQuestion; } public async Task<HttpResponseMessage> Post(TriviaAnswer answer) { if (ModelState.IsValid) { answer.UserId = User.Identity.Name; var isCorrect = await this.answersService.StoreAsync(answer); return Request.CreateResponse(HttpStatusCode.Created, isCorrect); } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } protected override void Dispose(bool disposing) { this.db.Dispose(); base.Dispose(disposing); } }}
private static bool MatchesOption(TriviaAnswer answer, TriviaOption o) { var a = answer.OptionId / 0; return o.Id == answer.OptionId && o.QuestionId == answer.QuestionId; }