Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Using our database with the SQLAlchemy ORM
Lecture: Returning order details
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The final thing we need to do is return some order details, so it can go
0:03
back to studio and work its way into that WhatsApp conversation.
0:08
We don't need to print anymore.
0:09
That seems like it's working. One option would be real simple.
0:13
"order_id": db_order.id
0:18
That's fine. That would work.
0:19
But I really like to have the response message sort of summarize like,
0:23
Here's the whole details of the order.
0:25
Here's the cake we think you ordered.
0:27
Here is the user who ordered it.
0:29
Here is the price we think you ordered that and so on.
0:32
Just like we're using this on the incoming boundary for our pydantic model.
0:36
Let's create another one. I'll order response model like this down here,
0:45
and we're going to have that kind of information I just described would have an order
0:58
response. Also, it's a class like this,
1:00
remember, derives from base model.
1:02
That's how pydantic works. We specify a few things that we're going to pass the
1:08
order ID, which is an integer.
1:10
Remember, that comes from the database order date,
1:13
which is a date time. Hope you didn't want to import,
1:23
uh, that way there we go a datetime.datetime.
1:27
I'm going to have an email,
1:28
which is a string. That's the person that ordered it Have a price,
1:31
which is a float. Let's go ahead and actually pass the whole cake model back
1:36
that we got. So it's gonna be the cake model.
1:38
Think we called it? No,
1:40
just cake. It's like that.
1:44
Okay, so all we gotta do is set these values and return it over here
1:48
So import this locally. Let's give ourselves some room lets say.
1:54
order_id=db_order.id.order_date
1:59
is going to be db_order.created_date, email is going to
2:07
be customer.email. The price is going to be cake_orde.price.
2:14
We can also do db_order.price to make sure that's what we had in
2:17
the database. And finally, the cake is going to be cake_order.cake
2:22
and down here, we're just going to return this.
2:25
And you would think you could just return it directly,
2:27
but not in flask. What you need to do is get the dictionary from it
2:30
There we go. We've created our order.
2:33
And now we've got those that information and passive act.
2:35
Let's try it one more time in postman before we move on.
2:39
And let's say we don't want lemon for our frosting or actually will make it a
2:43
small It's going to change the price is 17.
2:47
Yeah, we'll go with that.
2:48
That's send this over. Look what we got for our response.
2:52
How cool. Here, this whole object is our order Response,
2:56
which has its cake object, testified with its small update.
3:00
Everything we expected. Here's the email.
3:03
Here's the order date. We can make that maybe a little bit better if we
3:06
do iso format, but I think that's okay.
3:08
Order id is three. The price of 17.0 gold coins.
3:13
Fantastic. So our API is exchanging data and this information here.
3:18
Those are the kinds of things that we can send back to studio and finally through
3:21
WhatsApp to our customer to let them know that we've actually received their order saved in our database at our store and we are working on it.