I am a software engineer with 5 years experience building products.
Nothing here yet.
I made the switch from Javascript to Typescript a while ago and I have never looked back, the developer experience is smooth. I use the express framework which is very lightweight and has good integration with Typescript. Give it a shot, I can't wait to see what you build. Cheers :)
Kobby Discount 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. 1 users - The voters. 2 Polls - What is being voted on. 3 Votes - The vote. 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.
Kobby Discount 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.
Kobby Discount 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 Scenario 1 - A voting app, where a user can only vote once on a single poll Scenario 2 - A voting app, where a user can vote once on different polls , 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 :)
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 :)