Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the admin backend for Flask
Lecture: Concept: Fulfilling an order

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In our admin back end. We need to fulfill an order and there's two basic
0:04 steps. In order to do this,
0:06 we need to go and have some functional
0:08 talk to the database using the SQLAlchemy Unit of Work.
0:11 Find that order and fulfill it.
0:13 So here we write a simple query on the order say filter right on the id
0:18 give us the first one If that doesn't exist.
0:21 That means they passed an order_id to an order that doesn't exist.
0:25 Guess what? You can't fulfill that.
0:26 We just say return False. But if it's there,
0:29 we can start this fulfillment process.
0:31 Maybe it's already been fulfilled. We don't want to override that dates.
0:34 We'll just say, Yeah, sure,
0:35 it's fulfilled. Great. Still fulfilled.
0:37 And finally, if it's actually unfulfilled and we're switching at date,
0:42 we say the fulfilled date is currently now and then because we're leaving this context.
0:47 Block this unit of work with commit on success.
0:49 It was true. It's going to insert that change or update that change back to
0:53 the database and then we'll.
0:55 return True. This is the data layer side of fulfilling in order.
0:59 We also need a public interface for this to happen.
1:02 Remember, we built that cool admin back end had a table and had that
1:06 green button that said Fulfill when things were unfulfilled and what it would do is it
1:10 would call this flask end point here /admin/fulfill/7 or whatever the
1:17 number is, we're going to go over here.
1:19 We're actually going to check and see if the order exists.
1:22 Now, this is a bit of a duplication,
1:24 but I really wanted to make sure we could return 404 and be a
1:27 good Web citizen independent of whether or not we could fulfill the order.
1:31 So if it doesn't exist, we're going to say 404. But it's still a good
1:33 idea to have that validation in the service.
1:36 Then we're going to go to the service and say,
1:37 Fulfill the order and we actually also we're going to go to our WhatsApp service ultimately
1:42 and send the cake ready. We're not doing that yet in this chapter.
1:45 That's still to come, but that's what is happening overall when we fulfill the order
1:49 so the user will get a message.
1:50 Hey, your cake is ready.
1:51 And then we're going to just send them back to that admin page that will have
1:55 the effect of just refreshing that page and they'll see the change state from unfulfilled or
2:00 open to fulfilled. That's it really quite simple to go through that whole process, isn't it.