So we're going to need some database functions we can call from the back end that will allow us to get
all users to add a user, to get one user to delete the user, so on and so forth.
So let's get started with those functions.
So I'm looking at models that go and I'm at the very bottom.
And the first thing I'll do is create a function with the receiver of MTV model, same as always.
And I'll call this get all users and it takes no parameters and it returns a slice of pointers to user
and potentially there.
And we need our contact stuff.
So I'll copy that from the previous function pasted in here.
And I need somewhere to store my users.
So I'll say var users is of type slice of pointer to user, which is what we return it and I'll write
my query, which is dead.
Simple query is assigned the value of in back ticks select and we're going to select idy.
I'll put it on the next line.
I'd last name, first name email.
And we don't want the password because we're not going to do anything with that hash.
So we'll go with created and updated it and that's being selected from users and we'll order by last
name and then first name.
So that's our query.
Now let's execute that query.
So Rose and Erin are assigned the value of and debug query context.
And we had the context and our query and then we check for an error.
If error is not equal to nil, will return nil because they don't have anything.
And the error and then we'll defer rose close.
OK, assuming we get past that, we can now loop through rows, four rows dot next.
And I'll create a user, a variable to store an individual user in the value of type user and we'll
check for an error while we scan into that variable rose dot scan and we're going to scan into ampersand
Yuda ID lowercase you you idee.
And I'll duplicate that a few times.
So when for last name, first name, email created and updated.
OK, so we're going to scan into it and then last name and then first name and then email and created
and updated that.
And assuming we don't get an error, if error is not equal to nil then we'll return nil.
And the error, if we don't have an error then we can just append that to our slice.
So users equals append users.
And then a reference to you because we're putting a slice of pointers is our return value.
And down here, we return users and nil.
OK, so that's our first function that allows us to get all of the users we want.
Now we also want one that will allow us to get an individual user.
So I say func M pointer to D.V. model, get one user and I'll assume I'm want to get that by ID, which
is an end and will return a user and potentially an error.
So again, I want my contact stuff, so let's get that.
Actually we can copy this whole thing and just modify it.
It's a lot faster so we'll copy that, paste it into here, we'll modify our user variable to be just
a user.
So we'll call this year and our query becomes almost the same.
But instead of autobody, we say where vidi equals questionmark and put in our past parameter of ID.
And we want this to be a query row context, not just returns one row
and this goes away and this goes away.
And I'll just delete all of this and modify it appropriately, so error is equal to rodent skin.
We're getting all of that information
and here we say if error is not equal to nil and will return you and the error, otherwise we return
you and nil.
OK, so now we have one to get all the users, one to get an individual user.
We also need to edit users.
So let's go with func and DB model and its user and that will take a parameter, let's say you dot user
and it will return potentially an error.
And once again we'll copy our contact stuff and paste it in down here, make a statement
and that's a sign the value of.
Update users set, I will say first name equals questionmark, last name equals questionmark,
email equals questionmark.
I'm not going to do the password here.
This will just be to update the basic information for a user.
And then we want updated.
Get rid of that comma where
Pindi equals questionmark.
OK?
So that's the basic statement, let's execute to ignore the first return value, because we don't care
about it, but we do care about an error.
Those are a sign the value of MDVIP don't exact context with the context, then our statement and then
I'll put these on separate lines so it's more readable.
We want you dot first name and then I'll do that a few times.
You dot last name, you dot email and the next one is updated.
And that's just time dot now
and that's where you dot it.
And a comma, if there's an error we'll return it.
If error is not equal to nil, return the error, otherwise return nothing nil.
So now we can edit one.
We also want to insert one.
So I'll copy this whole thing, I'll paste it into here and I'll make this add user.
Now, this time we want the user and we want the password, which will get us a hash, which is a typed
stream, and we will pass that to this function.
And our statement becomes insert into users and we're going to insert first name, last name, email
password created up and update it up and then on the next line.
Values, and we're going to have one, two, three, four, one, two, three, one, two, three,
four, five, six, one, two, three, four, five, six.
And we're going to execute that and we're going to pass in the first name.
You don't last name, you don't email, then you dot password.
Which is actually hash, in our case, hash,
and then find out now get rid of this one and duplicate that one and everything else should be correct.
So now we can now we can get all the users.
We can get one user, we can edit the user, we can insert a user.
And we also need to be able to delete user.
So func and the DB model delete user.
And all we need for that is the idea of a type int and potentially an error is returned.
So let's copy this and paste it in there and make our statement.
Assign the value of delete from users where ID equals questionmark and now we'll execute it.
So we'll say I don't care about the first return value, do care about the error or assign the value
of IMDB.
Not exact context with X and our statement and the idea.
Can we check for an error, which is exactly the same as this.
So I shall copy and paste.
So those are the basic crud routines.
Get all the users, add a user, edit user, delete a user, get one user.
We're all set.
So those are our database functions.
So now we need to go write some logic in the back end.
A handler in the backend that will take some kind of Jason payload or some kind of request, get all
of the users and send them back as Jason and we'll be using the rest of these database functions before
too long.
Let's move on.