summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-10 10:31:23 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-10 10:31:23 -0400
commit15267c895e04f782aaae5fc7c24cd89d6db54f95 (patch)
tree779a4e13e26da4ace950249aadee1162640aa498
parent1cc4f17a742e0d0ef047842ac854e1d1ee8ec8b7 (diff)
add: Page to add new quotes.
-rw-r--r--src/data.rs1
-rw-r--r--src/data/templates/add_post.hbs61
-rw-r--r--src/error.rs5
-rw-r--r--src/server.rs28
4 files changed, 94 insertions, 1 deletions
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 @@
+<!doctype html>
+<html lang="no">
+ <head>
+ <meta name="robots" content="noindex, nofollow" />
+ <link rel="shortcut icon" href="/favicon.ico" />
+ <title>Quotes fra #linux.no på freenode</title>
+ <style type="text/css">
+ body {
+ font-family: monospace;
+ background: #ffffff;
+ color: #000000;
+ }
+ a, a:visited {
+ color: #000000;
+ }
+ hr {
+ border-style: solid;
+ border-color: black;
+ border-width: 1px;
+ }
+ .ragebutton {
+ font-family: monospace;
+ text-align: left;
+ text-decoration: underline;
+ color: black;
+ background: none;
+ margin: 0;
+ padding: 0;
+ border: none;
+ cursor: pointer;
+ -moz-user-select: text;
+ }
+ </style>
+ </head>
+ <body>
+
+<pre>
+ _ _
+ | (_)_ __ _ ___ __ _ __ ___
+ _|_|_ | | | '_ \| | | \ \/ / | '_ \ / _ \
+ _|_|_ | | | | | | |_| |) ( _| | | | (_) |
+ | | |_|_|_| |_|\__,_/_/\_(_)_| |_|\___/
+ -=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=- GAMMA -=-
+</pre>
+<hr>
+
+Takk ^___^
+
+<center>
+<br>
+<p>linoquotes v.3 © 2004-2017 Kjetil Ørbekk, Erlend Hamberg, Vidar Holen, John H. Anthony.
+<p>Source code at <a href="https://git.orbekk.com/linoquotes-gamma.git">https://git.orbekk.com/linoquotes-gamma.git</a>.
+<p>The quotes on this page are copyright their respective owners and submitters.</p>
+</center>
+
+ </body>
+</html>
+
+
+
+
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<T> = std::result::Result<T, LinoError>;
@@ -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<LinoError> 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<Response> {
}
fn add_post(r: &mut Request) -> IronResult<Response> {
+ 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::<Write<State>>().unwrap();
+ let state = mu.lock().unwrap();
+ data::new_quote(&state.connection, &date, &nick, &quote)?;
+ }
+
Ok(Response::with((
status::Ok,
Header(ContentType::html()),
- Template::new("add", Map::new()),
+ Template::new("add_post", Map::new()),
)))
}