diff --git a/.idea/dataSources.local.xml b/.idea/dataSources.local.xml new file mode 100644 index 0000000..d43fad1 --- /dev/null +++ b/.idea/dataSources.local.xml @@ -0,0 +1,18 @@ + + + + + + " + + + master_key + no-auth + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..4408f54 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/db/dev.db + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..5f9bf22 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/project_test.iml b/project_test.iml index 8f66a1c..37ef045 100644 --- a/project_test.iml +++ b/project_test.iml @@ -5,6 +5,7 @@ + diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs new file mode 100644 index 0000000..f3355ff --- /dev/null +++ b/src/controllers/mod.rs @@ -0,0 +1,8 @@ +use rocket::get; + +pub mod tasks_controller; + +#[get("/")] +pub fn index() -> &'static str { + "Hello, world!" +} \ No newline at end of file diff --git a/src/controllers/tasks_controller.rs b/src/controllers/tasks_controller.rs new file mode 100644 index 0000000..812a1c8 --- /dev/null +++ b/src/controllers/tasks_controller.rs @@ -0,0 +1,8 @@ +use rocket::serde::json::Json; +use rocket::get; +use crate::models::{DbConn, task::Task}; + +#[get("/tasks/")] +pub async fn show(conn: DbConn, id: i32) -> Option> { + Task::find_by(&conn, id).await.map(Json).ok() +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 49a671a..ee0d967 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,24 +2,11 @@ #[macro_use] extern crate diesel_migrations; mod models; +mod controllers; use rocket::*; use rocket::fairing::AdHoc; -use rocket_sync_db_pools::{database}; - -#[database("sqlite_logs")] -pub struct DbConn(diesel::SqliteConnection); - -#[get("/")] -fn index() -> &'static str { - "Hello, world!" -} - -#[get("/log/")] -async fn get_log(conn: DbConn, id: i32) -> String { - let result = models::task::Task::all(&conn).await; - format!("test {}, {:?}", id, result) -} +use models::DbConn; async fn run_migrations(rocket: Rocket) -> Rocket { embed_migrations!(); @@ -33,7 +20,10 @@ async fn run_migrations(rocket: Rocket) -> Rocket { #[launch] fn rocket() -> _ { rocket::build() - .mount("/", routes![index, get_log]) + .mount("/", routes![ + controllers::index, + controllers::tasks_controller::show + ]) .attach(DbConn::fairing()) .attach(AdHoc::on_ignite("Run Migrations", run_migrations)) } diff --git a/src/models/mod.rs b/src/models/mod.rs index 2f2b628..649adc6 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1 +1,7 @@ +use rocket_sync_db_pools::database; +use diesel::SqliteConnection; + +#[database("sqlite_logs")] +pub struct DbConn(SqliteConnection); + pub mod task; \ No newline at end of file diff --git a/src/models/task.rs b/src/models/task.rs index d7dcd99..0579a57 100644 --- a/src/models/task.rs +++ b/src/models/task.rs @@ -1,18 +1,15 @@ use rocket::serde::Serialize; use diesel::{self, result::QueryResult, Queryable, Insertable, prelude::*}; +use super::DbConn; table! { - tasks { + tasks (id) { id -> Nullable, description -> Text, completed -> Bool, } } -use tasks::dsl::{tasks as all_tasks}; - -use crate::DbConn; - #[derive(Serialize, Queryable, Insertable, Debug, Clone)] #[serde(crate = "rocket::serde")] #[table_name="tasks"] @@ -24,8 +21,16 @@ pub struct Task { impl Task { pub async fn all(conn: &DbConn) -> QueryResult> { - conn.run(|c| { - all_tasks.order(tasks::id.desc()).load::(c) + conn.run(move |c| { + tasks::table.load(c) + }).await + } + + pub async fn find_by(conn: &DbConn, id: i32) -> QueryResult { + conn.run(move |c| { + tasks::table + .filter(tasks::id.eq(id)) + .first(c) }).await } } \ No newline at end of file