WEBVTT 00:01.120 --> 00:05.920 So we're going to need some database functions we can call from the back end that will allow us to get 00:05.920 --> 00:11.070 all users to add a user, to get one user to delete the user, so on and so forth. 00:11.500 --> 00:13.900 So let's get started with those functions. 00:13.930 --> 00:16.540 So I'm looking at models that go and I'm at the very bottom. 00:16.930 --> 00:21.880 And the first thing I'll do is create a function with the receiver of MTV model, same as always. 00:22.240 --> 00:31.120 And I'll call this get all users and it takes no parameters and it returns a slice of pointers to user 00:31.330 --> 00:32.350 and potentially there. 00:33.700 --> 00:35.080 And we need our contact stuff. 00:35.080 --> 00:39.790 So I'll copy that from the previous function pasted in here. 00:40.780 --> 00:43.540 And I need somewhere to store my users. 00:43.540 --> 00:51.550 So I'll say var users is of type slice of pointer to user, which is what we return it and I'll write 00:51.550 --> 00:52.660 my query, which is dead. 00:52.660 --> 00:59.980 Simple query is assigned the value of in back ticks select and we're going to select idy. 01:00.460 --> 01:01.570 I'll put it on the next line. 01:01.620 --> 01:07.000 I'd last name, first name email. 01:08.020 --> 01:10.930 And we don't want the password because we're not going to do anything with that hash. 01:10.990 --> 01:22.660 So we'll go with created and updated it and that's being selected from users and we'll order by last 01:22.660 --> 01:25.320 name and then first name. 01:27.370 --> 01:28.540 So that's our query. 01:28.960 --> 01:31.810 Now let's execute that query. 01:31.810 --> 01:40.990 So Rose and Erin are assigned the value of and debug query context. 01:41.740 --> 01:46.540 And we had the context and our query and then we check for an error. 01:46.780 --> 01:53.920 If error is not equal to nil, will return nil because they don't have anything. 01:54.040 --> 01:59.110 And the error and then we'll defer rose close. 02:02.450 --> 02:07.750 OK, assuming we get past that, we can now loop through rows, four rows dot next. 02:10.440 --> 02:18.630 And I'll create a user, a variable to store an individual user in the value of type user and we'll 02:18.630 --> 02:27.750 check for an error while we scan into that variable rose dot scan and we're going to scan into ampersand 02:27.750 --> 02:30.990 Yuda ID lowercase you you idee. 02:32.010 --> 02:33.370 And I'll duplicate that a few times. 02:33.390 --> 02:37.230 So when for last name, first name, email created and updated. 02:39.150 --> 02:52.410 OK, so we're going to scan into it and then last name and then first name and then email and created 02:54.530 --> 02:55.320 and updated that. 02:57.720 --> 03:05.310 And assuming we don't get an error, if error is not equal to nil then we'll return nil. 03:05.310 --> 03:09.930 And the error, if we don't have an error then we can just append that to our slice. 03:10.380 --> 03:14.520 So users equals append users. 03:14.850 --> 03:22.110 And then a reference to you because we're putting a slice of pointers is our return value. 03:23.190 --> 03:27.570 And down here, we return users and nil. 03:28.190 --> 03:32.460 OK, so that's our first function that allows us to get all of the users we want. 03:33.870 --> 03:37.130 Now we also want one that will allow us to get an individual user. 03:37.140 --> 03:47.640 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 03:47.640 --> 03:53.190 is an end and will return a user and potentially an error. 03:54.780 --> 03:57.180 So again, I want my contact stuff, so let's get that. 03:57.180 --> 03:59.520 Actually we can copy this whole thing and just modify it. 03:59.520 --> 04:07.830 It's a lot faster so we'll copy that, paste it into here, we'll modify our user variable to be just 04:07.830 --> 04:08.440 a user. 04:09.030 --> 04:14.820 So we'll call this year and our query becomes almost the same. 04:14.970 --> 04:22.950 But instead of autobody, we say where vidi equals questionmark and put in our past parameter of ID. 04:24.510 --> 04:31.140 And we want this to be a query row context, not just returns one row 04:37.710 --> 04:42.510 and this goes away and this goes away. 04:45.270 --> 04:51.790 And I'll just delete all of this and modify it appropriately, so error is equal to rodent skin. 04:56.790 --> 04:58.320 We're getting all of that information 05:02.970 --> 05:12.960 and here we say if error is not equal to nil and will return you and the error, otherwise we return 05:13.500 --> 05:14.460 you and nil. 05:15.070 --> 05:19.480 OK, so now we have one to get all the users, one to get an individual user. 05:20.010 --> 05:21.590 We also need to edit users. 05:21.990 --> 05:34.230 So let's go with func and DB model and its user and that will take a parameter, let's say you dot user 05:34.860 --> 05:37.290 and it will return potentially an error. 05:39.030 --> 05:48.300 And once again we'll copy our contact stuff and paste it in down here, make a statement 05:51.210 --> 05:52.860 and that's a sign the value of. 05:57.720 --> 06:10.140 Update users set, I will say first name equals questionmark, last name equals questionmark, 06:12.900 --> 06:14.650 email equals questionmark. 06:15.750 --> 06:17.210 I'm not going to do the password here. 06:17.240 --> 06:20.190 This will just be to update the basic information for a user. 06:21.060 --> 06:22.080 And then we want updated. 06:25.860 --> 06:28.920 Get rid of that comma where 06:32.610 --> 06:33.970 Pindi equals questionmark. 06:34.560 --> 06:34.920 OK? 06:35.950 --> 06:43.420 So that's the basic statement, let's execute to ignore the first return value, because we don't care 06:43.420 --> 06:45.350 about it, but we do care about an error. 06:45.580 --> 06:55.540 Those are a sign the value of MDVIP don't exact context with the context, then our statement and then 06:55.540 --> 06:57.700 I'll put these on separate lines so it's more readable. 07:00.640 --> 07:06.400 We want you dot first name and then I'll do that a few times. 07:07.330 --> 07:13.540 You dot last name, you dot email and the next one is updated. 07:13.540 --> 07:15.340 And that's just time dot now 07:18.940 --> 07:21.340 and that's where you dot it. 07:22.030 --> 07:25.870 And a comma, if there's an error we'll return it. 07:26.800 --> 07:34.240 If error is not equal to nil, return the error, otherwise return nothing nil. 07:35.290 --> 07:36.400 So now we can edit one. 07:36.700 --> 07:37.960 We also want to insert one. 07:37.960 --> 07:45.070 So I'll copy this whole thing, I'll paste it into here and I'll make this add user. 07:46.390 --> 07:50.950 Now, this time we want the user and we want the password, which will get us a hash, which is a typed 07:50.950 --> 07:53.860 stream, and we will pass that to this function. 07:54.820 --> 08:05.620 And our statement becomes insert into users and we're going to insert first name, last name, email 08:06.730 --> 08:14.200 password created up and update it up and then on the next line. 08:17.110 --> 08:25.390 Values, and we're going to have one, two, three, four, one, two, three, one, two, three, 08:25.390 --> 08:29.860 four, five, six, one, two, three, four, five, six. 08:32.380 --> 08:38.170 And we're going to execute that and we're going to pass in the first name. 08:38.170 --> 08:43.750 You don't last name, you don't email, then you dot password. 08:45.240 --> 08:48.420 Which is actually hash, in our case, hash, 08:51.630 --> 08:59.010 and then find out now get rid of this one and duplicate that one and everything else should be correct. 08:59.220 --> 09:02.880 So now we can now we can get all the users. 09:03.960 --> 09:08.700 We can get one user, we can edit the user, we can insert a user. 09:08.940 --> 09:11.490 And we also need to be able to delete user. 09:11.490 --> 09:16.890 So func and the DB model delete user. 09:16.890 --> 09:21.390 And all we need for that is the idea of a type int and potentially an error is returned. 09:21.930 --> 09:29.250 So let's copy this and paste it in there and make our statement. 09:32.220 --> 09:41.100 Assign the value of delete from users where ID equals questionmark and now we'll execute it. 09:41.880 --> 09:47.520 So we'll say I don't care about the first return value, do care about the error or assign the value 09:47.520 --> 09:48.630 of IMDB. 09:48.630 --> 09:55.170 Not exact context with X and our statement and the idea. 09:56.070 --> 09:58.530 Can we check for an error, which is exactly the same as this. 09:58.530 --> 10:00.300 So I shall copy and paste. 10:02.070 --> 10:04.470 So those are the basic crud routines. 10:04.470 --> 10:08.550 Get all the users, add a user, edit user, delete a user, get one user. 10:08.580 --> 10:09.380 We're all set. 10:09.750 --> 10:11.250 So those are our database functions. 10:11.430 --> 10:14.550 So now we need to go write some logic in the back end. 10:14.550 --> 10:20.550 A handler in the backend that will take some kind of Jason payload or some kind of request, get all 10:20.550 --> 10:25.830 of the users and send them back as Jason and we'll be using the rest of these database functions before 10:25.830 --> 10:26.260 too long. 10:26.670 --> 10:27.300 Let's move on.