Inserting data into the database
In this section, we build a view that creates a to-do item. If we remember the rules regarding us creating, 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 fail with a filter based on the title that is passed into the view. We then check, and if no results are returned, we 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 of all, we reconfigure the imports, as seen in the following code block:
use crate::diesel; use diesel::prelude::*; use actix_web::HttpRequest; use actix_web::Responder; use crate::database::establish_connection; use crate::models::item::new_item::NewItem; use crate::models::item::item::Item; use crate::schema::to_do; use super::utils::return_state;
We import the necessary diesel
imports to make a query as described in the previous...