Plume/src/api/posts.rs

29 lines
1.0 KiB
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;
use rocket_contrib::json::Json;
2018-09-19 16:49:34 +02:00
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,
search::Searcher as UnmanagedSearcher,
};
use api::authorization::*;
use Searcher;
2018-09-19 16:49:34 +02:00
#[get("/posts/<id>")]
pub fn get(id: i32, conn: DbConn, auth: Option<Authorization<Read, Post>>, search: Searcher) -> Json<serde_json::Value> {
let post = <Post as Provider<(&Connection, &UnmanagedSearcher, Option<i32>)>>::get(&(&*conn, &search, auth.map(|a| a.0.user_id)), id).ok();
Json(json!(post))
2018-09-25 21:45:32 +02:00
}
#[get("/posts")]
pub fn list(conn: DbConn, uri: &Origin, auth: Option<Authorization<Read, Post>>, search: Searcher) -> 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, &UnmanagedSearcher, Option<i32>)>>::list(&(&*conn, &search, auth.map(|a| a.0.user_id)), query);
Json(json!(post))
}