Search posts, tags, users, and pages
Hi Kobby.
When a user registers on your app, you can set a default field hasVoted to be false on the user's document/object. So when the user proceeds to vote, you first check the hasVoted field whether it is false or true,
In code this will look like.
const user = {
name: "John Doe",
email: "test@email.com",
hasVoted: false
}
if(user.hasVoted){
// they have already voted, we cant allow them to proceeded further.
alert( 'You have already voted');
}else{
// they can now proceed to vote
...
}
I'm not familiar with flutter/dart. I wrote the above in javascript.
If you have any questions, let me know. Cheers :)
Thanks for responding to my question.
Per your response, it means i have to create a hasVoted field for each user isnt it?
Again i wanted to know will the code you have suggested be a class on it on? If No, where do you suggest is appropriate place (within my code presented in my question above) to fix it?
Yes, each user needs to have a hasVoted field. This approach is highly dependent on what you are building. I'll assume that you're building something in between
if Scenario 1 is the case, Then the suggestion of using the hasVoted field should work perfectly
So the general idea is this:
When a user signs up on your app, you basically have a database where all the your users are stored right?. That is the point where you should save the hasVoted field which should be false by default.
You need to have the validation code at both levels(server and app). When the user logs in, the app should fetch the user's data. then you can do the validation on the app side. On the server when the voting route is called, you can also do the validation there as well.
If case 2 is your case, I'll suggest another approach.
If you have any questions, let me know. Cheers :)
Olalekan Adekola Thanks for the response
The app is a voting app for a school. There are numerous positions like: President, Secretary, Finance, etc. What i want is for users to be able to vote in each category/position only once after login or registering on the app. At the moment, users can vote more than once and that is not what i want.
I think scenario 2 fit what i want the app to do.
Could you please brief me on scenario 2 also?
Thanks cheers :)
Great! I don't what type of database you use or how you store your data. To implement case 2, you will need to have a table/collection that stores the votes.
Assume we have a user and poll like below
const user = {
id: 1,
email: 'email@test.com',
name: 'Gon Frecss'
}
const poll = {
id: 1,
title: 'Who should be the president'
options: [{
name: 'Roosevelt',
voteCount: 23,
}, {
name: 'Damian',
voteCount: 19,
}]
}
When a user votes, we create a vote document and save it to the database. so the vote document will look like this.
const vote = {
id: <user.id>_<poll:id> // this way we can guarantee uniqueness,
time: Date()
}
So anytime a user attempts to cast a vote on a specific poll, we just need to check our database to see if that user has voted before. so we do something like this.
Vote.findOne({ id: <user.id>_<poll.id>})
if we find an existing vote, we can no longer allow the user the proceed. we need to tell that they have already voted on this poll.
Note: this validation can and must be done at the server level.
Olalekan Adekola You are gradually becoming a blessing to me bro
I am using Firestore as my backend. Lemme show you a pick of my backend and then see how the suggestion you just made can be applied. The link below will lead you to a pic of my database. In the backend i have collections (tables - president, secretary, porter, finance, organizer, voters. (the voters are the users and the positions are reserved for candidates ) and document.
From what I can see, your app communicates to firestore directly via the firestore sdk right?
With your current design, I see you have a collection for each category: finance, porter, etc. If you wanted to add more categories, you will need to create more collections which I think is not the best way. I don't think you need to maintain multiple collections, you just need three collections.
I think you should store your data like.
// user
{
id: "fdkja298",
name: 'Barragen King',
email: 'hollow@gmail.com'
}
A poll can contain the category it's intended for. it should look like this.
// poll
{
id: "JKFhiu42l",
title: 'Who should be elected as the secretary',
category: 'secretary',
options: [ "Russell", "Anderson" , "Brian" ]
}
Then the vote will look something like below;
{
id: <user.id>:<poll.id> //easy way to ensure that a user cannot vote more than once a single poll,
option: "Brian" // the option the user selected.
time: Date(),
user_id: user.id,
poll_id: pool.id
}
Then you can have a firestore query, that can return all the votes for a particular poll.
const votes = firestore.collection('votes').where('poll_id', '==', poll.id).get();
Then you write a function that collates all the results. for example.
const options = [ "Russell", "Anderson" , "Brian" ] // an Array or Map of the poll's options
// then you can create a map that corresponds to the options, in dart I think it should Map<string,number>
const totalResults = {
"russell" : 0,
"anderson": 0,
"brian": 0
}
for(const vote of votes){
const votedOption = vote.option
if(totalResults[votedOption]){ // check if the option exists in the totalResults map.
totalResults[votedOption] = totalResults[votedOption] + 1 // increment option in the totalResults Map
}
}
In the end your totalResults will look something like.
{
"russell" : x, // where 'x' is total number of people that voted russell
"anderson": y, // where 'y' is total number of people that voted anderson
"brian": z // where 'z' is total number of people that voted brian
}
f
Then you should be able to display this in the app.
Olalekan Adekola Thanks for the reply. Lemme put this to work and get back to you please.
Kobby Discount It been a while replying to this and that is because i was busy putting your suggestions to work. I had to create a complete small App to try out your suggestion though it did not work as i expected, i was impressed with the outcome
It did not work on my main project though but I am convinced that if you did know flutter/dart, you would have helped me willingly. Once again am grateful for your assistance.
Thanks bro.