CRUD With Async SQLModel (An Introduction to Dependency Injection) - FastAPI Beyond CRUD (Part 6)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back so in this video we going to be carrying on from where we stopped in the last video went ahead and created our database table using SQL model so we looked at how we can be able to set everything up from creating our database to creating database models and then using the database models to reflect in the database however I noticed that there are mistakes I did when creating the database model and let me address those right now so the first one was to go ahead and specify that these are fields and they are PG timestamps but what I forgot to do is to specify that inside our column right here this column type is not an is not a ptic type so this is an SQL Alchemy type so I have to go ahead and do it just like I did for the U idid fi so to change that I'm going to go ahead and specify that this is an sa column and then I'll say that this is going to be an column instead of a ptic column because this will give us errors when retrieving this data then I'll do the same thing for this updated at column and this will go ahead and still do the same thing another thing that I did was to call uid uid for and then include these two brackets so normally we are just okay with just leaving out the brackets because the moment we call it with those brackets you shall get errors that are from generating the same uu ID every time we are creating a uu ID so I hope you've understood why those changes have happened and now let's go ahead and clear our database so I'll go ahead and open up my terminal and inside my terminal right here I'll get access to the psql shell and I'm going to Simply go ahead and connect to the datab which we called bookly DB and I'll go ahead and just drop the database table that we created which is actually drop table which is books so the moment weart our server our tables will be created well and we shall be good to go so if I go ahead and save our server is going to go ahead and restart so when it restarts we shall notice that our book table is going to be created again so we have now created our book table and let us go ahead and look at how we can go ahead and use our newly created database model to carry out CED in the same way that we did in the previous videos so what I'm going to do at first is to create a service class where we're going to create all our CR so think of a service class as the class is going to have all the logic for creating all this Crow so that the moment we need to to carry out that quad the only thing we shall have to do is to go ahead and access a specific method on that class now there are very many ways to do that and very many approaches to that but what we're going to do is to Simply go within our books folder within our source directory and we're going to create a new file that we're going to call service. pii inside this we are going to go ahead and create a class on which you're going to access all these methods and this file is going to be one that's going to allow us to write all our Logic for CR so that we don't mix it up with our Logic for the route handlers if you notice what we did in the previous videos was to do all actions for CED within the route Handler functions and this makes them have a lot of code so for us to be able to separate that code from our route handlers all we do is to Define it within its own file and then simply access it via a class so that's what we going to achieve in this video so I'm going to go ahead and create a new class let me make this a little bigger so this class is going to be called the service or the let's just call it the book service so this is going to be our book service class and within here is where we going to define the different methods for creating reading updating and deleting our book resources so what I'm going to do is simply come right here and say a sync de and this is going to be one for reading all books so you can simply give it a name set such as get all books and this is going to take in self because it's a method that's going to be accessed via a book service object another thing is going to be the session so this session object is going to be an important object that's going to help us to perform any kind of CR operation on our database now think of the session as the medium that SQL Alchemy and SQL model provide for us to access our database and then carry out any transaction you want to go ahead and perform the session object also allows us to execute any SQL that may be written so we're going to look into how we do that but for now keep in mind that we need the session object so once you've created this session object and provided it as a parameter to this get all books function we're now going to go ahead and use type hints to define the type of this specific parameter so I'm going to go at the top right here and import it from SQL model so I'll say from SQL modelex do as sync iio do session we shall import our async session class and once you've been able to import this async session class now let's go ahead and inject it right there and simply specify its type which is going to be a sync session the reason that's why I'm doing it is that this type hinting is going to allow us to easily access the methods while programming so it's just one hack that makes your coding easy so I'll just pass this for now I'm going to repeat the same action for the other methods I just simply go ahead and copy what we have here and I'll paste it on a new line I'll do the same thing so I'll just simply call this our get book or this is going to be one for retrieving a single book so shall call it get book but this will also need the idea of the book we want to get so I'm going to go ahead and provide it before the session so this will be our book so in this case we're using U IDs so shall have to get our book U ID so this can be a string that will be converted to U IDs when querying then another thing we may do is to Define one for creating a book so this one is going to be called create book and it will take in a session but it will also need the data to create a book so we're going to go ahead and provide the book data and this book data also needs to be of a specific type now for us to determine which type of book we are going to create we shall need a partic model to help us dictate which fields are to be used to create a book now for us to be able to do that we are going to need to go to our schemers py and create that specific book create class now this is our book model or our book class that we use to retrieve our book objects in the previous videos but if you want to create a book then we're going to need a way to define this without those fields that we don't need so for now we have this as our ID but remember we change this to our U ID so we have to update that so I'm going to come right here and say that this is going to be our uid and instead of being an integer it's also going to be a uu ID so I'll go at the top right here and say import uu ID and once that is done then you're going to Simply come right in here and say that we're going to be making use of U ID dot the U ID type so once that is done now let us go ahead and create the class that's going to be used to create our book so we can also add the fields that are for the created art dat as well as the updated ad date so we call this updated at I'll just come right here in the schemers and also import our dat time class so I'll just come and say from dat time we are going to go ahead and import our date time and once that is done now let's go ahead and add those fields so I'll just specify right here so this will be our created at so this will be a daytime object just like we specified on the model and also this will be the updated at which we actually called update at but feel free to go ahead and update it so once this once this is done let us go ahead and now Define how we're going to create our book so I'm going to create a class and we're going to call this our book create model it's also going to inherit from the base model class now what you need to do here is to Simply go ahead and Define the fields that we are going to need to create a book now that is going to be very similar to what we have here only that we are going to also need the published date so I'm going to go ahead and simply copy what we have here and paste it right here so one thing we're going to also add is going to be our published dat so I'll copy it and then I'll paste it right here after the publisher so I guess I haven't copied it go ahead and copy it paste it right here and that is going to be our published date so keep in mind that we are passing this as a string but we're going to look at how we're going to convert this into a date object when storing our date so let's go ahead and do that so when go back to our models right here we def find our publish date as a string but we want to go ahead and save this as a date in our database so this is something we can also change so we can go ahead and specify that this is going to be a date on our database then we create the rec create the entire field in the database so let me go ahead and do that so when I run my server right now all these corrections to the database are going to be made so I'm not going to have the published data as a date but when I submit it within our schemers we're going to be accessing it as we're going to be getting it as a string and then we shall return it as a date and we're going to look at how we can do that so back to our service file inside our service file right here we're going to deter describe our book data as being of type book create model which we've created within our schemers here so I'm going to go ahead and import that class I'll say from this is going to be our DOT service or do schemers because this is in the same directory then we shall go ahead and import our book create model so once this book create model has been imported we're now going to go right in here and say our book data is going to be equal to our book create model or it's going to be of a type of book create model so once that is done let us now Define the two remaining methods for updating and deleting a book so I'm just simply going to go ahead and copy what we have here cuz they're going to be very similar and then I just simply paste what we have here and change this from get book to update book so when updating a book we're also going to need the data that's going to be for updating a book now let's call this the update data and this is also going to be of a certain type so let's just say that it's going to be of this type of our book update model so I also have to import that I'll say import our book update model so once we've imported our book update model then all we're going to do to say book update model and then we shall have our session so this will take in the U ID of the book as well as the data to update the book now let me format this so that it looks a little bit nicer and yeah that's how it looks for now all right so let's go ahead and finally delete the book or describe the method for deleting a book so that's going to be one similar to what we have so this will take in only the book uid and it will go ahead and delete the book all right we've now defined our methods that are specific to deleting or creating reading updating and deleting a book we're now going to go ahead and Define the logic that's associated with each of those operations now let us start by first of all using our session to create a statement that's responsible for reading all our books or getting out getting all our books from the database so I could just come right here and I'll create a statement and this statement is going to be a select statement so SQL model gives us methods or functions that help us to write our python in form of SQL for example we can be able to write a select statement and that select statement will be so equivalent to what we may have as a normal SQL statement and let me show you how that is done so we're going to be importing our select function from SQL model and once we've imported this select uh we're now going to create a select statement so this is going to be something like select and then we shall just provide the model that we want to select from so this is very equal to something like select stuff from a specific table in our database and we're going to look at how we can do that so we have to first import our model which is our Box model right here so I'm just simply going going ahead and importing it so I'll say from models we are going to go ahead and import our book model and once you've imported our book model now let's go ahead and select it so I just come right here and say select book and then we can now go ahead and do the filtering with wear or ordering by by just simply calling Dot and then the specific method so in this case what you want to do is to go ahead and order this by the date at which they were created so want the lat is being at the top of the list and the way you do that is by saying order by and once we've been able to do that we can now specify that specific column we want to order by so what I want to do is to Simply order this in a descending order so that the book object that we created or the book record that we created the latest is the want to appear first on the list so to do that we're going to also make use of another function called the descending function or the disk function and what that will do is to just simply be called and we can now go ahead and finally Define that specific column we want to go ahead and order in an descending order so we shall say book and then dot created at so in this case we want to go ahead and order this by the date at which we created them so once this is done we're now going to use our session object to go ahead and execute the statement so for us to do that we shall say that our result is going to be equal to using our session so most methods that are to be accessed on the session object are going to be core routines because this is an ASN session object so for us to do that shall say await and then this will be session dot so we need to call a specific method for executing a statement which is going to be exact and then in there is where we're going to provide our statement so just simply going to re write this to statement once that is done uh let's go ahead and access the result so for us to get the result from this result object all we have to do is to go ahead and call a specific method on this result object so we can simply go ahead and return our book by saying return result Dot all and this will give us access to all the books that are going to be returned from this result object so once that is done we can now go ahead and test it out so before we test it out I think it's something good that we first finish all the methods and then we look at how we can go ahead and test this out so let's first deal with retrieving a book so retrieving the book is going to be the same almost the same as what we have here but it's going to be retrieving one book of object so let us do that so I'll come right here and say that our statement is going to be equal to select and this select is going to be on our book model but what we need is to go ahead and access one book so in this case to filter we are going to be making use of thew method and using this we shall use our column that we want to look for so in this case it's going to be the book U ID and we shall check if the book U ID is going to be equivalent to the book U ID we provide within our method just like that and once that is done this is going to return a result object just like we've done when fetching Ro books so we shall say that our result is going to be equal to our wait and this case we shall say session do EXA and shall provide our statement so once that is done now we can go ahead and return our book object by saying return result do fast and that will go ahead and return our object we've now been able to get all books as well as getting a single book object now let's go ahead and handle the rest of the quired operations to create a book we shall need first of all access all the data that comes from the client so that is going to come via the book data and once you've been able to do that we're now going to create an object of type book or our book model and then simply use our session to add that to our database so to do that we're going to first of all access our book data via the book data dictionary so I'm just simply going to create a dictionary and this dictionary is going to be that's going to get our book data but in form of a to get this book data in form of a dictionary shall just have to say mod damp and once that is done then we're going to create a new book so a new book is going to be equal to a book object and this book object is going to be unpacked or the attributes are going to be unpacked from our book data dictionary what we are doing is to unpack all keys and values that we've got from our book data dictionary into our object so this is going to create a new book object with attributes it gets from this book data dictionary so this is what we have here so it seem like I'm having an error so book data dictionary is not defined so I guess I did double underscores in here and that's why it's returned that error so now to go ahead and save this book all we have to do is to say session so this is going to be using session do add and in this case we're going to be using our new book and then to finally save this to our database we are simply going to go ahead and await session do commit so that will go ahead and Commit This to our database and once this is done we need to also return the newly created book object so this is going to be a method that's going to add the object to the session and once we've added the book to the session we are now going to commit this to our database or to commit the current transaction so we return our new book by just simply saying new book and this is going to say return new book and what we've done is to create our new book so the way we've created our new book is simply get the data from our client create a new object with that data and then add it to a session and finally commit it to our database all right so let's go ahead and finish this app so to finish this app we're going to go to update so the process of updating is to first get the book and since we have a method for retrieving this book we're not going to repeat the entire process so we shall say that our book to update so in this case we're going to say book to update is going to be equal to self. getet book so in in this case we are going to get our book and this time we have to provide our book U ID so I'll just come right here and say we need to have our book Q ID and also we shall need our session so I'm going to say comma and then we shall also need our session just like we have right here so once that is done let's go ahead and now uh update this book so we going to access the data from our update data just like you see right here and then use that data to update our book so we are first going to convert that to a dictionary so I'll say book update data dictionary is going to be equal to our update data do model damp and once that is done we are now going to go ahead and make use of that data to update each of the fields on our book to update so what you shall do is to say for each of the keys and the values in the items that are inside our book update dictionary which we called the book update dictionary so in this case it's going to be update data dictionary we're now going to go ahead and access the items by calling the do items method since this is a dictionary and once we've done that now we're going to go ahead and update each of the keys or each of the attributes on this specific object using the special set attribute in built python function so what this does is to get an object and then update I've actually even written it badly it's going to be set attribute and this is going to get an object such as the book book to update object that we have right here and then it will update it basing on the keys and the values that we provide to it so in this case if you go ahead and say book to update and here we have to go ahead and provide the name of that specific attribute want to update which shall be the key in this case and then we shall also provide the value first to commit these changes to our database all we have to go ahead and say is await session do commit and just like that we shall have updated our book so once that is done let us go ahead and return our book updated so in this case you're just simply going to say return book uh to update now one important thing is we are not catching the error that may result out of the book not being found so what you can do is this case is to just come right here and write that check so we can say if book if book to update is not n which is not n then we can simply return all this logic and then in case we don't find the book that's very and else then we can simply return none in case we don't get any book then we can return none and in this case what you can do is to just come right here and say that our book is going to be equal to result fast but in case our book is not being found then we're going to return n so how we're going to do that we can say return book if book is not n else we can return our nine so in this case we shall return n in case the book is not found now this is going to be very helpful when we are dealing with our path handlers just in case we do not find a book we want to go ahead and throw that exception the HTTP exception to show that our book has not been found all right so let's go ahead and finish this up so going to go ahead and use the same logic right here so we simply going to come and say that our book so just even copy what we have right here and once I have this then we going to also do the same thing if book so in this case it's going to be book to delete is not none so I have to rename this from book to update to book to delete and then we shall go ahead and delete using our session so in this case we shall come and say that await session do delete and then we shall call our book to update so once we been able to do that then we are going to use our session to finally Commit This to our database this is actually book to delete not book to update so I'll just simply go ahead and change that and then I'll go ahead and Commit This so shall say await session do commit all right so once this is done then we need to throw an error or return none in case that book has not been found so shall just say else return none and just like that would being able to Define all our methods for carrying out our CED or our book resources now once this is done we're now going to go ahead and determine how we shall use our session within our path Handler function and that is where the concept of dependency injection comes in so dependency injection is a mechanism that F API uses to allow you to share logic across all route handlers that may need it for example in this case we are having this book service class having these methods and each of these methods rely on a session object but in case you want to go ahead and share that session object across all the other methods that need it then all we need to do is to go ahead and simply pass that session to all these path handlers now let's go ahead and and create that dependency so normally when you're using dependence injection you're going to have code that's going to be relied upon by other code so that code is what you call a dependency and that's what we're going to create so that dependency is going to be responsible for creating our session and then we shall use that dependency in the function that will depend on it so to do that we're going to head of our source folder and within our source folder we're going to access the DB module or the DB package right here and inside there is where we going to go ahead and access the file in which we going to carry out that specific code or write that code so we're going to now go ahead and create a function that's going to be responsible for returning our session that shall now use across all these rout handlers so let us go ahead and Define that so just simply going to come and say async def because this is going to be making use of some async features so I'll say I sync de and we shall call this get session so this get session is going to make a return and that is going to be of asking session so it makes sense for us to import ask sync session so I'll just simply go right at the top right here and say from SQL model we shall go ahead and access EXT do as syn iio do session and right inside there is where we're going to get our assn session once you've got to as sync session class now let's go ahead and determine or describe our as session as the return type of this function so okay now we have been able to Define this function let us go ahead and create the session class through which we're going to create our session object so this is what happens when you want to create a session you need a session class but for us to create the session class we're going to be making use of SQL Alchemy session maker and why we're going to use that is because we need to use this SQL model Asing session and the way we do that easily is through the use of the session maker class as it helps us Define any class that we want to use for creating a session now let's go ahead and import that I'll go at the top right here and say from SQL Alchemy I'm simply going to go ahead and import our session maker so I go ahead and import the session maker and once I've imported the station maker now let's go ahead and create a class and that's why I'm going to write it with a capital s or an uppercase s so this is going to be our session but this is going to be created from our session maker and the first thing we're going to have to do in creating a session or a session class is to bind it to one engine now remember that we created our engine now m d to the sync engine but you can feel free to go ahead and modify that right at this point in the video so this ASN engine object is what we're going to bind to this session so that we can access our database and then carry out the card so I'll just simply come right in here and say bind is going to be equal to a sync engine so once that is done now let's go ahead and also specify the class that we're going to be using so it's going to be equal to our as sync session the one that we imported and once we have been also able to do that let's go ahead and also add one attribute that's going to be the expire on Commit so expire on Commit is going to allow us to use our session object even after committing transactions to our database so in this case we are going to go ahead and specify that we don't need to expire our sessions so we just going to set it to F so that every session that we create can then be used even after committing the transaction all right so we are done creating our session class now let's go ahead and finally create our session object so we're just simply going to come right here and say a sync with and we're going to use this context manager to create our session so we now going to just simply call our session class or create an object of our session and we're going to just say with session as session then we shall just yield our session so this is going to return our session so that we can finally use it within our routes our path handlers and just like that we've been able to create our fast dependency now let's go ahead and inject this into the route Handler functions that are going to make use of it so going to head over to route. py within our books folder and we're going to begin by first of all importing it so I'm just going to go right here and say from so we can actually just say from do service or from Main from whatever and that will go ahead and basically access as long as it belongs to the same folder or the same directory just like you see right here so for now we want to access source. db. Main and in this we want to go ahead and access the get session f function so the get function is one that going to inject into these real functions in case want to carry out any CR and the way we do that is first of all accessing the depends function from first AP and when you access this depend function all we need to do is to go inside a r Handler such as the one for getting all books and then injecting in that specific objects but then we shall have to provide the value as that value that comes from that dependency just like we're going to see so let us begin with this function for getting Ro books so shall say that our session is going to be of a specific type which is going to be the one that we use to create this session so I'll just go ahead and copy what we have here and then introduce it within this file and once you've been able to do that then our session is going to be of type A sync session and the default value for that is going to be one that we get from using our dependency so we're going to call the depend function and then provide the dependency which is going to be our get session class this is actually a function so now going to say get session so once that is done now let us go ahead and use our session to retrieve or to get all books so let us first of all import our service so now this is a class and we need to create a session and then use each of the methods right here to carry out our CR so for us to do that I'm going to go ahead and import this book service so we're going to do that within our models actually our R so have to close this now I'm going to come and import that service class so I'll just come and say from source. books do service in this case we are going to go ahead and import our book service class so once we've imported the book service class now let's go ahead and change our code so that it makes use of our book service class instead of our book data so I'm going to go ahead and first remove the inputs for our books from our book data as well as our book data so that we get rid of it we are now using our database so we don't need it so once I've removed that that now let's go ahead and make use of our book service so this is going to be an object so I'll just have to come right here and say the our book service so this going to be our book service object which is going to be equal to our book service so once this is done now let's go ahead and fch all our books using our book service objects so I'm just going to come right here and say books is going to be equal to so in this case we shall say books is going to be to book service Dot and in this case we shall get all books but this is going to require us to have our session so we shall make use of the session object that we've got via our dependency injection so I'll just have to pass it in right there and once that is done uh we we will have got our books from the database and used them so that's how we're going to go ahead and finish up our C now let us begin doing this one by one so let's start with one for creating a book to create a book we are now going to go ahead and simply say this is going to be our new book data so let's just say this going to be our new book that's going to come from our book data so I'll just remove what we have right here so our book data is going to be available but we need to create our book so we shall say new book is going to be the one that we create from our book service this object and in this case we shall create this with our session so we shall actually need to just say book service. create book and in this case we shall need the book create data which is going to be our book data as well as our session which is going to be our session so shall go ahead and provide that and then finally we are going to inject our session right in here so I'm going to do the same thing I'll just come right here and say that we're going to have a session similar to what we have done right here and just like that we'll have injected our session into this route Handler so once the newly created book has been created it shall be returned right here now let's do the same thing so I'm going to format our code for this part I'm using a black code format extension for vs code and that's why I can easily format my code just like this so this is just enough to create our book and we can also Define how we're going to return our book so in this case we shall say that our book is going to be returned in a response model which is going to be equivalent to a newly created book so we are going to go ahead and say book so this is going to be equal to book and once that is done let us s ahead and retrieve a single book so to retrieve a book we shall just say uh in this case we are going to say book is going to be equal to book service dot get uncore book so I think that's what we called it so this is going to be book service I've made a mistake in the spelling but when you get our book service and we going to get our book and what you shall need is the IDE of the book as well as the specific session so I'm going to go ahead and provide the session right here and then we are going to provide our book U ID so I'll say our book ID is going to be our book U ID and I think I'm also going to change it right here from book ID to book U ID then we can also have our session so the thing is right now we need to handle what happens when this book is not return or when we return none from our service so I'll go ahead and pull that back so this can return none in case the book is not found and it will also return the book If the book is not found so for us to handle that what we're going to do is to say if book then in this case we can say uh return book else we shall just simply go ahead and return an exception so in this case we can say else sorry for this then we shall raise this exception for returning a F0 for not found and that is going to be one for retrieving a book so let's go ahead and finish up the first thing we're going to do is to head over to our update model and in this case we are simply going to delete what we have so shall have update and this is going to be our updated book so our updated book is going to be one that's going to be a result of calling book service in this case we're going to say update book and in this case we are going to need the book U ID so I'm going to change this from book ID to book U ID I'm going to Simply go ahead and change this and then book book U ID this is also going to be the book U ID so this will be the book update data which is going to be of TP book update model and then inside here what we're going to do is to provide that so I'll have to also get the session just have to copy this session object from any route Handler function and then simply go ahead and inject it inside sorry for this um we simply going to go ahead and and set this up by providing our session now let me format the rest of the codes so that you can easily see it and this is how it's going to be so we have our book U ID our update data as well as our session all right so let's go ahead and just provide them so we're going to have our book uid and in this case we shall have our book update data and then we shall also have our session so that will be enough for us to go ahead and update our book so another thing is these are cor routines so I've just forgotten to call them with our wait so I have to do that so I'm just simply going to come come right here and then I'll simply highlight all occurrences of book service dot the method and before calling them we going to go ahead and provide the await so I'll just come and call them with await because these are all methods defined as score routines and yeah that will be enough for us to update so remember that this is returning n also so when you go back to our service file we have update book uh checking if the book is not none and then returning n in case the book is none so so in that case we have to also handle that so we shall say if updated book in this case we shall go ahead and return our book so we shall return our update book and that's where we're actually going to do uh our return so I'll say return our updated book and then else we are going to go ahead and return or raise an HTTP exception since the book is not found now also going to look into other ways to handle these exceptions in a later video but for now what you can just do is to just simply raise 44 not found uh to delete we're going to do the same thing so I have to go ahead and inject our session object right in here and once that is done then we're going to just say uh book to delete so we don't even expect this to return a response because it is a 2 44 204 no content kind of response so this is going to be equal to await book service Dot and in this case we are going to call delete book so in this case we are simply going to provide our session as well as our book uid so just like you see right here this requires us to provide the book U ID so I think that's something we forgot so we have to go back here delete book I think we have to name this something else uh I think this is book delete and it's going to take in the session so this is going to be um delete book taking the book Q ID as well as a session so I guess I have to do that so this is going to be book service. delete book and in this case what we're going to have is our book U ID first as well as our session so if we return the book we have deleted uh in this case we can just simply return something so let's just return an empty dictionary and in case that book is not found then we shall return none so I'll do the same thing if book to delete then we shall return nothing because this is a 204 non found and then once we have not form the book we shall say else and then this is going to return our HTTP exception so this is going to be our status code of for 0 for not found another thing we can do is to change this so we having book ID and this is supposed to be book U ID so let's go ahead and correct that in other method so this is book ID and then we need the book uid and then we shall do the same thing so we have book uid book U ID book U ID book Q ID and then book your ID book your ID so it see like we are now done our methods so let's check if our survey is still running and if it's still running then I think it's time we tested this API end points so let's go ahead to our rest Fox and try to test this out so right here in my rest Fox I'm going to begin by fing all books so to do that we simply going to make use of the API end points we did when performing our Crow so just press send and this is going to return an empty list of books meaning that we do not have any books created yet so we're going to head over to where I create a book so this is the structure for creating a book and this was making use of our simple list but remember we using ID so I decided to eliminate the ID and eliminate all other fields that may not be needed so when you try to create a book we're now going to get an error so right here the error is telling us that we are having an error and that error is coming from invalid input type string object has no attribute to Orin so this is something that we can easily handle so for us to fix that error what we're going to do is to go back to our service file and fix the way we are storing our date inside our database table so what first is telling us is this is actually coming from SQL Aly so SQL Alchemy is telling us that we are trying to fill a date a date fi with a string and therefore it can't access that specific method on that string object so for us to do that we are going to head over to our service and then Define how we're going to be storing the date at which we published that specific because I think it comes from the published date field so for us to fix that we are now going to head over to where I create a book and when you create a book we're now going to also modify the attribute of the published date to a date time or a date object so we're going to first begin by importing the date class so we're going to say from date time we are going to go ahead and import our dat time class and once we've imported the DAT time class now let's go ahead and modify this new book so shall say new book dot in this case we shall say published date so the published date is going to be equal to dat time dot in this case we are going to access it as we actually going to just change it to a DAT time object using that do STP STP time and what is going taken is a string that we provide right here as a string so this is going to be our date that shall access from our book data so we shall just come right in here and say the object or the type of data that you're going to get from our book data dict uh this is going to be our published date so I guess I have to just copy it and then simply paste it so right after doing that then we're going to specify the format in which this St object is going to be created so this is going to be created in this format which is going to have the year remember we using year month and date so in this case it's going to be percentage why meaning yeah then Dash this is going to be the month and then we are going to have the date so I guess this is going to fix our error so once we've set our published date as a date object then it's going to be finally saved to our database as a date so let's go ahead and try to create create a new book once again so if we try we now see that our book has been created to confirm this we're going to try to retrieve all books and now we can see that our books are being returned or our books are being returned from the database so we can also go ahead and Define other fields on a specific book object and we're going to look at how we can do that in the later video so let's go back and try to finish up so getting your book by its ID I'm going to copy the U ID of this specific book and once I have that then I'm going to head over to this part right here and try to fill in from an integer to a u ID and when we send we now see that we have uh input should be a valid integer unable to pass string as an integer so I think that is coming from where we are having the model that's going to return all these books so let's go ahead and fix that we shall go back to our schemas and within our schemas I think there's somewhere we still have an integer book book uid so path book uid uh this is going to be an integer I think that is within our routes so pantic is trying to so you see we still have integers in here so we can go ahead and change them to U IDs or strings so let me try change them to Strings when you change this to Strings I think it's going to work so when you provide them as strings we are still having an issue and so we are now having a message input should be a valid dictionary and I think this is coming from a response that we are supposed to return so let us see where that response is coming from so we are supposed to return a dictionary but it seem like we are returning something else that is not a dictionary and this is the stupid mistake I did right here so I'm going to fix that by saying return book and that should be returning a book so when you try once again input should be so this is a book object so the thing is when returning a response first requires us to return something that is Json serializable and in this case what we doing is to return a book object that we have right here so if we want to fix that then all we're going to do is to go ahead and specify a response model for this specific route Handler or this specific request now just come right in here and say response model and in this case we are going to go ahead and return the response model for our book object now let us make use of our book right here which is from our schemer so I'm going to be making use of this so I just come write where we have our RS and inside there is where we're going to make use of our book so I'm just simply going to remove our book model because this is somehow going to confuse us let us just use our schemers since we are now having the book schema so when we have that we're now going to come right in here and say that our response model is going to be a book and this is going to carry out any validations on our book object and return it in a Json serializable fashion so when you try to send this we still having an error published date input should be a valid string input is date so we need to also fix that now what is happening is when you try to access this uh we've changed this from a published date which is a string to a published date that is a date so notice that our Fields defined on our model should be in the same way so in case you define this as a string you should have your your schemas using that as a string in case you defined it as a date then you should also be a date now I'm going to go ahead and change that by simply going to schemas p and and then inside there I'll change how we are returning our published dat I'm going to go ahead and change this to a de all right so now when you try to send still we are having an error so let's go ahead and fix that our date is not defined so I guess I'll have to go ahead and import it from date time so when you save um cancel this request and before I cancel it we now see that our response has been returned so when we return we're now return in that specific book object all right so two things are working let's go ahead and now uh try to update as well as to delete a book so updating a book is going to require us to have an idea of that specific book I'm going to come right in here and get and ID simply one that we have right here so let me try to create some more books so this is headfast HTML 5 programming what if we change this to something like Java head fast Java and then say we called our author John do uh we can also change the publisher to something so just simply going to say um whatever this is going to be and when you try to create another date so let's say this was created in OCT October 2024 or let's say October 2022 if we send um we have some errors right here so we having fields that are actually missing so I guess this is something that's coming from so let's see where where where this area is coming from so this is coming from so we missing the created art date as well as the update at dat so we're having some missing fields we have the uid as well as the update update and I wonder why this is happening because we already defined this inside our book so let's see why this is happening so I think this is happening in the response because the response is returning a book response model and this book right here is something that we are not so let's try to debug this I guess our book data is being for being being used in a book fashion so this is requiring us to even provide these fields that are part of our book yet we have a book create model so forgive me for that I just come and say that we going to make use of our book create mod model which is defined specifically for creating a book I'm going to go ahead and change this from book to book create model and this shall be just enough for us to create a book just like you see right here all right let's go ahead and finish this up uh when you go so we've created a second book we now have two books and these are ordered by the data which they were created so I hope you can also note that so we can create another book let's say this is going to be uh jungle jungle five by example and this is going to be by the author I love the most which is Antonio male I don't know whether that's the right way to pronounce his name that's the name is Antonio Mel he created one of the books that Tau me jungle when I didn't know jungle so I'll say this is Antonio mail and then this is going to be let's say by packed Publishers so I'll say 2024 let's say March so 03 and then let's say this has 200 Pages or 300 whatever and the language is English when you create that book we now see that that book has been created all right so let's try to update a book so I'm going to copy a random uid of a certain book um let me see I'm going to copy this one for head fast HTML and once I've copied it I'm now going to go ahead and update it so of where we update a book now let's make sure that our book update model is what we using which is okay um so we're going to head over right there and I'm going to change this from head fast HTML 5 programming to something like let's say HTML for Dames and then we can also change this to something like let's say G do so this is going to be uh really and yeah let's try to so we have an error if you fix this uh we never awaited something so get book was never awaited so I have to go back to our service and then inside there is where we're going to go ahead and change our update so that we can use await so that's a mistake I was doing I'll just simply say await and then I think that's the same thing I'm going to do right here so these are some of the mistakes you may land in uh but uh I think think we have fixed it so when you go back right here and send still have an error so this error is coming from invalid input for query argument one we have gu ID and this is actually supposed to be for the U ID so I made a mistake we supposed to provide the ID of that specific book we had to update sorry for that let me just copy this U ID that we have here well I think I even copy the U ID so I'll just provide that and when we send still having a lot of Errors so input should be a valid dictionary I think we need to do the same thing so I'll just go back to our routes and then specify the specify the response model so this is going to be our response model but this is going to be one for the book so shall say book and when we save our server shall restart then we shall shall return and this is going to be uh updated successfully okay so let's try to ret to delete a book now I'm going to go and delete the same book I provide it to you U ID right here and now we see that the book is not found I wonder why so let's try to see because this book is existing in our database uh let's try to all right so after a series of debagging I decided that the best way to handle this was to first of all handle what happens when the object that we've got from our query is nine and in this case what I did was to write if update book is n or if the updated book so that's actually if the update book if the updated book that we have right here is n then we shall return an exception or raise an exception for it not being found and if it's actually found then the result will be for us to return the updated book so after doing that when you go back right here that tended to work for updating if we delete an object so let me try to delete this specific book that we have right here I'll just copy it ID and when I copy it ID when we try to send it right here now see the book is not found so the the thing is the idea is that when we when we are handling such a scenario we have to first check for the book not being found so if we say if book. delete is none then we are going to first of all raise an exception in case it is not found and when we find it we are now going to go ahead and that specific book so that was the entire mistake I was doing so what I'm going to do is to just simply come right here and say if the book is none then we're going to go ahead and return that and in case we do not return a book Fast API is not okay without with us using none in a response so that was just the case so when you try to go to our books right here and we said that book has been deleted and when we try to get it we now see that the book is not found so that is the problem what we are doing is to first of all handle what happens when the book is got but we are not properly handling what is happening with the book in this video we've been able to look at how we could rewrite our using SQL model we've looked at how we can integrate some of the features in SQL Alchemy to use them together with SQL model to come up with a session we've also got introduced to dependen injection the mechanism that can help us to share logic across very many API in prints thank you guys for watching and if you have a question about this video of which I know you do please leave a comment down in the description telling me about what you learned or telling me about some suggestions that you have about the approaches that we use in this video I hope to see you in the next video and I promise this is the last cred video of this specific series thanks for watching guys and yeah I'll see you in the next video you by
Info
Channel: Ssali Jonathan
Views: 488
Rating: undefined out of 5
Keywords:
Id: AtHEX76Wysw
Channel Id: undefined
Length: 67min 28sec (4048 seconds)
Published: Tue Jun 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.