-: So let's go ahead and make the changes
to the database that we need to make.
Let's start with our database changes in poll.py.
We need get poll options and get poll.
So I'm gonna go over to database.py down here.
I'm gonna add a quick comment just to say polls,
and then we're going to start creating our functions here.
I'm actually going to move create poll up to the top.
I'm gonna give you this file afterwards, by the way,
so if you're a bit lost with these changes, don't worry.
I'm gonna give you everything that we've changed over,
and it's all in the e-book as well.
So create poll is up here.
We're gonna have get polls.
We need one for getting a single poll,
so we're gonna do get poll.
Takes in a connection and a poll ID,
and that should be an integer,
and returns a poll, and then it just uses a connection
with connection dot cursor as cursor,
and execute.
So cursor execute.
We're gonna execute select poll,
notice that doesn't exist, with poll ID.
Then at the end we return cursor dot fetch one.
Alright, so let's create select poll.
Just like that, we're gonna select star from polls,
where the ID is equal to the question mark.
Or, actually, this should be percent S, my apologies.
Still in SQLite mode.
So there we have our query to select a single poll,
and now we've got the get poll function there.
Something important to note and that can be a bit confusing
is this poll return here.
Remember, this is the poll type that we defined,
and it's not the poll class,
because the database is still very much
working with tuples and not with classes.
That is what the model classes are for.
We no longer need get poll details,
but we do need get poll options,
so we're going to change this into get poll options,
and it does very much the same thing
but now it returns a list of options.
We do have to create that, and again,
all it does is use the connection, use the cursor,
and then it's gonna select poll options,
not poll with options, passing in the poll ID
and then fetch everything.
We now have to go ahead and create these, too.
So what we're gonna do is down here,
we're gonna do option equal a tuple of INT S-T-R and INT.
We no longer need poll with option,
and we do need the query.
So instead of select poll with options,
we're just gonna do select poll options,
and it's gonna be simpler.
We just have to select star from options
where poll ID is equal to that.
So we'll do where poll ID is equal to that.
We don't need to join anymore,
and we can actually make this a single string.
So you can see that by removing the join,
this query and that return is now
only going to return options.
So when we use that in here, that is just going
to give us the option data,
and we are then passing that into the option constructor
which gives us an option object,
and that is what we're putting
into our list that we return.
So later on, app.py is going to be dealing
with poll and option objects.
So that's everything for the poll.py class.
Now we can move onto option.py,
make sure that we've got everything that we need
from the database.
So we've got add option and get option
and get votes for option that we need to add.
So let's go into database.py and add that.
So here underneath get poll options
I'm going to add another comment for options,
and note that we don't really need a lot of these things
so we're gonna be deleting them in a moment,
but I'm gonna go ahead and do get option.
That takes a connection and an option ID,
which is an integer, and returns an option,
and we also need add option, which takes a connection,
option text, poll ID, and again,
we're gonna do pass in there.
Once we've got that, what else are we missing?
Get votes for option.
So we're gonna be adding that as well.
So if we go over to database.py down here,
we're gonna add a comment for votes
and we're gonna do get votes for option.
Takes in a connection and an option ID as well,
and is gonna return a list of votes.
Alright, what else do we need?
We need add poll vote.
So we're gonna make sure that that's there,
and we can see it.
It's down here, so all we have to do now
is delete the rest.
We're no longer gonna use the database
to get a random poll vote, and we're no longer
gonna get poll and vote results together,
so we can get rid of those.
While I'm here I'm noticing this red underline here.
This should just be returning a poll and not a list of polls
so we actually have to change this query.
Instead of select latest poll with options,
now we're just gonna change this to select latest poll,
just like that, and the query has to change slightly.
We simply find a bit in that it no longer needs the join.
Alright, let's make sure to use that down here.
Cool, now let's go and implement
the get option and add option functions.
I've gone ahead and written these in.
We've got select option being called here
to fetch one individual option,
and add option just uses the insert option query
that already existed, so all we have to do
is go up here and find a way to select that single option,
which we know how to do.
We can just do select option equal
select star from options where ID equal percent S.
Finally, for get votes for option,
all we have to do, again, is call the appropriate query.
So that's select vote for options.
That doesn't exist yet, but we can make it.
We'll do select votes for option equal
select star from votes where option ID equal percent S.
So select poll vote details is no longer necessary,
and you can start to see, the other thing we don't need
is, of course, select random vote.
We've got rid of that function,
so we're gonna get rid of that query,
and we're gonna calculate the random votes using Python.
And something else we don't need
is the poll results data type here,
so I'm just gonna get rid of that.
Something I forgot to change here when creating polls
is that we no longer need to pass in
a list of options in here, and indeed,
the create poll only needs to grab
the insert poll return ID.
So what we're gonna do is we're going to delete that
and then return poll ID.
Now when we call this, we're gonna be doing that
from poll.py, so do make sure that we fix this.
Here we need to pass in a connection and that's about it.
In option.py I actually forgot the same thing,
so make sure to pass the connection in there.
Just pay attention to the PyCharm type hints there
that I clearly didn't,
and then let's go over to add option here
and we're gonna make sure that we're returning
the ID that the option method needs.
So we're gonna add returning ID here to the query name,
and then we're gonna do option ID
is equal to cursor dot fetch one zero,
and we're gonna return option ID.
Then I'm gonna copy that, go up to the top,
and just rename this here.
I'm actually gonna rename it to match poll there,
so just return ID there.
The values are gonna be percent S, percent S,
and returning ID.
Now let's copy that and go back down.
Make sure that this is matching.
It's good to have consistency there in the code.
Alright, that's all the changes we have to make.
Now we're ready to go over to app.py.
So I'll see you in the next video for that.