Build a Pizza delivery API with FastAPI #12 | Update an Order's Status (HTTP PUT REQUEST)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back in this video we're going to make uh a route that basically updates an order for so in the last video looks at one that can update and order folding in this video you're going to be implementing that that updates an order status so let's go ahead and implement that so the first thing i'm going to do is to go to ask him as dot pi and in this case i'm going to create a schema specific to updating the order setters only so with output with a put method we can be able to delete the entire object but in this case we're only updating we can actually update the only the the only field that you want to update in this case and that's the order status so let's go and do that so what i'm going to do is to come we need to create a class a bidet class and i'm going to call this order status model so this is going to inherit from base model and right after doing this i'll simply put in our other status so our order status is simply going to be an optional integer an optional string i mean this will be a string and it will have a default off uh in this case you're going to have a other set as having a default pending so what i'm going to do is also add the configuration so i'll say class config and then right here i'll specify that we're having rm mod uh equal to true right after doing that we're going to go ahead and also add uh our schema extra so ask him actually it's just simply going to give us an example of how we are going to uh do a request from the swagger ui so while these two specify that this is going to be a dictionary so it will have an example which will also be a dictionary so this dictionary is basically going to show us which field to pass it so in this case what you're going to pass in is uh so in this case what we shall say is going to be that our piece actually our order status so this is going to be our other status in this case so in this case we're going to pass in our order status and named that default since the default is pending yes basically going to say that we shall have pending and after we do this then we're going to go back to our other route so what i'm going to do the first thing i'm going to do is to import our order model status so i'll say i'm going to also import all that this is going to be order status so this will be all that status so right after importing order status model we're going to go ahead and make use of our other status window so to do that i'm going to go and create a route for updating us to that so i'll come right here and say add all the router so this is going to be that order router then in this case since we're sending a patch request so the patch request is going to be able to update only one field of an object so we shall say hold that or the router don't patch then you pass in the route in this case when you go back to our mini documentation right here we're having that as uh updating the order status and it's only done by the super user so in this case uh let's say uh they have received an order and then they have started delivering it so they can be able to change the set of something like uh in transit or to something like uh delivered that's that's in case an order has been delivered so i'm going to copy that out which is going to be this since this is a prefix so i'll copy it and then go back to our other route so i'm going to paste this right in here so i'm just going to simply call this the id of that order and then i'm going to create a function so this is going to be a sync dev and then this will be update uh so we shall call this update order status so right in here what i will do is to actually specify that i id is going to be an integer and after doing that we are also going to add our authorization dependency since we need to protect this route so what i'll do is to say authorize and authorize is going to be of type of jwt and it's going to also be a dependency so it's going to be equal to depends so right after doing this we're going to protect our route so i'll simply go and copy this code i'm going to copy this line this block of code and then paste it right in here so this is going to protect our routes uh so that we require adjusted to access it so let me fix this indentation right here and right after doing this uh the next thing is going to be to uh get that currently logged in user and since this request is carried out by a super user which i'll need to check if this currently logged in user is a super user so to do that i'm going to get the username so i'll say username is going to be for true so there is a name is kept under the subject of the jwt so in this case to get the username shall say authorize dot get in this case it's going to be dirty subject and right after doing this i'm going to go ahead and query for a user with a specific username so we shall say current user is going to be equal to so in this case we shall say uh session dot query and we're going to query for a user so we need to query for main filter for the user with that specific username so what i'll do is to say uh user dot username in this case again it says that that is a name and if there is a name is equal to our username in order to return the first object with that specific username so right after doing this then we're going to go ahead and check if the superuser is a super user so every super user has that dot is uh stuff uh attribute of the model equal to true so to do that you should say if current user dot is staff then we're simply going to get that specific uh we're going to get that order and then update it so to get that or double shell then query for that order by its id so i'll say order it's going to be go to session dot query and then we're going to query for order and then read filter by the id so i'll say dot filter and then what i'll do is to specify that we are going to get the id so in this case we're going to say order dot id and if the id is equal to the id that you provided right here we seem to say uh it's equal to id then we're going to get the first object with that specific id so right after doing this now what we're going to do is to update this order so we created our orders test mode right here and this other status module is going to help us to validate the data that we're going to pass within our our actually that we're going to pass to our api for many clients so while this will come right here and i'll also add the other status so i'm just going to simply come right here put this on a new line then what i'll actually do is to say now we're going to have an order and this is going to be of type order status mode also main persona or the status right like this so after doing this uh hoping that everything is fine i'm going to come right here and what i'll do is to update this order so i'll do is to say order dot so in this case we shall say oh that dot actually is going to be all the status actually what i'll do is to call this order to update since to make things uh not confused so about this say this is going to be order to update and this is going to actually be all that update all that status then we shall simply say that that is going to be equal to uh the order we've passed in right here so we shall say it's going to be equal to order but actually let me so this is going to actually be order dot or that status so right after doing this we shall just commit our session so bring cessation not commit and what we shall do is to just simply return our object so to do that we're going to just come and say return this enable encode that so we're going to return adjacent networking corner and then push our return order to update so i mean save this so right now i can see that i have an error my link is showing me an error and i think it's uh so we see raise http exception we didn't close this parenthesis so i'm going to close it and when i close it now the next thing we're going to do is to run a server and test this route so what i'll do is to save and then i'm going to pull up the terminal and i'm going to simply run so we insert for that command again so actually what i'll do is to just i try to search for that command so let me just type it normally so i mean say uv coin main since the main module is what is having our main app instance so i'll say app and then i'll specify the reload option so this is going to basically run a server i'm hoping that everything is fine our server is going to run so right now we see a special form does not support item assignment so what we're going to do is to check this so i'm going to go ahead and check our schemas so right here in our schemas you see that actually this is supposed to be a type it's not actually an equals we made a mistake because even here you see that we assigned the type and then we specified that it's equal to a default uh value so in a save and our server is going to restart hopefully that everything is fine our server has restarted so i'm going to uh pull up my instagram here and then we go ahead and we test this route we need to go to mainstream area right here okay now now our installment has opened up so i'm going to create a new request now this new request is going to be to update and order status so i'm going to create this request and it's going to be a patch request so i'm going to create this what i'll do is specify that output is going to be of type json now i'm going to add the route so i'll come back to our code right here and i'm going to go to the other routes so i'm going to do is to copy this url sorry for that so i mean for this url and head over to instrument and then i'll paste this in so i'll specify the id of an order so then i'll also add the prefix so this is going to be uh localhost 8000 slash or dash and then slash order then we're going to specify the id of that order so i'm going to basically get a specific order so let's say we are to get a specific order so the first thing i'll do is to acquire a token i'll go to that login route and then send a request so this is going to give me access to an access token so i'll get an access token and then what i'll do is to go to that get specific order out and then go to the header section so i'll put in the token and send so right now we see we've got an id of 10 so basically i'm going to try to update the status of this so you need to go to update and other status and i'll go to the body actually before i go to the body i have to pass in the authorization header then right here i'm going to pass in the value as bearer and then the token so i'll go to the body and right within the body i'm going to go to schemas and i'm going to basically get this example right here so i head over to install me once again and what i'll do is to basically paste in our example so i mean try to fix this so i'll do just this so rachel say let's say we having our our status like our audacity has been changed to in transit so if you look at our choices right here if we go back to our model so i'm going to open modules.pi we can have our other statuses just like you see we have in transit and delivered so let's say we want to change this uh order status to transit so you need to just copy this so this is basically that key and then you shall also have the value the specific value that will be in that database so when i go to uh insomnia let's say one changes to in transit so when i try to make this order right now we see detail not found i think that's because we haven't specified the id so i'll provide the id which is 10. so when i send right now we see 200 okay and we're seeing this empty object so let's try to see uh if we get that specific order with id of 10 in this case we see that our order has been updated so let's go ahead and try to fix these empty objects returned to us if you've observed every time we've created an update our request we've been able to we've been actually getting an empty response so let's go ahead and fix that so i'm going to fix for both updating the order status as well as for both updating and order so what i'm going to do is to create a response uh dictionary so i'll create a dictionary called response so this is going to be the response dictionary and within the this dictionary i'm going to specify the various fields on the schema of a certain order so what i'll do is go to schemas dot pi and then what i'll do is to basically copy like i'll copy an example such as this and then what i'll do for this case is uh actually i'm going to replace this dictionary with the schema that we've copied so right now we have uh this so i'll do is to just basically update this with the current values of that order to update so to do that i'll say this is going to be order to update dot quantity and then right here i'll also say order to update dot in this case it's going to be pizza size and then what i'll do is also say that our order status is going to be order to update dot in this case will be order status and right after doing this we are also going to put in the id of that specific order so what i'll do is to come and say this is going to be our id and then i'll say it's going to be our order to update dot the id so having done this i'm going to come and then return the response instead of our object so to do that i'll simply come right here and i'll return the response so after returning the response let's go ahead and test this so i'll come to our insomnia and right here i'm going to try to send the request again so when i send the request you now see that our object has been returned to us so i'll do the same thing for updating and order i'll come and provide the the same token so i'll come and copy this token so i'm actually going to acquire a new token so when i acquire a new fresh token and see i'm super going to go to that to update an order and do the same thing so i'll go to the same route for updating an order so i'll simply copy this response and then go and implement it here on order to update so right here i'm going to also do the same thing so instead of saying actually it's going to be ordered to update the same thing so after doing this i'm going to go ahead so hoping that everything is fine or that quantity id pizza says and all that status so now everything is fine so when i go back to insomnia right here i mean to go to headers and then change the token right here to the current token that you have so right here we're going to have a token so i'm going to go ahead and copy this okay once again so when i copy that token i'm going to go ahead to the update and order out so i pasted the token and when i send this we now see that we are now returning objects instead of returning an empty object so in this video i've been able to see how to update the status of and of of our orders we've also been able to see how to fix our empty objects returned to us when we update an object thank you for watching guys and if you're new to this channel please go ahead and leave and subscribe to this channel please hit the like button it helps me out a lot thank you for watching guys see you in the next one bye
Info
Channel: Ssali Jonathan
Views: 60
Rating: 5 out of 5
Keywords: fastapi, fastapi sqlalchemy, sqlalchemy, fastapi postgresql, fastapi tutorial, postgresql, sqlalchemy fastapi, fastapi pydantic sqlalchemy, fastapi python tutorial, fastapi sqlalchemy pydantic, fastapi postgres, postgres, fastapi restapi, fastapi mysql, python fastapi, fastapi postgresql pydantic, fastapi pydantic postgresql, sqlalchemy tutorial, creating restapi with fastapi, fastapi channel, http-put, put request fastapi
Id: CZ11koq_N1Q
Channel Id: undefined
Length: 19min 44sec (1184 seconds)
Published: Sun Sep 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.