I am making a small polls app using yeoman's angular-fullstack generator. I have setup some dummy data for polls. When the user clicks a poll, they get routed to a view page where they can input their choice based on the answers set for the question.
Currently, when the user chooses an option and presses submit, the change updates on the client-side, but I am having trouble knowing what to put for the database POST request.
Here is the view
view:
<div class="container">
<h3>{{poll.title}}</h3>
<form ng-submit="submitForm()">
<div ng-repeat="answer in poll.answers">
<label><input type="radio" name="option" ng-model="radioData.index" value="{{$index}}"/>
{{ answer.value }} - {{ answer.votes }} Votes
</label>
</div>
<button class="btn btn-success" type="submit">Vote!</button>
</form>
</div>
And here is its controller:
'use strict';
angular.module('angFullstackCssApp')
.controller('ViewCtrl', function ($scope, $routeParams, $http) {
$http.get('/api/polls/' + $routeParams._id).success(function (poll) {
console.log(poll);
$scope.poll = poll;
$scope.radioData = {
index: 0
};
$scope.viewPoll = true;
$scope.alreadyVoted = false;
$scope.submitForm = function () {
$scope.alreadyVoted = true;
console.log($scope.radioData.index);
$scope.poll.answers[$scope.radioData.index].votes += 1;
// Change database entry here
};
});
});
Here is my poll schema:
var PollSchema = new Schema({
creator: String,
title: String,
answers: [{
value: String,
votes: Number
}]
});
So my questions are:-
How do I write a suitable POST request to increment the vote for the answer by one?
Is it correct to be writing my code within this GET request as I am doing? It feels like there must be a more appropriate way to do it, especially due to the fact I am calling another request within it.