From 4bee2732168ae87a6885c8d08c94b3e574d8869d Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Thu, 8 Jul 2021 22:48:27 +0200 Subject: [PATCH] Add CRUD Task --- src/controllers/tasks_controller.rs | 22 ++++++++++++++++++- src/main.rs | 6 +++++- src/models/task.rs | 33 +++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/controllers/tasks_controller.rs b/src/controllers/tasks_controller.rs index 812a1c8..24824d9 100644 --- a/src/controllers/tasks_controller.rs +++ b/src/controllers/tasks_controller.rs @@ -1,8 +1,28 @@ use rocket::serde::json::Json; -use rocket::get; +use rocket::{get, post, patch, delete}; use crate::models::{DbConn, task::Task}; +#[get("/tasks")] +pub async fn index(conn: DbConn) -> Option>> { + Task::all(&conn).await.map(Json).ok() +} + #[get("/tasks/")] pub async fn show(conn: DbConn, id: i32) -> Option> { Task::find_by(&conn, id).await.map(Json).ok() +} + +#[post("/tasks", data = "")] +pub async fn create(conn: DbConn, task: Json) -> Option> { + Task::create(conn, task.clone()).await.map(Json).ok() +} + +#[patch("/tasks/", data = "")] +pub async fn update(conn: DbConn, id: i32, task: Json) -> Option> { + Task::update(conn, id, task.clone()).await.map(Json).ok() +} + +#[delete("/tasks/")] +pub async fn delete(conn: DbConn, id: i32) -> Option> { + Task::delete(conn, id).await.map(Json).ok() } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ee0d967..1be8de7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,11 @@ fn rocket() -> _ { rocket::build() .mount("/", routes![ controllers::index, - controllers::tasks_controller::show + controllers::tasks_controller::index, + controllers::tasks_controller::show, + controllers::tasks_controller::create, + controllers::tasks_controller::update, + controllers::tasks_controller::delete ]) .attach(DbConn::fairing()) .attach(AdHoc::on_ignite("Run Migrations", run_migrations)) diff --git a/src/models/task.rs b/src/models/task.rs index 0579a57..2ae4d5f 100644 --- a/src/models/task.rs +++ b/src/models/task.rs @@ -1,5 +1,5 @@ -use rocket::serde::Serialize; -use diesel::{self, result::QueryResult, Queryable, Insertable, prelude::*}; +use rocket::serde::{Serialize, Deserialize}; +use diesel::{self, result::QueryResult, Queryable, Insertable, AsChangeset, prelude::*}; use super::DbConn; table! { @@ -10,7 +10,7 @@ table! { } } -#[derive(Serialize, Queryable, Insertable, Debug, Clone)] +#[derive(Serialize, Deserialize, Queryable, Insertable, AsChangeset, Debug, Clone)] #[serde(crate = "rocket::serde")] #[table_name="tasks"] pub struct Task { @@ -21,7 +21,7 @@ pub struct Task { impl Task { pub async fn all(conn: &DbConn) -> QueryResult> { - conn.run(move |c| { + conn.run(|c| { tasks::table.load(c) }).await } @@ -33,4 +33,29 @@ impl Task { .first(c) }).await } + + pub async fn create(db: DbConn, task: Task) -> QueryResult { + db.run(move |conn| { + diesel::insert_into(tasks::table) + .values(&task) + .execute(conn) + }).await + } + + pub async fn update(db: DbConn, id: i32, task: Task) -> QueryResult { + db.run(move |conn| { + diesel::update(tasks::table) + .set(&task) + .filter(tasks::id.eq(id)) + .execute(conn) + }).await + } + + pub async fn delete(db: DbConn, id: i32) -> QueryResult { + db.run(move |conn| { + diesel::delete(tasks::table) + .filter(tasks::id.eq(id)) + .execute(conn) + }).await + } } \ No newline at end of file