mirror of https://github.com/rwf2/Rocket.git
96 lines
2.7 KiB
Rust
96 lines
2.7 KiB
Rust
#![feature(plugin, custom_derive, const_fn)]
|
|
#![plugin(rocket_codegen)]
|
|
|
|
extern crate rocket;
|
|
extern crate serde_json;
|
|
#[macro_use] extern crate diesel;
|
|
#[macro_use] extern crate diesel_codegen;
|
|
#[macro_use] extern crate serde_derive;
|
|
extern crate rocket_contrib;
|
|
extern crate r2d2;
|
|
extern crate r2d2_diesel;
|
|
|
|
mod static_files;
|
|
mod task;
|
|
mod db;
|
|
#[cfg(test)] mod tests;
|
|
|
|
use rocket::Rocket;
|
|
use rocket::request::{Form, FlashMessage};
|
|
use rocket::response::{Flash, Redirect};
|
|
use rocket_contrib::Template;
|
|
|
|
use task::{Task, Todo};
|
|
|
|
#[derive(Debug, Serialize)]
|
|
struct Context<'a, 'b>{ msg: Option<(&'a str, &'b str)>, tasks: Vec<Task> }
|
|
|
|
impl<'a, 'b> Context<'a, 'b> {
|
|
pub fn err(conn: &db::Conn, msg: &'a str) -> Context<'static, 'a> {
|
|
Context{msg: Some(("error", msg)), tasks: Task::all(conn)}
|
|
}
|
|
|
|
pub fn raw(conn: &db::Conn, msg: Option<(&'a str, &'b str)>) -> Context<'a, 'b> {
|
|
Context{msg: msg, tasks: Task::all(conn)}
|
|
}
|
|
}
|
|
|
|
#[post("/", data = "<todo_form>")]
|
|
fn new(todo_form: Form<Todo>, conn: db::Conn) -> Flash<Redirect> {
|
|
let todo = todo_form.into_inner();
|
|
if todo.description.is_empty() {
|
|
Flash::error(Redirect::to("/"), "Description cannot be empty.")
|
|
} else if Task::insert(todo, &conn) {
|
|
Flash::success(Redirect::to("/"), "Todo successfully added.")
|
|
} else {
|
|
Flash::error(Redirect::to("/"), "Whoops! The server failed.")
|
|
}
|
|
}
|
|
|
|
#[put("/<id>")]
|
|
fn toggle(id: i32, conn: db::Conn) -> Result<Redirect, Template> {
|
|
if Task::toggle_with_id(id, &conn) {
|
|
Ok(Redirect::to("/"))
|
|
} else {
|
|
Err(Template::render("index", &Context::err(&conn, "Couldn't toggle task.")))
|
|
}
|
|
}
|
|
|
|
#[delete("/<id>")]
|
|
fn delete(id: i32, conn: db::Conn) -> Result<Flash<Redirect>, Template> {
|
|
if Task::delete_with_id(id, &conn) {
|
|
Ok(Flash::success(Redirect::to("/"), "Todo was deleted."))
|
|
} else {
|
|
Err(Template::render("index", &Context::err(&conn, "Couldn't delete task.")))
|
|
}
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index(msg: Option<FlashMessage>, conn: db::Conn) -> Template {
|
|
Template::render("index", &match msg {
|
|
Some(ref msg) => Context::raw(&conn, Some((msg.name(), msg.msg()))),
|
|
None => Context::raw(&conn, None),
|
|
})
|
|
}
|
|
|
|
fn rocket() -> (Rocket, Option<db::Conn>) {
|
|
let pool = db::init_pool();
|
|
let conn = if cfg!(test) {
|
|
Some(db::Conn(pool.get().expect("database connection for testing")))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let rocket = rocket::ignite()
|
|
.manage(pool)
|
|
.mount("/", routes![index, static_files::all])
|
|
.mount("/todo/", routes![new, toggle, delete])
|
|
.attach(Template::fairing());
|
|
|
|
(rocket, conn)
|
|
}
|
|
|
|
fn main() {
|
|
rocket().0.launch();
|
|
}
|