Skip to content
Permalink
Browse files
show all todos works
woo
  • Loading branch information
steveklabnik committed Oct 23, 2014
1 parent 0155982 commit df60ad6c3fd9d763ccee4a2082623018a7a8d336
Showing 1 changed file with 22 additions and 1 deletion.
@@ -1,9 +1,14 @@
extern crate nickel;
extern crate postgres;
extern crate serialize;

use std::io::net::ip::Ipv4Addr;
use nickel::{Nickel, Request, Response, HttpRouter, StaticFilesHandler};

use postgres::{PostgresConnection, NoSsl};

use serialize::json;

#[deriving(Decodable, Encodable)]
struct Todo {
title: String,
@@ -21,5 +26,21 @@ fn main() {
}

fn todos_handler (request: &Request, response: &mut Response) {
response.send("{\"todos\":[]}");
let conn = PostgresConnection::connect("postgres://rustmvc@localhost",
&NoSsl).unwrap();

let stmt = conn.prepare("SELECT title, is_completed FROM todos")
.unwrap();
let results = stmt.query([]).unwrap().map(|row| {
Todo {
title: row.get(0u),
is_completed: row.get(1u),
}
}).collect::<Vec<Todo>>();

let results = json::encode(&results);

response
.content_type("json")
.send(format!("{{\"todos\":{}}}", results));
}

0 comments on commit df60ad6

Please sign in to comment.