Inserting into the database
In this section, we will build and create a view that creates a to-do item. If we remember the rules around our creating of to-do items, we do not want to create duplicate to-do items. This can be done with a unique constraint. However, for now, it is good to keep things simple. Instead, we will make a database fall with a filter based on the title that is passed into the view. We then check, and if no results are returned, we will insert a new to-do
item into the database. We do this by refactoring the code in the views/to_do/create.rs
file. First, we reconfigure the imports as seen in the following code:
use crate::diesel; use diesel::prelude::*; use actix_web::HttpResponse; use actix_web::HttpRequest; use crate::json_serialization::to_do_items::ToDoItems; use crate::database::establish_connection; use crate::models::item::new_item::NewItem; use crate::models::item::item::Item; use crate::schema::to_do;
In the preceding code, we import the necessary...