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 back to studio and work its way into that WhatsApp conversation.
0:09
We don't need to print anymore. That seems like it's working. One option would be real simple. "order_id": db_order.id That's fine. That would work.
0:20
But I really like to have the response message sort of summarize like, Here's the whole details of the order. Here's the cake we think you ordered.
0:28
Here is the user who ordered it. Here is the price we think you ordered that and so on.
0:33
Just like we're using this on the incoming boundary for our pydantic model. Let's create another one. I'll order response model like this down here,
0:46
and we're going to have that kind of information I just described would have an order response. Also, it's a class like this,
1:01
remember, derives from base model. That's how pydantic works. We specify a few things that we're going to pass the order ID, which is an integer.
1:11
Remember, that comes from the database order date, which is a date time. Hope you didn't want to import, that way there we go a datetime.datetime.
1:28
I'm going to have an email, which is a string. That's the person that ordered it Have a price,
1:32
which is a float. Let's go ahead and actually pass the whole cake model back that we got. So it's gonna be the cake model. Think we called it? No,
1:41
just cake. It's like that. Okay, so all we gotta do is set these values and return it over here
1:49
So import this locally. Let's give ourselves some room lets say. order_id=db_order.id.order_date
2:00
is going to be db_order.created_date, email is going to be customer.email. The price is going to be cake_orde.price.
2:15
We can also do db_order.price to make sure that's what we had in the database. And finally, the cake is going to be cake_order.cake
2:23
and down here, we're just going to return this. And you would think you could just return it directly,
2:28
but not in flask. What you need to do is get the dictionary from it There we go. We've created our order.
2:34
And now we've got those that information and passive act. Let's try it one more time in postman before we move on.
2:40
And let's say we don't want lemon for our frosting or actually will make it a small It's going to change the price is 17. Yeah, we'll go with that.
2:49
That's send this over. Look what we got for our response. How cool. Here, this whole object is our order Response,
2:57
which has its cake object, testified with its small update. Everything we expected. Here's the email.
3:04
Here's the order date. We can make that maybe a little bit better if we do iso format, but I think that's okay.
3:09
Order id is three. The price of 17.0 gold coins. Fantastic. So our API is exchanging data and this information here.
3:19
Those are the kinds of things that we can send back to studio and finally through
3:22
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.