WEBVTT 00:01.530 --> 00:05.470 So the next step in this process is saving or adding a user. 00:05.700 --> 00:09.350 So we want to save an edited user or we want to insert the new user. 00:09.840 --> 00:13.580 So obviously we need a root and a handler on the backend to make that happen. 00:13.590 --> 00:19.170 So in Stasch API, let's create a route to a non-existent handler and then we'll make the handler. 00:20.250 --> 00:28.500 So it'll be a post route and it will go through API slash admin, slash all dash users, edit, slash 00:28.650 --> 00:29.030 Heidi. 00:29.340 --> 00:32.400 And the idea of course, will be different for every user. 00:33.000 --> 00:39.390 And of course you can use whatever path name you want there and we'll call after edit user, which doesn't 00:39.390 --> 00:39.950 exist yet. 00:39.990 --> 00:45.600 So let's make that so over an handler's dash API ago at the very bottom. 00:48.670 --> 00:55.260 We'll write one handler that handles both situations, so I call this a thunk with a receiver of pointer 00:55.270 --> 01:00.270 to application and we're going to call it edit user and it's a handler. 01:00.280 --> 01:06.460 So w htp dot response writer and R is a pointer to an htp dot request. 01:08.230 --> 01:16.900 Now we need the ID, so we'll get that from the Euro Chheda, Euro Perram and we hand it out, which 01:16.900 --> 01:22.210 is our request and we're looking for ID and we'll convert that to an end user ID. 01:22.450 --> 01:24.610 And again I'm ignoring the error and you should not. 01:25.270 --> 01:28.810 And that sterckx on a two hour and we're looking for it. 01:30.730 --> 01:37.720 Now let's create a variable for our user of type models, dot user, and want to assume that we're getting 01:37.720 --> 01:40.330 all of the information we need pass to us is Jaiswal. 01:40.930 --> 01:49.960 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 01:49.960 --> 01:50.360 user. 01:50.410 --> 01:55.120 That's what we're reading into and we check for an error and I'll just copy this, which is sufficient 01:55.120 --> 01:56.170 for my purposes 01:58.810 --> 01:59.650 and paste it in there. 02:00.880 --> 02:04.990 So now we have our JSON all parsed into the variable users. 02:05.080 --> 02:11.680 So we'll make a decision here if user ID, which is the value we got on line seven. 02:11.680 --> 02:17.410 Twenty eight in my code, if that's greater than zero, then we're editing an existing user or else 02:17.440 --> 02:19.410 we're going to insert a new user. 02:20.470 --> 02:26.290 So all we have to do here for the first part is to say we're editing an existing user. 02:26.290 --> 02:37.870 So I can just say error is equal to after DB dot edit user and handed the user because that's all that 02:37.870 --> 02:38.470 that means. 02:38.590 --> 02:40.780 And we'll check for an error, so I'll paste that in there. 02:42.220 --> 02:48.310 The other thing we need to take into account is if someone is filled out the password and verify password 02:48.310 --> 02:53.140 fields and we're not just changing first name, last name and email, we're also resetting the password, 02:53.230 --> 02:55.090 but we already have logic in place to do that. 02:55.600 --> 02:58.300 So we'll do a check if user got password. 02:58.570 --> 03:02.170 If that's not equal to an empty string, then we need to reset the password. 03:03.400 --> 03:08.440 And to do that, we can use exactly the same logic we did in the other situation where we're changing 03:08.440 --> 03:11.910 a password after a user has forgotten their password and wants to reset it. 03:11.920 --> 03:16.180 So we'll create a variable called new hash and potentially an error. 03:16.390 --> 03:24.850 And those are going to be populated by calling the B crypto package B creped dot and we want to generate 03:24.850 --> 03:25.510 from password. 03:26.020 --> 03:28.630 And just like before, we'll put it in a slice of bytes. 03:32.490 --> 03:38.700 And that consists of user dot password, and we'll use the same second parameter we did before, which 03:38.700 --> 03:47.130 was 12, and we check for an error, and then if we get past that, then we simply update the password 03:47.130 --> 03:47.730 for user. 03:47.760 --> 03:52.690 So error is equal to the update password for user. 03:54.000 --> 03:59.650 And that requires the user variable and it requires the new hash as a string. 03:59.670 --> 04:02.980 So we just cast new hash to a string. 04:03.720 --> 04:04.900 And again, we check for an error. 04:05.830 --> 04:12.870 OK, so that should take care of the first situation where we're actually updating a user now we need 04:12.870 --> 04:14.080 to handle adding a user. 04:14.280 --> 04:16.490 So in that case, the ID is equal to zero. 04:17.370 --> 04:19.580 So we do need to create a hash again. 04:19.590 --> 04:23.610 So I'll just copy this code and paste it down here. 04:23.910 --> 04:35.400 And that gives us new hash format this properly and now we call error is equal to AFG DB don't add user 04:35.670 --> 04:38.190 and we hand it user and string. 04:38.610 --> 04:41.330 You hash and we check for error. 04:42.030 --> 04:43.920 So I'll copy my error checking code. 04:46.530 --> 04:47.430 Paste that in here. 04:50.060 --> 04:55.070 And at this point, we're pretty much done, so we just need to send back a response so we'll do what 04:55.070 --> 04:55.400 we've done. 04:55.400 --> 05:01.820 In most cases, our RESP is a struct and it has error, which is a bull. 05:02.480 --> 05:09.590 And in JSON, we'll call that error and a message, which is a string. 05:10.250 --> 05:15.930 And in JSON we'll call that message and then restart. 05:15.980 --> 05:19.940 Error does equal default because we've got this far. 05:19.940 --> 05:28.940 Everything is worked and we say after right, Jason and we write w dot status, OK, and rest. 05:30.860 --> 05:36.610 And that should handle the situation on the back end where we're trying to add or update a user. 05:36.740 --> 05:41.720 So in the next lecture we'll just go write the necessary JavaScript to call this route and fire off 05:41.720 --> 05:42.320 this handler.