In the first part of the project code, the management of movie information in the JSON file has been implemented, and the addition and deletion operations of movies are restricted through role-based access.
This time, the code has been modified to change the project to a workspace, and the JSON file storing movies has been changed to 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, and modifying movie information, as well as user role identification functionality.
Additionally, the sql_tools.rs part implements the database initialization operation, 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. There is still room for improvement in the future.