-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
main.rs
116 lines (99 loc) · 3.57 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_sync_db_pools;
#[macro_use] extern crate diesel;
#[cfg(test)]
mod tests;
mod task;
use rocket::{Rocket, Build};
use rocket::fairing::AdHoc;
use rocket::request::FlashMessage;
use rocket::response::{Flash, Redirect};
use rocket::serde::Serialize;
use rocket::form::Form;
use rocket::fs::{FileServer, relative};
use rocket_dyn_templates::Template;
use crate::task::{Task, Todo};
#[database("sqlite_database")]
pub struct DbConn(diesel::SqliteConnection);
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
struct Context {
flash: Option<(String, String)>,
tasks: Vec<Task>
}
impl Context {
pub async fn err<M: std::fmt::Display>(conn: &DbConn, msg: M) -> Context {
Context {
flash: Some(("error".into(), msg.to_string())),
tasks: Task::all(conn).await.unwrap_or_default()
}
}
pub async fn raw(conn: &DbConn, flash: Option<(String, String)>) -> Context {
match Task::all(conn).await {
Ok(tasks) => Context { flash, tasks },
Err(e) => {
error!("DB Task::all() error: {e}");
Context {
flash: Some(("error".into(), "Fail to access database.".into())),
tasks: vec![]
}
}
}
}
}
#[post("/", data = "<todo_form>")]
async fn new(todo_form: Form<Todo>, conn: DbConn) -> Flash<Redirect> {
let todo = todo_form.into_inner();
if todo.description.is_empty() {
Flash::error(Redirect::to("/"), "Description cannot be empty.")
} else if let Err(e) = Task::insert(todo, &conn).await {
error!("DB insertion error: {e}");
Flash::error(Redirect::to("/"), "Todo could not be inserted due an internal error.")
} else {
Flash::success(Redirect::to("/"), "Todo successfully added.")
}
}
#[put("/<id>")]
async fn toggle(id: i32, conn: DbConn) -> Result<Redirect, Template> {
match Task::toggle_with_id(id, &conn).await {
Ok(_) => Ok(Redirect::to("/")),
Err(e) => {
error!("DB toggle({id}) error: {e}");
Err(Template::render("index", Context::err(&conn, "Failed to toggle task.").await))
}
}
}
#[delete("/<id>")]
async fn delete(id: i32, conn: DbConn) -> Result<Flash<Redirect>, Template> {
match Task::delete_with_id(id, &conn).await {
Ok(_) => Ok(Flash::success(Redirect::to("/"), "Todo was deleted.")),
Err(e) => {
error!("DB deletion({id}) error: {e}");
Err(Template::render("index", Context::err(&conn, "Failed to delete task.").await))
}
}
}
#[get("/")]
async fn index(flash: Option<FlashMessage<'_>>, conn: DbConn) -> Template {
let flash = flash.map(FlashMessage::into_inner);
Template::render("index", Context::raw(&conn, flash).await)
}
async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
DbConn::get_one(&rocket).await
.expect("database connection")
.run(|conn| { conn.run_pending_migrations(MIGRATIONS).expect("diesel migrations"); })
.await;
rocket
}
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(DbConn::fairing())
.attach(Template::fairing())
.attach(AdHoc::on_ignite("Run Migrations", run_migrations))
.mount("/", FileServer::new(relative!("static")))
.mount("/", routes![index])
.mount("/todo", routes![new, toggle, delete])
}