So the next step in this process is saving or adding a user.
So we want to save an edited user or we want to insert the new user.
So obviously we need a root and a handler on the backend to make that happen.
So in Stasch API, let's create a route to a non-existent handler and then we'll make the handler.
So it'll be a post route and it will go through API slash admin, slash all dash users, edit, slash
Heidi.
And the idea of course, will be different for every user.
And of course you can use whatever path name you want there and we'll call after edit user, which doesn't
exist yet.
So let's make that so over an handler's dash API ago at the very bottom.
We'll write one handler that handles both situations, so I call this a thunk with a receiver of pointer
to application and we're going to call it edit user and it's a handler.
So w htp dot response writer and R is a pointer to an htp dot request.
Now we need the ID, so we'll get that from the Euro Chheda, Euro Perram and we hand it out, which
is our request and we're looking for ID and we'll convert that to an end user ID.
And again I'm ignoring the error and you should not.
And that sterckx on a two hour and we're looking for it.
Now let's create a variable for our user of type models, dot user, and want to assume that we're getting
all of the information we need pass to us is Jaiswal.
So all we have to do is the error is a the value of after JSON and we give it W R and a reference to
user.
That's what we're reading into and we check for an error and I'll just copy this, which is sufficient
for my purposes
and paste it in there.
So now we have our JSON all parsed into the variable users.
So we'll make a decision here if user ID, which is the value we got on line seven.
Twenty eight in my code, if that's greater than zero, then we're editing an existing user or else
we're going to insert a new user.
So all we have to do here for the first part is to say we're editing an existing user.
So I can just say error is equal to after DB dot edit user and handed the user because that's all that
that means.
And we'll check for an error, so I'll paste that in there.
The other thing we need to take into account is if someone is filled out the password and verify password
fields and we're not just changing first name, last name and email, we're also resetting the password,
but we already have logic in place to do that.
So we'll do a check if user got password.
If that's not equal to an empty string, then we need to reset the password.
And to do that, we can use exactly the same logic we did in the other situation where we're changing
a password after a user has forgotten their password and wants to reset it.
So we'll create a variable called new hash and potentially an error.
And those are going to be populated by calling the B crypto package B creped dot and we want to generate
from password.
And just like before, we'll put it in a slice of bytes.
And that consists of user dot password, and we'll use the same second parameter we did before, which
was 12, and we check for an error, and then if we get past that, then we simply update the password
for user.
So error is equal to the update password for user.
And that requires the user variable and it requires the new hash as a string.
So we just cast new hash to a string.
And again, we check for an error.
OK, so that should take care of the first situation where we're actually updating a user now we need
to handle adding a user.
So in that case, the ID is equal to zero.
So we do need to create a hash again.
So I'll just copy this code and paste it down here.
And that gives us new hash format this properly and now we call error is equal to AFG DB don't add user
and we hand it user and string.
You hash and we check for error.
So I'll copy my error checking code.
Paste that in here.
And at this point, we're pretty much done, so we just need to send back a response so we'll do what
we've done.
In most cases, our RESP is a struct and it has error, which is a bull.
And in JSON, we'll call that error and a message, which is a string.
And in JSON we'll call that message and then restart.
Error does equal default because we've got this far.
Everything is worked and we say after right, Jason and we write w dot status, OK, and rest.
And that should handle the situation on the back end where we're trying to add or update a user.
So in the next lecture we'll just go write the necessary JavaScript to call this route and fire off
this handler.