In the first part of the project code, the management of movie information in the JSON file has been implemented, and operations for adding and deleting movies are restricted based on user roles.
This time, the code has been modified to change the project to a workspace, and the JSON file storing movies has been replaced with SQLite storage, allowing for CRUD operations using the database, as well as role determination using the database.
The specific code section is replaced by db_services.rs instead of the original services.rs. It includes operations for adding, deleting, updating, and querying movie information, as well as user role identification functionality.
The sql_tools.rs part implements the database initialization operations, adding a few test data entries when creating the database, and includes a function to read and transfer the JSON file to the database.
// db_tools.rs
// Transfer all movie information from the JSON file to the database
fn json_save_to_db(conn: &Connection) -> Result<(), Box<dyn Error>> {
let movies = read_form_json()?;
for movie in movies {
conn.execute(
"INSERT INTO movies (disc, year, title, remark, user_id) VALUES (?1, ?2, ?3, ?4, ?5)",
(movie.disc, movie.year, movie.title, movie.remark, 1),
)?;
}
Ok(())
}
The complete code can be found in the GitHub repository.
Currently, I am a beginner in Rust and feel that my code is ugly, and I haven't utilized Rust's struct features; it can be improved further in the future.