Plume/src/api/posts.rs

38 lines
1012 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,
api_tokens::ApiToken,
db_conn::DbConn,
posts::Post,
};
2018-09-19 16:49:34 +02:00
#[get("/posts/<id>")]
fn get(id: i32, conn: DbConn, token: ApiToken) -> Json<serde_json::Value> {
if token.can_read("posts") {
let post = <Post as Provider<Connection>>::get(&*conn, id).ok();
Json(json!(post))
} else {
Json(json!({
"error": "Unauthorized"
}))
}
2018-09-25 21:45:32 +02:00
}
#[get("/posts")]
fn list(conn: DbConn, uri: &Origin, token: ApiToken) -> Json<serde_json::Value> {
if token.can_read("posts") {
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);
Json(json!(post))
} else {
Json(json!({
"error": "Unauthorized"
}))
}}