2016-08-02 02:07:36 +00:00
|
|
|
#![feature(plugin, custom_derive, custom_attribute)]
|
2016-08-06 04:57:55 +00:00
|
|
|
#![plugin(rocket_macros, serde_macros, diesel_codegen)]
|
2016-07-16 04:09:08 +00:00
|
|
|
|
|
|
|
extern crate rocket;
|
2016-08-02 02:07:36 +00:00
|
|
|
extern crate tera;
|
|
|
|
#[macro_use] extern crate diesel;
|
|
|
|
#[macro_use] extern crate lazy_static;
|
|
|
|
extern crate serde_json;
|
|
|
|
|
|
|
|
mod static_files;
|
|
|
|
mod task;
|
2016-07-16 04:09:08 +00:00
|
|
|
|
|
|
|
use rocket::Rocket;
|
|
|
|
use rocket::response::Redirect;
|
2016-08-02 02:07:36 +00:00
|
|
|
use task::Task;
|
|
|
|
|
|
|
|
lazy_static!(static ref TERA: tera::Tera = tera::Tera::new("templates/**/*"););
|
2016-07-16 04:09:08 +00:00
|
|
|
|
2016-08-02 02:07:36 +00:00
|
|
|
fn ctxt(error: Option<&str>) -> tera::Context {
|
|
|
|
let mut context = tera::Context::new();
|
|
|
|
context.add("error", &error.is_some());
|
|
|
|
context.add("msg", &error.unwrap_or("").to_string());
|
|
|
|
context.add("tasks", &Task::all());
|
|
|
|
context
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 02:07:36 +00:00
|
|
|
#[route(POST, path = "", form = "<todo>")]
|
|
|
|
fn new(todo: Task) -> Result<Redirect, tera::TeraResult<String>> {
|
|
|
|
if todo.description.is_empty() {
|
|
|
|
let context = ctxt(Some("Description cannot be empty."));
|
|
|
|
Err(TERA.render("index.html", context))
|
|
|
|
} else if todo.insert() {
|
|
|
|
Ok(Redirect::to("/")) // Say that it was added...somehow.
|
|
|
|
} else {
|
|
|
|
let context = ctxt(Some("Whoops! The server failed."));
|
|
|
|
Err(TERA.render("index.html", context))
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 04:09:08 +00:00
|
|
|
|
2016-08-02 02:07:36 +00:00
|
|
|
// Should likely do something to simulate PUT.
|
|
|
|
#[route(GET, path = "/<id>/toggle")]
|
|
|
|
fn toggle(id: i32) -> Result<Redirect, tera::TeraResult<String>> {
|
|
|
|
if Task::toggle_with_id(id) {
|
|
|
|
Ok(Redirect::to("/")) // Say that it was added...somehow.
|
|
|
|
} else {
|
|
|
|
let context = ctxt(Some("Could not toggle that task."));
|
|
|
|
Err(TERA.render("index.html", context))
|
|
|
|
}
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 02:07:36 +00:00
|
|
|
// Should likely do something to simulate DELETE.
|
|
|
|
#[route(GET, path = "/<id>/delete")]
|
|
|
|
fn delete(id: i32) -> Result<Redirect, tera::TeraResult<String>> {
|
|
|
|
if Task::delete_with_id(id) {
|
|
|
|
Ok(Redirect::to("/")) // Say that it was added...somehow.
|
|
|
|
} else {
|
|
|
|
let context = ctxt(Some("Could not delete that task."));
|
|
|
|
Err(TERA.render("index.html", context))
|
|
|
|
}
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[route(GET, path = "/")]
|
2016-08-02 02:07:36 +00:00
|
|
|
fn index() -> tera::TeraResult<String> {
|
|
|
|
TERA.render("index.html", ctxt(None))
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-08-02 02:07:36 +00:00
|
|
|
let mut rocket = Rocket::new("127.0.0.1", 8000);
|
|
|
|
rocket.mount("/", routes![index, static_files::all, static_files::all_level_one]);
|
|
|
|
rocket.mount("/todo/", routes![new, delete, toggle]);
|
2016-07-16 04:09:08 +00:00
|
|
|
rocket.launch();
|
|
|
|
}
|