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;
|
2018-09-29 19:14:48 +02:00
|
|
|
use plume_models::{
|
|
|
|
Connection,
|
|
|
|
db_conn::DbConn,
|
|
|
|
posts::Post,
|
2018-12-02 17:37:51 +01:00
|
|
|
search::Searcher as UnmanagedSearcher,
|
2018-09-29 19:14:48 +02:00
|
|
|
};
|
2018-10-23 11:37:24 +02:00
|
|
|
use api::authorization::*;
|
2018-12-02 17:37:51 +01:00
|
|
|
use Searcher;
|
2018-09-19 16:49:34 +02:00
|
|
|
|
|
|
|
#[get("/posts/<id>")]
|
2018-12-02 17:37:51 +01:00
|
|
|
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();
|
2018-10-23 11:37:24 +02:00
|
|
|
Json(json!(post))
|
2018-09-25 21:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/posts")]
|
2018-12-02 17:37:51 +01:00
|
|
|
fn list(conn: DbConn, uri: &Origin, auth: Option<Authorization<Read, Post>>, search: Searcher) -> Json<serde_json::Value> {
|
2018-10-23 11:37:24 +02:00
|
|
|
let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("api::list: invalid query error");
|
2018-12-02 17:37:51 +01:00
|
|
|
let post = <Post as Provider<(&Connection, &UnmanagedSearcher, Option<i32>)>>::list(&(&*conn, &search, auth.map(|a| a.0.user_id)), query);
|
2018-10-23 11:37:24 +02:00
|
|
|
Json(json!(post))
|
|
|
|
}
|