public async Task <ActionResult> Vote(int id, int question, int answer) { if (question < 1 || answer < 1) { return(BadRequest("Index values must start at 1")); } try { await RegisterVote(id, question, answer); return(new NoContentResult()); } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (PollClosedException) { return(BadRequest("Poll is not open for voting")); } catch (Exception e) { _logger.LogError(LoggingEvents.OpenPoll, "Error updating poll status to open {id}: Exception {ex}", id, e.Message); return(StatusCode(500, e.Message)); } }
public async Task <ActionResult> NextQuestion(int id) { try { Poll updatedPoll = await _pollRepository.SetNextQuestionAsync(id); if (updatedPoll != null) { PollResult message = PollHelper.GetPollResults(updatedPoll); await _hubContext.Clients.All.SendAsync(MessageTypes.LOAD_QUESTION, message); _logger.LogInformation(LoggingEvents.PollSetNextQuestion, "Poll {id} next question updated", id); return(Ok(updatedPoll)); } else { return(StatusCode(500, "Error occurred updating the database")); } } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (PollAtLastQuestionException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (Exception e) { _logger.LogError(LoggingEvents.PollSetNextQuestion, "Error setting next question for poll {id}: Exception {ex}", id, e.Message); return(StatusCode(500, e.Message)); } }
public async Task <ActionResult> Open(int id) { try { bool result = await _pollRepository.OpenPollAsync(id); if (result) { _logger.LogInformation(LoggingEvents.OpenPoll, "Poll {id} status set to open", id); return(new NoContentResult()); } else { return(StatusCode(500, "Error occurred updating the database")); } } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (DbUpdateException e) { _logger.LogError($"Error occurred whilst attempting to update poll {id}: {e.Message}"); throw (e); } catch (Exception e) { _logger.LogError(LoggingEvents.OpenPoll, "Error updating poll status to open {id}: Exception {ex}", id, e.Message); return(StatusCode(500, e.Message)); } }
public async Task <ActionResult> GetCurrentPoll() { try { var poll = await _pollRepository.GetFirstOpenPollAsync(); return(Ok(poll.Id)); } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } }
public async Task <ActionResult> ResetPoll(int id) { try { Poll poll = await _pollRepository.GetPollAsync(id); if (poll != null) { // Go through each question and reset the vote counts foreach (Question question in poll.Questions) { foreach (Answer answer in question.Answers) { answer.VoteCount = 0; } } poll.CurrentQuestion = 1; // Update the poll then notify connected clients of the poll status using SignalR poll = await _pollRepository.UpdatePollAsync(poll); PollResult message = PollHelper.GetPollResults(poll); await _hubContext.Clients.All.SendAsync(MessageTypes.RESET_POLL, message); _logger.LogInformation(LoggingEvents.ResetPoll, "Poll {id} has been reset", id); return(Ok(poll)); } else { return(StatusCode(500, "Error occurred updating the database")); } } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (Exception e) { _logger.LogError(LoggingEvents.PollSetNextQuestion, "Error resetting poll {id}: Exception {ex}", id, e.Message); return(StatusCode(500, e.Message)); } }
public async Task <ActionResult> Get(int id) { _logger.LogInformation(LoggingEvents.GetPollDefinition, $"Getting poll definition {id}"); try { var pollDefinition = await _pollDefinitionRepository.GetPollDefinitionAsync(id); return(Ok(pollDefinition)); } catch (PollDefNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (Exception e) { _logger.LogError(LoggingEvents.GetPollDefinition, $"Error getting poll definition {id}: Exception {e.Message}"); return(StatusCode(500, e.Message)); } }
public async Task <ActionResult> GetPollByHandle([FromRoute] string handle) { _logger.LogInformation(LoggingEvents.GetPoll, "Getting poll with handle {handle}", handle); try { var poll = await _pollRepository.GetPollByHandleAsync(handle); return(Ok(poll)); } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (Exception e) { _logger.LogError(LoggingEvents.GetPollByHandle, "Error getting poll {id}: Exception {ex}", handle, e.Message); return(StatusCode(500, e.Message)); } }
public async Task <ActionResult> VoteOnCurrent(int id, int answer) { if (answer < 1) { return(BadRequest("Answer values must start at 1")); } try { // Get the poll to determine the current question var poll = await _pollRepository.GetPollAsync(id); if (poll.Status != PollStatus.Open || poll.CurrentQuestion == 0) { return(BadRequest("Poll is not open for voting")); } await RegisterVote(id, poll.CurrentQuestion, answer); return(new NoContentResult()); } catch (PollClosedException) { return(BadRequest("Poll is not open for voting")); } catch (PollNotFoundException e) { ApiStatusMessage a = ApiStatusMessage.CreateFromException(e); return(BadRequest(a)); } catch (Exception e) { _logger.LogError(LoggingEvents.OpenPoll, "Error updating poll status to open {id}: Exception {ex}", id, e.Message); return(StatusCode(500, e.Message)); } }