From 15267c895e04f782aaae5fc7c24cd89d6db54f95 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Mon, 10 Jul 2017 10:31:23 -0400 Subject: add: Page to add new quotes. --- src/data.rs | 1 + src/data/templates/add_post.hbs | 61 +++++++++++++++++++++++++++++++++++++++++ src/error.rs | 5 ++++ src/server.rs | 28 ++++++++++++++++++- 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/data/templates/add_post.hbs diff --git a/src/data.rs b/src/data.rs index d559daf..7f1fce8 100644 --- a/src/data.rs +++ b/src/data.rs @@ -64,6 +64,7 @@ pub fn new_quote(c: &Connection, date: &str, author: &str, content: &str) -> Res "#, &[&date, &author, &content], )?; + info!("New quote added by {}", author); Ok(()) } diff --git a/src/data/templates/add_post.hbs b/src/data/templates/add_post.hbs new file mode 100644 index 0000000..33dc481 --- /dev/null +++ b/src/data/templates/add_post.hbs @@ -0,0 +1,61 @@ + + + + + + Quotes fra #linux.no på freenode + + + + +
+                     _ _  
+                    | (_)_ __  _   ___  __  _ __   ___  
+              _|_|_ | | | '_ \| | | \ \/ / | '_ \ / _ \ 
+              _|_|_ | | | | | | |_| |)  ( _| | | | (_) | 
+               | |  |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/ 
+             -=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=- GAMMA -=-
+
+
+ +Takk ^___^ + +
+
+

linoquotes v.3 © 2004-2017 Kjetil Ørbekk, Erlend Hamberg, Vidar Holen, John H. Anthony. +

Source code at https://git.orbekk.com/linoquotes-gamma.git. +

The quotes on this page are copyright their respective owners and submitters.

+
+ + + + + + + diff --git a/src/error.rs b/src/error.rs index 649021e..3ccb73c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,7 @@ use iron::modifiers::Header; pub enum LinoError { DbError(rusqlite::Error), NotFound(String), + BadRequest(String), } pub type Result = std::result::Result; @@ -19,6 +20,7 @@ impl fmt::Display for LinoError { match *self { LinoError::DbError(ref err) => err.fmt(f), LinoError::NotFound(ref x) => write!(f, "Could not find {}", x), + LinoError::BadRequest(ref x) => write!(f, "Bad request: {}", x), } } } @@ -28,6 +30,7 @@ impl std::error::Error for LinoError { match *self { LinoError::DbError(ref err) => err.description(), LinoError::NotFound(_) => "not found", + LinoError::BadRequest(_) => "bad request", } } @@ -35,6 +38,7 @@ impl std::error::Error for LinoError { match *self { LinoError::DbError(ref err) => Some(err), LinoError::NotFound(_) => None, + LinoError::BadRequest(_) => None, } } } @@ -49,6 +53,7 @@ impl From for IronError { fn from(err: LinoError) -> IronError { let code = match err { LinoError::NotFound(_) => status::NotFound, + LinoError::BadRequest(_) => status::BadRequest, _ => status::InternalServerError, }; let description = format!("{:?}", err); diff --git a/src/server.rs b/src/server.rs index eec0e72..7c1b3db 100644 --- a/src/server.rs +++ b/src/server.rs @@ -40,6 +40,10 @@ fn make_renderer() -> HandlebarsEngine { "add".to_string(), include_str!("data/templates/add.hbs").to_string(), ); + templates.insert( + "add_post".to_string(), + include_str!("data/templates/add_post.hbs").to_string(), + ); e.add(Box::new(MemorySource(templates))); if let Err(r) = e.reload() { @@ -80,10 +84,32 @@ fn add_get(r: &mut Request) -> IronResult { } fn add_post(r: &mut Request) -> IronResult { + let nick = get_param(r, "nick")?; + let date = get_param(r, "date")?; + let quote = get_param(r, "quote")?; + + macro_rules! check { + ($i:ident) => ( + if $i.is_empty() { + return Err(From::from(LinoError::BadRequest( + format!("missing parameter: {}", stringify!($i))))); + } + ) + } + check!(nick); + check!(date); + check!(quote); + + { + let mu = r.get::>().unwrap(); + let state = mu.lock().unwrap(); + data::new_quote(&state.connection, &date, &nick, "e)?; + } + Ok(Response::with(( status::Ok, Header(ContentType::html()), - Template::new("add", Map::new()), + Template::new("add_post", Map::new()), ))) } -- cgit v1.2.3