1 00:00:00,150 --> 00:00:02,610 For the final step in our book voting system, 2 00:00:02,610 --> 00:00:05,190 I want to update the way our voting works. 3 00:00:05,190 --> 00:00:08,180 Only authenticated users should be able to vote, and 4 00:00:08,180 --> 00:00:11,120 any user can only vote once per book. 5 00:00:11,120 --> 00:00:14,215 But that user is always able to change their vote. 6 00:00:14,215 --> 00:00:17,584 Open the book.php file in the inc folder. 7 00:00:17,584 --> 00:00:21,121 Around the arrows, let's check if the user is authenticated. 8 00:00:39,697 --> 00:00:44,643 Next, we want to update our arrows to show the way the current user has voted. 9 00:00:44,643 --> 00:00:48,854 We're going to change the color of the arrows if our user's vote corresponds 10 00:00:48,854 --> 00:00:50,557 to one of the vote directions. 11 00:00:58,063 --> 00:01:04,570 If ($book['myVote'] = 1, 12 00:01:08,518 --> 00:01:14,442 Then we're going to set the style="color:orange". 13 00:01:24,250 --> 00:01:28,788 We'll do the same thing to the down arrow, only we'll change it to -1. 14 00:01:31,007 --> 00:01:35,409 To get my vote, we need to update the get all books function. 15 00:01:37,618 --> 00:01:38,974 First we need the userId. 16 00:01:42,773 --> 00:01:48,262 Let's set the default to 0, then, if (isAuthenticated, 17 00:01:55,270 --> 00:02:02,011 Then we can set the userId equal to the userId in our jwt, 18 00:02:02,011 --> 00:02:05,890 we'll decodeJwt and pass sub. 19 00:02:07,960 --> 00:02:11,690 Now we need to update our query statement by adding an inner select statement. 20 00:02:14,140 --> 00:02:15,210 Make sure you add a comma. 21 00:02:18,380 --> 00:02:25,586 And then in parentheses, we're going to add SELECT value FROM votes. 22 00:02:29,921 --> 00:02:36,688 WHERE votes.book_id=books.id. 23 00:02:40,176 --> 00:02:46,480 AND votes.user_id=userId. 24 00:02:48,290 --> 00:02:51,410 And this will be as myVote. 25 00:02:52,480 --> 00:02:53,854 Now we need to bind our variable. 26 00:03:08,470 --> 00:03:12,900 Next we want to clear the votes for the current user and book combination. 27 00:03:12,900 --> 00:03:15,070 So, we don't have duplicate votes for our user. 28 00:03:17,290 --> 00:03:18,320 Let's go down to our vote. 29 00:03:19,760 --> 00:03:22,340 Below the vote function, let's add a new function. 30 00:03:23,870 --> 00:03:25,966 We'll call this clearVote. 31 00:03:28,956 --> 00:03:30,226 And we'll pass the bookId. 32 00:03:35,629 --> 00:03:40,086 We'll need our global $db. 33 00:03:40,086 --> 00:03:44,094 And once again, we'll pull the userId from our Jwt. 34 00:04:13,288 --> 00:04:19,280 For our query, we'll DELETE FROM 35 00:04:19,280 --> 00:04:25,685 votes WHERE book_id = :bookId AND 36 00:04:25,685 --> 00:04:29,826 user_id = :userId. 37 00:04:36,717 --> 00:04:41,931 We'll prepare our query, And then bind our variables. 38 00:05:11,513 --> 00:05:15,874 Execute and then we'll return the row count. 39 00:05:22,602 --> 00:05:26,034 This will tell us if we actually removed a vote or not. 40 00:05:26,034 --> 00:05:28,494 Finally, we need to update our vote procedure. 41 00:05:32,213 --> 00:05:36,844 We're going to wrap our switch statement in the clearVote. 42 00:05:36,844 --> 00:05:43,760 if (!clearVote and pass the bookId. 43 00:05:48,881 --> 00:05:49,707 Then we'll make a vote. 44 00:05:53,297 --> 00:05:56,745 We either clear the vote or make a vote. 45 00:05:56,745 --> 00:06:01,214 Finally we need to update our vote function to use the current user to score 46 00:06:01,214 --> 00:06:01,874 the vote. 47 00:06:06,602 --> 00:06:13,040 Instead of the user ID = 0, we'll use our decodeJwt and pass sub. 48 00:06:15,160 --> 00:06:19,430 Great, we're now setting our votes table to record our user, and 49 00:06:19,430 --> 00:06:22,780 we're allowing the user to vote only once. 50 00:06:22,780 --> 00:06:25,628 Let's go back to the browser and view the book list page. 51 00:06:35,184 --> 00:06:39,566 Great, we can now make only a single vote for each book, 52 00:06:39,566 --> 00:06:45,330 either we have no vote, one vote up, Or one vote down.