Putting it all together
Now that we have covered authentication, passing JSON body data, and connecting to the database, we can put this all together and create a create API endpoint where we will authenticate our JWT, pass in data about the to-do item we are creating, and check to see if the item already exists. If the item does not exist, we must insert the new item that is being created.
Once we've done this, we need to get all of the items and process them to return the state of all the items in the database in relation to the user. We do not have to build any more dependencies to do this. Initially, we need to import the new item data model with the following code:
use crate::models::item::new_item::NewItem;
With this, we can start building our create view:
- First of all, we must define all the data that's needed for the create to-do item process:
#[post("/create", data="<item>", format = "json")] fn create(item: Json...