So let's get started working on our handler and improving it a bit, but before we do that, you probably
noticed I had a typo here in the Makefile and I corrected that before I posted the source code of the
last lecture.
It shouldn't be in void here on line sixty three where I'm stopping the invoice.
It should be invoice.
OK, just a typo.
Now back over here in our handlers and our roots, we're saying get this route and go to the handler.
But ultimately, of course, we want this to be a post route.
So let's change that to post and let's go over to that handler.
And I want to do more here than just format, print, line.
What I want to do is to receive JSON.
So I'm going to receive adjacent post.
And then after that, I want to generate a PDF
invoice and then I want to create some mail, I want to send the mail with an attachment.
Which, of course, is the PDF invoice, and then I want to send a response now.
That, of course, begs the question, what are we going to get in our Jason payload?
I could if I wanted to import an order, for example, and a customer and a transaction right from the
model's package.
But remember that this is supposed to be a loosely coupled micro service.
So instead, I'm going to define a custom type.
And since this is a small application, I'll put it right here type and I'll just call it order and
it will be a struct and it will have many of the same things that we have in models.
So let's go open up model start.
Go save some typing right here and I'll get the order type which which is at the top.
So order right here, I'll just copy this or the contents of this.
And I'll modify it as necessary, so I'll paste this in here, I will have an ID, I'm not going to
have a rigid ID, not going to have a transaction ID or a customized customer ID or a status ID.
I will have quantity and amount and I'm probably going to want the name of the product, whatever it
might be.
So I'll just call it product, which would be a string.
And in Jason I'll call that product.
Created, I don't need updated, that is not important.
And I will receive that from Jason called created that so I know when the order took place.
Not going to have a widget or transaction or customer.
But in short time.
Format this and what else are we going to need?
Well, obviously the customer's first name, last name and email address, and that should be enough
information for us.
First name, string, chasten first name and duplicate and call this one last name and last name and
email.
And e-mail.
OK, so let's format this now.
That should be enough information for us to generate a very simple invoice and then we can create an
email and send that off.
Now, of course, I actually need to receive the JSON and we've done that in the past with a helper
file.
So what I'm going to do is open up this helper's file from inside the API package, select all and copy,
close it and I'll create a new helper file inside micro invoice file helpers Dongo.
Paste that in there and get rid of these functions.
I don't need password matches.
I don't need invalid credentials to delete those, but I do want that request and I do want JSON and
I do want Richardson.
So let's save this to clean up the imports.
There we go.
And let's go back to invoice handlers and let's actually read this, Jason.
So we're going to receive the Jayson's we'll create a variable verb order of type order, which I define
right up there, just above this on line nine.
And we'll read this.
And adjacent error is a sign the value of at DOT region, and that requires W r and we're going to read
it into order and we check for an error.
If error is not equal to nil, then Abdulahad request and that requires W are in the error and will
return.
We'll skip generating an invoice and creating a mail and sending them out, but we will do send response
right now, which is pretty straightforward.
So we'll create over resp, which is a struct and it has two fields error, which is a bool and injuries
and it's called error.
And we'll also have the message message string and in JSON that's called message.
We'll populate the fields for that variable rescored error equals false and respawn message equals format
s print f.
We'll give them a message this time invoice percent pdf and that's what we're going to name it.
It'll be the order ID pdf.
Created and sent to pass, and those will be coming from order to I.D. and ordered our email and we
write that, Jason, so.
So right, Jason, which requires the response, right, or some status and the one that seems appropriate
here is a status created right there and we're sending RESP.
OK, now, obviously, the core logic here isn't done yet, but this is a good start on our handler.
And it's absolutely loosely coupled because we're not using anything.
No models from the from the front end or the back end.
We've recreated our helper file and that seems like code duplication.
But remember, what we're doing in this micro service might change over time and it might be very different
from what we're doing in the front end or the back end.
So this is absolutely loosely coupled right now.
It has no external dependencies from the rest of our code base.
So in the next lecture, we'll get started on creating an actual invoice.