Add CRUD Task
This commit is contained in:
parent
ce7ef4807b
commit
4bee273216
3 changed files with 55 additions and 6 deletions
|
@ -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<Json<Vec<Task>>> {
|
||||
Task::all(&conn).await.map(Json).ok()
|
||||
}
|
||||
|
||||
#[get("/tasks/<id>")]
|
||||
pub async fn show(conn: DbConn, id: i32) -> Option<Json<Task>> {
|
||||
Task::find_by(&conn, id).await.map(Json).ok()
|
||||
}
|
||||
|
||||
#[post("/tasks", data = "<task>")]
|
||||
pub async fn create(conn: DbConn, task: Json<Task>) -> Option<Json<usize>> {
|
||||
Task::create(conn, task.clone()).await.map(Json).ok()
|
||||
}
|
||||
|
||||
#[patch("/tasks/<id>", data = "<task>")]
|
||||
pub async fn update(conn: DbConn, id: i32, task: Json<Task>) -> Option<Json<usize>> {
|
||||
Task::update(conn, id, task.clone()).await.map(Json).ok()
|
||||
}
|
||||
|
||||
#[delete("/tasks/<id>")]
|
||||
pub async fn delete(conn: DbConn, id: i32) -> Option<Json<usize>> {
|
||||
Task::delete(conn, id).await.map(Json).ok()
|
||||
}
|
|
@ -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))
|
||||
|
|
|
@ -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<Vec<Task>> {
|
||||
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<usize> {
|
||||
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<usize> {
|
||||
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<usize> {
|
||||
db.run(move |conn| {
|
||||
diesel::delete(tasks::table)
|
||||
.filter(tasks::id.eq(id))
|
||||
.execute(conn)
|
||||
}).await
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue