2021-04-29 12:19:24 +00:00
|
|
|
use rocket::serde::Serialize;
|
2020-06-08 19:11:34 +00:00
|
|
|
use diesel::{self, result::QueryResult, prelude::*};
|
2017-02-03 01:38:36 +00:00
|
|
|
|
2017-02-02 08:41:47 +00:00
|
|
|
mod schema {
|
2017-12-29 23:03:06 +00:00
|
|
|
table! {
|
|
|
|
tasks {
|
|
|
|
id -> Nullable<Integer>,
|
|
|
|
description -> Text,
|
|
|
|
completed -> Bool,
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 08:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 23:03:06 +00:00
|
|
|
use self::schema::tasks;
|
|
|
|
|
2020-07-11 18:17:43 +00:00
|
|
|
use crate::DbConn;
|
|
|
|
|
2021-04-29 12:19:24 +00:00
|
|
|
#[derive(Serialize, Queryable, Insertable, Debug, Clone)]
|
|
|
|
#[serde(crate = "rocket::serde")]
|
2022-06-21 14:07:21 +00:00
|
|
|
#[diesel(table_name = tasks)]
|
2016-08-02 02:07:36 +00:00
|
|
|
pub struct Task {
|
2022-06-21 14:07:21 +00:00
|
|
|
#[serde(skip_deserializing)]
|
2017-05-26 23:44:53 +00:00
|
|
|
pub id: Option<i32>,
|
|
|
|
pub description: String,
|
|
|
|
pub completed: bool
|
|
|
|
}
|
|
|
|
|
2020-06-08 19:11:34 +00:00
|
|
|
#[derive(Debug, FromForm)]
|
2017-05-26 23:44:53 +00:00
|
|
|
pub struct Todo {
|
2016-08-02 02:07:36 +00:00
|
|
|
pub description: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Task {
|
2020-09-03 07:12:00 +00:00
|
|
|
pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> {
|
2020-07-11 18:17:43 +00:00
|
|
|
conn.run(|c| {
|
2022-06-21 14:07:21 +00:00
|
|
|
tasks::table.order(tasks::id.desc()).load::<Task>(c)
|
2020-07-11 18:17:43 +00:00
|
|
|
}).await
|
2016-08-02 02:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:11:34 +00:00
|
|
|
/// Returns the number of affected rows: 1.
|
2020-09-03 07:12:00 +00:00
|
|
|
pub async fn insert(todo: Todo, conn: &DbConn) -> QueryResult<usize> {
|
2020-07-11 18:17:43 +00:00
|
|
|
conn.run(|c| {
|
|
|
|
let t = Task { id: None, description: todo.description, completed: false };
|
|
|
|
diesel::insert_into(tasks::table).values(&t).execute(c)
|
|
|
|
}).await
|
2016-08-02 02:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:11:34 +00:00
|
|
|
/// Returns the number of affected rows: 1.
|
2020-09-03 07:12:00 +00:00
|
|
|
pub async fn toggle_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
|
2020-07-11 18:17:43 +00:00
|
|
|
conn.run(move |c| {
|
2022-06-21 14:07:21 +00:00
|
|
|
let task = tasks::table.filter(tasks::id.eq(id)).get_result::<Task>(c)?;
|
2020-07-11 18:17:43 +00:00
|
|
|
let new_status = !task.completed;
|
2022-06-21 14:07:21 +00:00
|
|
|
let updated_task = diesel::update(tasks::table.filter(tasks::id.eq(id)));
|
|
|
|
updated_task.set(tasks::completed.eq(new_status)).execute(c)
|
2020-07-11 18:17:43 +00:00
|
|
|
}).await
|
2016-08-02 02:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:11:34 +00:00
|
|
|
/// Returns the number of affected rows: 1.
|
2020-09-03 07:12:00 +00:00
|
|
|
pub async fn delete_with_id(id: i32, conn: &DbConn) -> QueryResult<usize> {
|
2022-06-21 14:07:21 +00:00
|
|
|
conn.run(move |c| diesel::delete(tasks::table)
|
|
|
|
.filter(tasks::id.eq(id))
|
|
|
|
.execute(c))
|
|
|
|
.await
|
2016-08-02 02:07:36 +00:00
|
|
|
}
|
2018-12-30 21:52:08 +00:00
|
|
|
|
2020-06-08 19:11:34 +00:00
|
|
|
/// Returns the number of affected rows.
|
2018-12-30 21:52:08 +00:00
|
|
|
#[cfg(test)]
|
2020-09-03 07:12:00 +00:00
|
|
|
pub async fn delete_all(conn: &DbConn) -> QueryResult<usize> {
|
2022-06-21 14:07:21 +00:00
|
|
|
conn.run(|c| diesel::delete(tasks::table).execute(c)).await
|
2018-12-30 21:52:08 +00:00
|
|
|
}
|
2016-08-02 02:07:36 +00:00
|
|
|
}
|