This is apparently the most loved CRUD app of 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
software developers we're an opinionated Bunch yes especially when it comes to our favorite Tex Stacks fortunately stack Overflow provides us with an annual survey which we can use to provide data on why our chosen Tech stack is the best my opinions the only one that counts and that's exactly what I'm going to do with this video I'm going to create a crowd app using the most loved Technologies according to the 2023 stack Overflow developer survey first we need to find out which language we're going to use for our crud app let's open up the 2023 developer survey and check out the programming scripting and markup languages section this list contains some languages that are a little concerning such as matlb cobal and Visual Basic which I really don't want to be building a CR out with luckily these are at the lower end of the spectrum unfortunately I'm not doing a video on the most despised Tex St not until I hit at least 75,000 Subs so uh please don't press press that subscribe button fortunately on the other end of the list we have rust as the most loved language just beating out Elixir for first place perfect we'll go with that next we need a web framework while stack Overflow does provide a list of the most loved web Frameworks it doesn't provide an option for any rust based one it does provide one for Elixir through Phoenix but as we're not using Elixir well it's no good fortunately however I have done a video on my own favorite rust web framework but with not going to use it in this video instead we're going to use axom which a lot of you had a high amount of praise for in the comments so it feels like a good choice for our most loved text stack lastly every good CR app needs a database luckily for me postgres is the winner here on the stack Overflow developer survey although I wouldn't have minded using reddis which came in at a close second with our application stack decided let's take a quick peek at what we're going to build for this video we're going to create a simple crud app to manage book quotes using an HTTP Json API interface our application will allow for crud operations using the following API endpoints and our data model will consist of the book title and the quote itself simple stuff we're also going to deploy this application to the cloud for free but more on that in a minute first we need to create a new project using cargo you can do so by using the cargo new command in your terminal for this project we're using rust 1.72 so make sure install it using rust up as per your operating system with our project set up we now need to add in the dependencies via the cargo. Tomo the dependencies we want are axom which is our web server Sur and Sur Json for encoding and decoding Tokyo which is our application asynchronous runtime SQL X which allows us to connect to our postgres database and The UU ID and Chrono data types which will allow us to store unique identifiers and timestamps in our data model when adding these to your own cargo. Tomo make sure to set the same feature flags as I have with our initial dependencies added let's whip up a basic service to begin open up the main. RS file in your text editor then go ahead and delete everything inside of it next add in the following lines of code here we're creating an asynchronous main function to initialize our application which will create and start an axom server on Port 3000 next we want to create a health check endpoint which will return the status code of 200 we can create this using the following function and binding it to our SL path in the application router now we can go ahead and test this code let's open up a new terminal window and use the cargo run command to both build and run our application once the application is running we can open up another terminal window and send a c request to Local Host 3000 we receive back the HTTP status code of 200 or okay which tells us everything is working correctly so far so good so what's next well personally I like to deploy my applications as soon as possible the reason for this is that it's always easier to do so when the application is less complex this also means it's much easier to discover any problems as you iterate rather than having to sift through a larger amount of code later on we have a few options to deploy this typically I'd use my new home lab but that's not ideal for everyone following at home at least until I start doing some more home lab based content still I wanted a free option which typically Cloud providers such as AWS don't really provide so I'm going to use the sponsor of today's video flow to deploy our application flow's based offering provides us with a free application and a database which is harder and harder to find in this economy that database also happens to be postr so it'll work with our chosen text a in order to deploy to flow we're going to first need to containerize our application fortunately Docker also happens to sit pretty high on the most love Technologies list I love it when a plan comes together let's go ahead and create a Docker file for use the flow documentation has an example on how to do this for rust applications let's go ahead and copy this pasting it in we then need to make a few modifications in order to work for our own application the first is to change the version of rust to 1.72 the second is to build our app in release mode which gives us much better performance and lastly we just need to change the reference of template - rust to our application's name quotes with our Docker file complete we can test that it's working using the Docker build command and if everything works you should see a similar output as on screen now we can actually test that the application runs by using the docker run command and exposing our Port of 3000 once it's running we can run the other curl command as we did before so far so good however we need to make one more change for our app to work with flow the documentation recommends dynamically setting your web server Port based on an environment variable passed into the application to make that change let's head on back over to our main. RS file and add in the following line to pull out the port from an environment variable or fall back to Port 3000 if the nvar doesn't exist with the code changes and Docker file completes we now need to get our code into a remote repository flow integrates with GitHub so let's go ahead and create a new repository for our code then we can go ahead and commit our code add the repository as the origin and push it on up now we're ready to deploy our application to do so first sign in to flow and then create a new works bace followed by adding a new project will'll give ours the name of most loved crud app next we need to link our GitHub account so Flo can access our code then we can create a new application with our repo in the application settings check to make sure you're using the correct branch and select the region you want to deploy on you can also see the port environment variable here as well which you can change to whatever you like after clicking deploy we get some lovely confetti and we can check the build and deploy logs for our applications code by the way flow are launching on product hunt so so be sure to check them out there you can check the link in the description down below after a short while everything should be deployed correctly and we can go ahead and test our application using the provided URL with that we have our app deployed with no cost very cool with everything deployed now is a good time to refactor our code let's first create a new handlers. RS file which will use to store all of our Handler logic we can start by moving the health Handler into here as a first step then we can head on back over to our main function import the Handler module and use it with our SL endpoint now we're ready to proceed the next thing we want to do is start creating our crud endpoints the best place to start a book is always at the beginning let's do the same here and start with the letter c AKA create as we're building a restful API the create endpoint will be a post method to the/ quotes path this endpoint will accept a request body with the Json schema as on screen which which is an object that expects a string for both the book and quote fields to represent this we can create a new struct in Rust and use the surdy macros to easily support the deserialization of our input model with this model defined we can now add our Handler function I got to say the recommendation to use axom was pretty spot-on it's a really nice framework to use especially for removing boiler plates such as request body decoding that you typically have to do with other Frameworks so far I'm impressed rest with our route defined we can now start thinking about our database integration to achieve this we're going to use the SQL X package for rust which works with both postgres and Tokyo which is the asynchronous runtime for axom it also happens to be my favorite SQL driver for rust and I have a pretty decent video on how to use it to begin let's head back over to our main function the first thing we want to do is pull out the database URL from the associated environment variable we'll also throw an exception if this doesn't exist next we can then create our PG pool options passing in the database URL we can then provide the database pool to our handlers using the with State function this way our HTTP Handler functions are able to have access to the database through the state parameter if they need it let's go ahead and modify our create Handler to accept it by adding in the following line pretty simple now that we have our database connectivity set up we need to add an instance or postgres to our applications deployment to set this up with flow is incredibly easy if we head back on over to the web page for our projects we get an option to add a database to it go ahead and set this up as I have done on screen once you're complete we should be given a database URL that we're able to connect with go ahead and copy this to your clipboard then heading over to a terminal add it to an environment variable now we're ready to set up our database in order to get our initial tables up and running the best way to do this is using database migrations fortunately the sqlx CLI can help us with this let's first install it using the following cargo command on our terminal once the CLI is installed we can now use it to generate our initial migrations using the command on screen this command will create a new directory called migrations with an initial file inside of it open up this file and add in the following lines of SQL to create our quote table this table will have an ID field which will be stored as a uu ID and a column for both the book and the quote itself which are of type varar and text respectively then we'll add a Tim stamp for both the inserted at and updated at times we also don't want to double up on quotes so we'll go ahead and make both the quote and book pair a unique constraint perfect now let's go ahead and run these migrations we can do this with the sqlx CLI as well make sure you're in the terminal where you set up your database URL environment variable and then run the following command if everything is successful we can now move on to writing our database query back over in our rust code let's first create another struct to represent the database model this is going to be similar to the model of our request body but will also contain the quote ID as a uu ID and our two timestamps we'll also add in a Constructor which will take both the book and quote as an input and generate a u ID and the necessary timestamps for us scrolling down to the create function first change the return type to what I have on screen this will return both a status code and Json in the event of a success and in the event of failure just an HTTP status code then let's use our Constructor method to create a new quote and add some SQL to insert it into our table then we want to bind our input parameters to the SQL statement followed by executing the command on our database pool next we'll check the result of this query and if everything was successful return the HTTP status code of created with our quote encoded as a Json response otherwise we'll return an internal server error next we just need to add an endpoint for the create Handler to our application router to do this first import the post method from the axom routing module and then add the following line to the app router in order to use the Handler with the SL quotes path with the create Handler integrated we're now able to commit our code and push it up to our GitHub repo flow will automatically monitor the main branch of our code and whenever it detects a change automatically build and deploy our code for us pretty neat after a short while the deployment should complete however in our case we've actually encountered an error fortunately flow uses AI to actually detect what went wrong and suggest what we need to do to fix it in our case we forgot to add the database URL as an environment variable so let's jump on over to our application settings to fix this up here flow also provides a button to import our database credentials as environment variables which makes it really easy to fix our problem what we have to do now is just save our settings both of these are fantastic quality of life features now that our environment variables are set up let's go ahead and redeploy this manually by pressing the following buttons after another short wait we should see everything deploy which means we can go ahead and test our new endpoint heading back to our terminal let's create a new quote using the following curl command if everything is successful you should receive a Json response with the full quote details and the ID showing that our read Handler is working as intended with our create endpoint completed let's move on to the read Handler here we're going to to keep it simple and only Implement a method to retrieve all of the quotes in our database in a production system you'd probably want to implement pagination and perhaps filtering but that's another Topic in itself if you're interested in any more production ready setup however then please let me know in the comments down below if I get enough interest then I'll probably set up an entire course on how to do so jumping back on over to our handlers code let's go ahead and create a new one for reading our quotes this method will return either a Json body with quotes or an HTTP status code to make pulling data out of the database a little easier let's add a new macro to our quote struct this is the from row macro from the sqlx library which allows us to automatically pull out rows from the database into our rust types now we can use the query as method from SQL X which will allow us to convert our rows automatically into our quote type using this with the fetch all method will give us a vector of quotes which happens to be our return type lastly we just want to match the response to either a success or failure depending on the result of the query with the Handler implementation complete we can now add it to our applications router in the main function we'll use the get method for this with the SL quotes path make sure that your handle function is also said to be public otherwise your build will fail then we just need to commit our code and push it up to GitHub all we have to do now is wait for flow to build and deploy it once it's deployed we can test our endpoint using curl and we get back an array containing our single quote to test this further let's go ahead and add another quote using our create endpoint now if we run our get request again we should receive an array containing both of them with that our read Handler is complete and we can move on to the next method the next letter in the acronym is U which stands for update using this method we're going to want to be able to update both the book and quote fields of an existing quote record to achieve this we're going to create an endpoint which accepts an ID as a path parameter and will be called using an HTTP put method the Handler will accept the same request body as our create endpoint back in our Handler code let's define a new function which takes the following three parameters the first is our state which provides our database connection pole the second is the quote uu ID pulled from the ID path parameter and finally the third is going to be a request body which is the same as our create Handler lastly we want to return an HTTP status code to determine whether or not the update operation was successful with our our function defined let's go ahead and implement it let's first create a variable storing the current time we'll use this to set the updated at Tim stamp in our data model next we can craft our SQL which will update the row with the provided ID setting both the book and the quote itself to the input values we'll also set the updated at timestamp to the value we stored earlier the last thing we want to do is check to see how many rows were actually affected by this update operation if the count of rows is zero then that means there was no entry for the ID given and we should return a 404 to the client to let them know that this resource doesn't exist otherwise we'll set a status code of okay lastly we'll check the result of this operation and return an internal server error if something went wrong now we can add this Handler to our application router we first need to import the put method from the aent package and then we can use it with our SL quotes slid path the syntax specifies that the path itself has a path parameter in our case called ID same as before let's commit our code and push it to our repo then we just need to wait for flow to automatically build our code and deploy it to test this I created an initial quote that is incorrect I can send a put request to the/ quotes slid path with my updated fields and I receive back an HTTP status code of 200 now if I retrieve all of the quotes again I can see that my changes have been applied and the quotes updated at Value has also changed the Last Action we need for our basic crud application is the delete functionality which will accept an HTTP delete request to a path containing the quote ID we wish to delete we should be pretty familiar with how to do this by now let's add a new function called delete quote into our handlers method this function will accept the database pool and The UU ID found in the path the sequel for this method is pretty simple deleting any rows from the quotes table where the ID matches our input for our status code return value let's use the same logic we use used in our update Handler where we check for the number of rows affected and return an appropriate status code accordingly with the Handler completed we can now add it to our application router using the delete method now that that's done we can commit this code and push it up to our repo which will then be deployed by flow once everything is deployed we can test it out using curl let's first grab the ID of the quote that we wish to delete we can then send a delete request to the following endpoint making sure to use the ID of the entry we wish to remove we receive back a00 which tells us everything works as expected which we can verify the last thing we want to test is deleting a quote that doesn't exist if we generate a brand new uu ID and pass this in as our path parameter we should receive a 44 when we try to delete that resource which we do showing that everything is working as expected and with that we've managed to create a simple CR app using the most loved Tech stack according to the stack Overflow developer survey at least using the sponsor of today today's video flow we were able to easily deploy this with integrated cicd at no extra cost which allows us to easily get started with an app idea without needing to worry about deployment this is a pretty awesome product in my opinion if you do want to move to a paid service well their pricing is pretty reasonable especially for this economy as always you can find this code on GitHub the link is in the description down below otherwise I want to give a big thank you to my newest Channel member jazil Sama thank you for supporting the channel and enabling me to bring content to hundreds of thousands of viewers a big thank you to everyone else for watching and I'll see you on the next one
Info
Channel: Dreams of Code
Views: 129,324
Rating: undefined out of 5
Keywords: rust, elixir, cobol, phoenix, postgresql, docker, crud, visual stuido, basic, matlab, crud api, api
Id: 3oN70MzSDfY
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Sun Oct 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.