WEBVTT 00:01.630 --> 00:06.580 So let's get started working on our handler and improving it a bit, but before we do that, you probably 00:06.580 --> 00:11.140 noticed I had a typo here in the Makefile and I corrected that before I posted the source code of the 00:11.140 --> 00:11.760 last lecture. 00:12.460 --> 00:16.770 It shouldn't be in void here on line sixty three where I'm stopping the invoice. 00:16.780 --> 00:18.060 It should be invoice. 00:18.320 --> 00:19.490 OK, just a typo. 00:20.620 --> 00:26.730 Now back over here in our handlers and our roots, we're saying get this route and go to the handler. 00:26.740 --> 00:29.350 But ultimately, of course, we want this to be a post route. 00:29.560 --> 00:32.920 So let's change that to post and let's go over to that handler. 00:33.910 --> 00:38.170 And I want to do more here than just format, print, line. 00:38.350 --> 00:41.410 What I want to do is to receive JSON. 00:41.740 --> 00:43.270 So I'm going to receive adjacent post. 00:46.230 --> 00:49.590 And then after that, I want to generate a PDF 00:51.990 --> 01:01.170 invoice and then I want to create some mail, I want to send the mail with an attachment. 01:03.590 --> 01:09.350 Which, of course, is the PDF invoice, and then I want to send a response now. 01:10.620 --> 01:14.800 That, of course, begs the question, what are we going to get in our Jason payload? 01:15.000 --> 01:22.080 I could if I wanted to import an order, for example, and a customer and a transaction right from the 01:22.080 --> 01:23.010 model's package. 01:23.550 --> 01:27.180 But remember that this is supposed to be a loosely coupled micro service. 01:27.180 --> 01:29.850 So instead, I'm going to define a custom type. 01:30.030 --> 01:37.440 And since this is a small application, I'll put it right here type and I'll just call it order and 01:37.440 --> 01:41.130 it will be a struct and it will have many of the same things that we have in models. 01:41.670 --> 01:43.040 So let's go open up model start. 01:43.100 --> 01:50.970 Go save some typing right here and I'll get the order type which which is at the top. 01:53.180 --> 01:57.680 So order right here, I'll just copy this or the contents of this. 02:02.100 --> 02:07.380 And I'll modify it as necessary, so I'll paste this in here, I will have an ID, I'm not going to 02:07.380 --> 02:15.050 have a rigid ID, not going to have a transaction ID or a customized customer ID or a status ID. 02:15.060 --> 02:19.890 I will have quantity and amount and I'm probably going to want the name of the product, whatever it 02:19.890 --> 02:20.300 might be. 02:20.310 --> 02:23.280 So I'll just call it product, which would be a string. 02:23.430 --> 02:25.860 And in Jason I'll call that product. 02:28.000 --> 02:30.940 Created, I don't need updated, that is not important. 02:32.940 --> 02:38.520 And I will receive that from Jason called created that so I know when the order took place. 02:40.100 --> 02:43.120 Not going to have a widget or transaction or customer. 02:47.180 --> 02:48.320 But in short time. 02:51.770 --> 02:53.510 Format this and what else are we going to need? 02:54.440 --> 02:59.600 Well, obviously the customer's first name, last name and email address, and that should be enough 02:59.600 --> 03:00.710 information for us. 03:00.980 --> 03:14.960 First name, string, chasten first name and duplicate and call this one last name and last name and 03:14.960 --> 03:15.320 email. 03:19.350 --> 03:19.870 And e-mail. 03:20.850 --> 03:23.200 OK, so let's format this now. 03:23.220 --> 03:28.230 That should be enough information for us to generate a very simple invoice and then we can create an 03:28.230 --> 03:29.760 email and send that off. 03:30.510 --> 03:35.160 Now, of course, I actually need to receive the JSON and we've done that in the past with a helper 03:35.160 --> 03:35.500 file. 03:35.520 --> 03:43.760 So what I'm going to do is open up this helper's file from inside the API package, select all and copy, 03:43.770 --> 03:52.970 close it and I'll create a new helper file inside micro invoice file helpers Dongo. 03:54.360 --> 03:57.080 Paste that in there and get rid of these functions. 03:57.090 --> 03:58.650 I don't need password matches. 03:59.040 --> 04:06.510 I don't need invalid credentials to delete those, but I do want that request and I do want JSON and 04:06.510 --> 04:07.520 I do want Richardson. 04:07.540 --> 04:09.930 So let's save this to clean up the imports. 04:10.440 --> 04:11.010 There we go. 04:12.720 --> 04:16.470 And let's go back to invoice handlers and let's actually read this, Jason. 04:16.500 --> 04:23.550 So we're going to receive the Jayson's we'll create a variable verb order of type order, which I define 04:23.550 --> 04:25.740 right up there, just above this on line nine. 04:26.490 --> 04:27.210 And we'll read this. 04:27.210 --> 04:34.110 And adjacent error is a sign the value of at DOT region, and that requires W r and we're going to read 04:34.110 --> 04:37.290 it into order and we check for an error. 04:38.070 --> 04:45.540 If error is not equal to nil, then Abdulahad request and that requires W are in the error and will 04:45.540 --> 04:46.110 return. 04:48.060 --> 04:53.490 We'll skip generating an invoice and creating a mail and sending them out, but we will do send response 04:53.490 --> 04:54.980 right now, which is pretty straightforward. 04:54.990 --> 05:04.850 So we'll create over resp, which is a struct and it has two fields error, which is a bool and injuries 05:04.860 --> 05:06.000 and it's called error. 05:07.590 --> 05:13.860 And we'll also have the message message string and in JSON that's called message. 05:15.060 --> 05:26.340 We'll populate the fields for that variable rescored error equals false and respawn message equals format 05:26.550 --> 05:27.540 s print f. 05:27.750 --> 05:33.630 We'll give them a message this time invoice percent pdf and that's what we're going to name it. 05:33.660 --> 05:35.940 It'll be the order ID pdf. 05:37.520 --> 05:47.780 Created and sent to pass, and those will be coming from order to I.D. and ordered our email and we 05:47.780 --> 05:49.460 write that, Jason, so. 05:50.500 --> 05:56.680 So right, Jason, which requires the response, right, or some status and the one that seems appropriate 05:56.680 --> 06:03.870 here is a status created right there and we're sending RESP. 06:04.480 --> 06:10.920 OK, now, obviously, the core logic here isn't done yet, but this is a good start on our handler. 06:10.930 --> 06:15.400 And it's absolutely loosely coupled because we're not using anything. 06:15.400 --> 06:18.900 No models from the from the front end or the back end. 06:19.240 --> 06:23.260 We've recreated our helper file and that seems like code duplication. 06:23.840 --> 06:28.990 But remember, what we're doing in this micro service might change over time and it might be very different 06:28.990 --> 06:30.890 from what we're doing in the front end or the back end. 06:30.910 --> 06:33.360 So this is absolutely loosely coupled right now. 06:33.370 --> 06:37.060 It has no external dependencies from the rest of our code base. 06:37.510 --> 06:41.620 So in the next lecture, we'll get started on creating an actual invoice.