FastAPI and React Full-stack Application: CRUD API functionality fastAPI Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube welcome back to my channel so in today's tutorial we are going to go ahead and create the the the crowd functionality for the product so in the last tutorial this is what you were able to cover so we're able to cover the i've just set my server so to start your server you have to go you have to type in uv cone space up and then colon up and then that's the space test hyphen hyphen reload okay so that will start your development server and if you started you can go to localhost at port 8000 first stocks to get this ui okay so this would have been my uh we have managed to create the last uh in the last tutorial so this this tutorial are going to create the same functionality but for product because this functionality only deals with the with the suppliers refresh if you could get the suppliers so we're going to do the same thing for the products right so be able to get the products to be able to update product you'll be able to add products and delete products and then stuff like that okay so that's what we're going to be covering in this tutorial so let's get started i have opened up my feature studio code and in here we're going to go ahead and actually start creating the the cloud functionality for the for the products okay so this is very similar to what you have just done so far so if you have understand the previous tutorial you can actually create this on your own but uh for practical sake i'm going to go through it together so let's go ahead and actually so we need to say app dot post first we're going to create the one the route for adding products we're going to call it products products and then i'm going to go forward slash i'm going to add supplies so this is going to be an uh a parameter right a query parameter so it's going to be called um supplier underscore id so the reason why we need supplier id is that we want to create a product right you want to attach that product to a certain supplier like the person who supplies that product so that's why we need that okay and then this is going to be a uh and a sink a singing await function so i'm going to do a sync and i'm going to give uh we're going to call it um add underscore product so it is going to be used to add products we're going to accept the sub player there's a player underscore id uh parameter here and then it's going to be of type integer so we need to say of type of type in so if you don't know what type hints are in python i have a whole tutor on typing so you can check that throughout in my channel if i remember i'll try to leave a card up here on type hints okay so now that we have that new uh we also need the products id right so i mean the products data so i need to say uh products underscore uh details because we need to if we need to add a product we need the details of that product now this is going to be uh of type identity okay my bad this is this is nothing okay uh we need to we need uh we need to have the product details and the product details is going to be of type by identity now it's going to be uh let me just scroll down here we have created this one for pedentic uh sub products right so we're going to use a product in in order to accept the products that you're going to take in so you're going to exclude the read-only fields and the roof only fills include the id okay so we're going to accept every other field let's set the id so we need to win the the user needs to protect the products the user needs to provide rather the products name the quantity in stock the quantity sold the unit price and then the revenue and then the supplied by id is what you're going to be passing in here right so let's go ahead and actually import those things in so you need to say uh [Music] our products magnetic by dentist in products by dentist in uh we also need a product by identique and then finally we need we need the we need the products itself okay the product uh product model or table itself okay so i'm going to go ahead and actually save that so now that we have that we need to come down here and then you're going to say pedentic sorry product by identique in so that's what we need so now we're going to accept uh and just pass this for now you're going to you're going to accept the the you're going to going to have a call for slash product and then the id of the one will supply the product and you're going to accept that idea and it's going to be of type integer again if you're not typing so i have a whole tutorial on my youtube channel you can check that out on type hints so i'm going to accept the product details and the product details is going to be of type identity in okay okay now if you have simply passed it but now we i want actually to add this to the database so what we need to do first of all is to first get the supplier so i'm going to go sub player i'm going to type in supplier it should just be under we're going to await this i wait and then this is going to be a supplier we're going to say supplier dot get and you're going to say id is equals to supplier underscore id sorry so player underscore id so this is going to be the id of the supplier okay that you're going to pass in here so this apply id is the one that we uh we define up here so this is what we've passed in here we're going to use that id to get the supplier from the suppliers table so now that you have the suppliers object let's go and actually create a product we're going to a product is going to be uh we're going to say a product i'm going to call it products uh sorry uh you call it product details i'm going to say product underscore detail this one dot dictionary you're going to convert into a dictionary and then you're going to say x exclude and set okay it goes to true exclude uh exclude underscore unset uh this should be unset this should be uh of true okay so they can exclude the unset values so now that you have that you're also going to say uh product and then this now additional so you can be able to access the the revenues the revenue of the of the product which is going to be passed in because each product is going to have a revenue right so the revenue right here so the revenue is going to be because the product quantity times the quant the unit price right is this uh quite easy mathematics because if you have a product that is sold at a unit price of ten dollars or let's say 10 and then you have 20 of these products in stock to find the the revenue from that product maybe you have sold 20. we just take 10 times 20 right because the unit price is 10 and you have sold 20 of them right so this is that's what i'm trying to do here so i'm going to say product so this is actually going to be product detail okay so product detail just like that and then this is going to be product detail and then this is going to be uh let's get this is going to be a quantity in quantity sold times the unit price so this is going to be quantity and underscore sold and then you're going to multiply this by uh product underscore detail product details okay product details you're going to get the the units the units uh the unit uh price okay so we're going to take the product details uh this is the details so i'm going to take the product details i'm going to get the quantity sold multiplied by the product unit price that should get us that answer of the total revenue that you obtained from selling those products okay and then you're going to create a product uh a product object selenico product and that's called obj this is going to be the product object and this is going to be an await a wait so then go products dot create you're going to create this and this is going to be you're just going to spread this so you're going to use the unpack operator and then you're going to do products product detail just like that and then uh what does we need to do we need is going to supply provided supplied by okay so supplied supplied supplier i supplied by supplied underscore by which is the foreign key from our table right here right so supplied by the foreign key and it's going to relate to the foreign to the suppliers table so we need to provide any supply objects and then our table will take care of creating that fire so it's going to be the supplier which you just got up here okay uh this gives me errors let me just copy this and paste it there yeah so i'm going to say this is a weight okay so we're going to go products it's actually productive products so we're going to get a supplier and then you're going to get the we're going to cover the product the object has to be passed the json into a dictionary and you're going to exclude the unset and then we're going to calculate the revenue which is the product quantities quantity sold times the unit price we got the revenue from that product then you have created an object which is going to be whatever you're stored into the database we're going to get an object of it using this object you're going to create a response and simply return that response right like that we have done so far so the response is going to be just called response response uh is going to be a response is going to be await again we're going to await on this opening so it's the product spidentic in repressory product by identique.from it is from underscore tortoise orm so this is the tortoise orm and then in you're going to pass in the object that you've just created so product underscore obj just like that and then you're going to simply return the responses i'm going to copy this copy this and then move it here so that's all we're going to do for creating our product so let's check if our server is running uh yeah our server is running so if i refresh this now we should see this right here so product so we can try this out we can add in here an id of the product and then we can post that put that product new product on our id but before i do that let's go and actually finish the whole functionality of the grant for the products okay so now that you have the you have the post let's go ahead and do kit so let's go up dedicate and then forget what we're going to implement the getting of all products so it's going to be for slash products so first last product this is going to be also nothing in a way so this way i think and then it should be dev get uh get um let me just keep all underscore products and then it's just going to be uh you're not going to accept any uh parameters arguments here what you're simply going to do is going to return a response we're going to create a response object a response variable and it's going to be just an away so we're going to await and then we're going to get everything from the database so what you're simply going to do you're going to say uh product uh pedentic pedentic and then you're going to say dot from underscore query set and then you're going to pass in products dot org so this is going to get all the products from our database and then you're going to simply make it a query object uh from this and then we're going to get that away you're going to get the json i i'm going to store it in this await and i'm simply going to i'm we're going to store it in the response there but it's going to return that response variable that's all we need to do for the gate quite easily just two lines of code to get that done so just going to say product manage from query set and then products that all so now we have for getting all products let's make one forget for getting individual products so up dot get and then this would be um this would be product first slash and then you're going to do here an id so we're going to have an id so you can get the product with the specific id again this is going to be a sync so def get let's say a specific product let's call it specific underscore product okay so specific products you're going to going to accept the id so we're going to accept the id and i id is of type integer so again if you don't know what type is i have a tutor on my youtube channel about typing so you can check that out so just simply going to do i'm going to create a response variable so we're going to create a response and then the response is going to be uh going to await this process and then this is going to be um a product by dentik and then dot from uh this is from query query set uh this is going to be a single because you're going to get only one that's gonna have to use a single and make sure to use the signal if you use just the the from query set you get an error so just make sure you use a pedentic uh product underscore identity. from query set a query from query from underscore query underscore single okay so to make that work so you get then id is going to be id you have got provided in there you're simply going to return that response i'm going to i've just pasted what i have copied from up here i've visited down here so i'm going to return a response of status okay and then the data is going to be that response right there so that's all we need to do to get a specific product so now let's write the functionality for updating our product so just say app dot uh put right you need to use that put uh the put uh method not to update our product so to update our product we also need to have the product id in order to update a product right we need to know which product you want to update again this is going to be a sync so i'm going to call it update underscore product so you're going to accept the id it's going to be of type int and then now we also have the id you also need to have the new information that you're going to be used using to update our table right so to get that it's very very simple so what we need to do is that we need to call uh uh we need we need to get the product with the id so we actually we need that id in the under in the query as a query parameter and then we also need to uh we also need the information of the new product so let me just call it new uh let me just call it update underscore info this is going to be of type identity in so product uh products by identique in just like that so we're going to use this information to update the the current the product that you're going to get so first of all what you need to do is in the first get the old the all the product that you want to update so we're going to await this uh way to away this process i'm going to say products dot get and then you're going to get uh id the id is going to be the id that you have just passed in and then now that we have that product we need to now do a new we need to convert the the update info update underscore info i'm going to convert that into uh update info dot dict i'm going to go into a dictionary and i'm going to exclude i'm going to exclude unset and i'm going to set that to be true to to exclude the answer so now that you have that let's go ahead and now that we have that now we need to update it because information the product information so the product name might change so we're going to do it there at the obvious that you have got it from here we're going to go go ahead and actually say products this is not dictionary so we're going to say name and the name these fields i'm getting them from our model fuse right so a product has a name a quantity in stock a quantity sold the unit price revenue and then the supply buy okay so uh we're not going to actually update the supplied by uh we can you're just going to update the product uh details if you want to update the supply by again you can go ahead and actually implement that on your own path for this tool let's keep it simple and let's not update the supplied by info so i'm just going to say update info and i'm going to get the name okay so i'm going to set the old product name to the update infos name okay so this is the old product you're going to set its name to the update info the name from the dictionary okay so now we're going to repeat that for all the other fields so we can do product uh underscore quantities quantity sold it should just be update underscore info and then this is going to be quantity underscore uh in underscore stock okay this actually contains stock okay you can just type quantity in stock okay now that i have that i'm going to do a product uh this is going to be uh the revenue so it's going to be revenue and it's going to be update uh it's going to be update info and then you're going to get the revenue but for this the revenue might increase right so we need to increment this so it is going to say uh plus equals the update the update uh the quantity sold times the new uh times the unit price okay so again say quantity underscore sold uh times the updated information so update info there you need uh underscore price okay so yeah so the reason we're going to do that is that we're going to add there might be some revenue in the that that we already obtained from that product right so it's going to increment it but the quantity that has been sold the new the new quantity that has been sold times the new uh uh unit price even if it stays the same that there'll be no problem you're just going to update the revenue sold i hope it might make sense to you guys if it doesn't you can leave a comment in the comment section below i'll try to explain this logic too but it looks quite fairly simple it's just basic right basic mathematics is nothing complex okay and also going to do the same thing for quantity so we're going to do product dot quantity quantities quantity sold right so we're going to do an increment on this is going to be update info and then we're going to get the quantity [Applause] quantity underscore sold okay so we're going to create the quantity sold and then for the unit price you can also update the unit price of the products we're going to do a product uh product dot unit underscore price this will just be uh which is any price basically uh updated we're going to increment this so there's a unit a unit underscore price okay so uh now that we have there so let's see why getting the error product is not defined okay so i have a typo there so actually called product so uh yeah let's check we have updated the name the quantity sold the revenue the the the quantity sold the unit price here i think we have updated everything that we need to update now what we need to do is now we need to save the new uh the newly updated product info so you say products that uh this is going to be called we need to await on this process when you see a wait a weight product this could be a product let's save just like that you're going to save that right there so now that you have saved that let's go ahead and actually create a response and return the newly updated info we're going to await this process again it's just going to say uh product identity and then dot from uh tortoise over ram so from totto is orm what you're simply going to do is we're going to pass in the product just like that and then you're going to return the response right there so uh yeah that's all you need to do so after saving the up updating the product info you're going to get a save the product you're going to await this process we're going to get a response and also we need to avoid this process okay so we're going to get the response uh you're going to get the response which is going to be the product we're going to convert into adjacent we need to serialize it and then you need to wait upon this process after that you need to restart a response of the newly updated product info to the user that's all you're going to do so now let's go ahead and actually create the the uh the last route for the therefore the cloud functionality the for the product it's going to called a delete so again to create a delete so this is going to be product and for the to delete the product we need which folder we need to know which product to delete so you need to know the id of the product and then you're going to also call the electrical async uh this is going to be nsync and then it's going to be called the uh delete underscore products okay you're going to pass in the id and the id is going to be of type int and then this your type hints in python if you're not familiar with the new type hints i have a whole tutorial on typing so you can check that out so we need to avoid this process and you close the simply going to do is product dot filter and then you're going to filter id is going to be the id that we got after getting that product we simply need to say dot uh delete it and then we're going to delete it and you're going to return as a response yes i'm going to paste that in there and then this is going to be only a response of our status of okay to tell the user that you've we've already deleted the info the reason why we're not sending the response back is that there is nothing to send since already deleted it already so just going to tell the user that okay we deleted it we don't need to send it back what if we deleted it's not in our database anymore we're not concerned with it so it's just simply going to send you a status of okay which means everything is fine that you have deleted it right it makes quite uh it makes sense i hope it makes sense to you guys so for deleting we usually do not send back the response okay but we don't we don't send back what you have deleted you might say that okay i can get the the info before deleting it yeah that's fine but there's no point in returning what you've already deleted so i hope that makes sense so let's just go back to our front end here and i'm going to refresh this so i've already said might have my development server running so you can see it's running here on localhost at about eight thousand so you can see for products we can add product we can uh uh all we can get all products we can uh get a specific product we can update product and then we can delete a product so let's go ahead and actually try to add a product so i'm going to sell first of all i need to know which supplier that i want to add this to so i'm going to get a supplier and i'm going to try this get all the supplies in my database so i have a supplier with an id of two and his name is cendo okay gender okay so i'm going to come in i'm going to try this and i'm going to put in an idf2 which is for jindo i'm going to call this milk let's call it milk maybe i have 20 in stock the quantity sold i sold 10 the unit price let's say the unit price is 200 and then the revenue for now is zero so the reasons that i'm expecting i'm expecting the revenue to become uh the unit profit is 200 times 10 which i sold 10 of them so i expect the revenue to be 2 000. okay whatever currency that it is that you're using so i'm going to run that you can see we got back an internal server error so let's check where the error is coming from so product in uh is not subscriptable so okay so we have an error in there so uh let's check uh why we're getting that so let's check uh the uh appears so getting product uh details and then it's not subscriptable so it's up here yeah so i've converted this into a dictionary so uh this is of type product uh supplier in and then you have passed in the we have converted into a dictionary by excluding the unset it is true and then let's see where the error is so we have just got the name it is this one the updated info uh that should work so i don't know why it is not working so let's try to figure it out so the product habitat info is equal to i have converted into a dictionary and then why is it telling me dictionary is not subscriptable i really can get it so uh supplier uh sorry this is for suppliers rather sorry let's go for the one for products so for products i've converted okay this is actually products i've made an error here so you can see on the other places i have used i have used a product instead of products detail okay so i'm going to copy that and make sure i replace that here so that should solve our error so uh let's go back to and then try to run this again so i'm going to make the post post that again you can see we have that posted okay so that's my error so uh i i call this product instead of product underscore details so that was the error so make sure that you change online 50 to product details it goes to product details and then convert that into a dictionary okay so you can see now we have our products added and the revenue has been calculated accordingly which is just the quantity sold which is 10 times the unit price which is 200 and the revenue will be 2000 as expected so now we have up we have added our product to a database losses try to add in another one let's call this um let's say bread uh okay let's say the bread is sold uh quantity in stock let's see we have 40 and then the quantity sold let's say we have sold 20 the unit price is 300 let's just call it 300 and then we can just make a revenue say the revenue i have sold one bread already and one bread is already the unit price is 300 so sorry this is three hundred so the even the revenue which i'm expecting will be uh quantity sold quantity sold sorry quantity sold should be 20 so the quantity sold is 20 and then the 20 times uh 300 which would be 6 000 plus 300 so it would be 6 thousand and 300 right yeah which is the revenue here that's why uh that's why i have the the addition uh for the revenue i did a plus or equals in order to add in case that we have already sold something before making that request to the server okay so you can see now we have all those two products in there and everything is working as expected in adding the product so now let's go ahead and actually get all products so we can try this out make a request and industry then to us exactly two products and these are the two products in here so now let's go ahead and actually get a specific product so let's get the product with an id of two make that request and then you're going to get a product the idea of tweet is bread okay so i don't know i just said bread maybe because in the morning okay so now let's uh try to update a given product so let's try to update product two uh let's name it like we call it milk and then let's change the let's see uh so i'm going to create products so i'm going to make a request to you so i'm going to copy everything here copy that and then scroll down here and then i'm going to paste it here let me just paste it here here paste that in there and then fix the indentation okay and we have to fix this line below now you can see i've just fixed that indentation so i'm going to change the i'm going to change now we're making a request again and let's say that i have already i have not sold any i've not sold only maybe another 30 and i have sold a bread for another sorry two hundred two hundred dollars uh whatever currency you want to use and then the quantity installed placed here i tend this to 40 and then yeah let's leave everything as the same so i'm going to make the request again post that and then i go back an internal server error so let's check where that error is so i'm going to bring up my terminal to see where that error is so uh this one quantity in stock okay so okay i got the spelling of quantity wrong so let me go and actually uh sorry let me go and actually change that so this is actually uh updating the product to this actually quantity in stock okay so uh i think i've made an error somewhere quantity quantity in stock okay is this quantity installed copy and paste that there and then we can go back and try to make that request so make that post and then we can see we can put the information it has been updated accordingly okay so now everything is working fine and you can see the price has increased uh yeah so uh let's check the functionality uh here so the you the revenue okay the revenue will be the the okay so you can see that uh the revenue is going to add the revenue we're going to increment it by this amount okay so that makes sense okay so uh if i did any error in the match guys i'm sorry you can uh forgive me for that but i i think this error uh this this should do it because we're gonna take the old revenue plus the new quantity sold uh i think we should also add in the new revenue that has been passed in in case okay so let's try to do updates so this should be update update info and then you're going to add in the new revenue that has now been passed in so revenue yeah so that should be done so we're going to take the the need the new quantity let me just try to make this so to put in some precedences we're going to take the i'm going to first calculate the newly the new the new amount revenue which is we're going to get from products uh the quantity sold times the unit price plus any revenue in case any revenue has been passed down okay so that's all so let's go ahead and actually try to draw to test the update functionality again so let's try to update this again and then i'm going to provide in the same values run this then we can go back you can see now the 200 has been now added which was previously not added so yeah now it's working fine uh let's try to go ahead and actually uh update um we have done the delete let's try to update the product and i'm going to hit try and i'm going to pass in one so i'm going to delete the first product so we run this without bucket setters of okay which means that it's dedication if i try to get all products and then i execute this i should get back only one product product with an id of two since the product of id of one has been deleted so guys this is all the functionality this is all we need to have to implement the functionality for the cloud for the product so in the next we're going to write the functionality for the sending the email and then we'll do the uh the next comment we'll do the functionality for course and then go into front-end development and try to develop the react application that is going to consume this api okay so guys that's all i have for you today destroy if you like this show so if i give it a thumbs up and subscribe to my youtube channel if you are new here in case you are new here well uh my this channel is called coded prince and my name is prince this channel is all about uh development web development data science python and programming stuff in general so if you're interested in those kind of topics data science machine learning a web development python especially python please kindly consider subscribing to my youtube channel for more informative videos like this okay the framework that you have used for this draw is fast api and what you're trying to build is a full stack application using the back end using python first api and the front end which is going to be a react application this is going to be an inventory management system for a small shop or something like that it's just to practice uh to teach you to uh to introduce you to a full stack development in the last two drawers you have implemented the other functionality so if you haven't checked those tutorials up you can go back in to watch those tutorials guys thanks for watching uh this is all i wanted us to discuss in this video keep safe i'll see you in the next one
Info
Channel: Code With Prince
Views: 3,236
Rating: undefined out of 5
Keywords: python, fastapi, api, python tutorial, learn python, python programming, pydantic, tutorial, apprendre fastapi, learn fastapi, tortoise orm, tortoise, sqlite, react for beginners, FastAPI, REST API, Python, Framework, Programming, Building Rest API, database, async, encode/databases, fastapi restapi, fastapi asyncpg, asynchronous restapi in python, python restapi, python fastapi tutorial, fastapi cors, fastapi pydantic
Id: vknfcDAbDC0
Channel Id: undefined
Length: 31min 55sec (1915 seconds)
Published: Mon May 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.