Plume/src/api/posts.rs

24 lines
700 B
Rust
Raw Normal View History

2018-09-25 21:45:32 +02:00
use canapi::Provider;
use diesel::PgConnection;
2018-09-29 16:45:27 +02:00
use rocket::http::uri::Origin;
2018-09-19 16:49:34 +02:00
use rocket_contrib::Json;
use serde_json;
2018-09-29 16:45:27 +02:00
use serde_qs;
2018-09-19 16:49:34 +02:00
2018-09-25 21:45:32 +02:00
use plume_api::posts::PostEndpoint;
2018-09-19 16:49:34 +02:00
use plume_models::db_conn::DbConn;
use plume_models::posts::Post;
#[get("/posts/<id>")]
fn get(id: i32, conn: DbConn) -> Json<serde_json::Value> {
2018-09-25 21:45:32 +02:00
let post = <Post as Provider<PgConnection>>::get(&*conn, id).ok();
Json(json!(post))
}
#[get("/posts")]
2018-09-29 16:45:27 +02:00
fn list(conn: DbConn, uri: &Origin) -> Json<serde_json::Value> {
let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("Invalid query string");
let post = <Post as Provider<PgConnection>>::list(&*conn, query);
2018-09-25 21:45:32 +02:00
Json(json!(post))
2018-09-19 16:49:34 +02:00
}