Plume/src/api/posts.rs

26 lines
691 B
Rust
Raw Normal View History

2018-09-25 21:45:32 +02:00
use canapi::Provider;
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;
use plume_models::{
Connection,
db_conn::DbConn,
posts::Post,
};
2018-09-19 16:49:34 +02:00
#[get("/posts/<id>")]
fn get(id: i32, conn: DbConn) -> Json<serde_json::Value> {
let post = <Post as Provider<Connection>>::get(&*conn, id).ok();
2018-09-25 21:45:32 +02:00
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("api::list: invalid query error");
let post = <Post as Provider<Connection>>::list(&*conn, query);
2018-09-25 21:45:32 +02:00
Json(json!(post))
2018-09-19 16:49:34 +02:00
}