From f893056d6d9e9916995f334dd9a9ccf3dc093581 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 25 Sep 2018 20:45:32 +0100 Subject: [PATCH] Mount the API endpoints --- Cargo.lock | 2 ++ Cargo.toml | 4 ++++ src/api/posts.rs | 14 ++++++++++++-- src/main.rs | 5 ++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee275d22..ea75c73c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1459,6 +1459,7 @@ version = "0.2.0" dependencies = [ "activitypub 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "atom_syndication 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "canapi 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1468,6 +1469,7 @@ dependencies = [ "guid-create 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "multipart 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)", + "plume-api 0.1.0", "plume-common 0.2.0", "plume-models 0.2.0", "rocket 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=55459db7732b9a240826a5c120c650f87e3372ce)", diff --git a/Cargo.toml b/Cargo.toml index 78e9f70e..630e134f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ version = "0.2.0" [dependencies] activitypub = "0.1.3" atom_syndication = "0.6" +canapi = "0.1" colored = "1.6" dotenv = "0.13" failure = "0.1" @@ -30,6 +31,9 @@ version = "0.4" features = ["postgres", "r2d2", "chrono"] version = "*" +[dependencies.plume-api] +path = "plume-api" + [dependencies.plume-common] path = "plume-common" diff --git a/src/api/posts.rs b/src/api/posts.rs index 061e81d6..c193b09a 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -1,11 +1,21 @@ +use canapi::Provider; +use diesel::PgConnection; use rocket_contrib::Json; use serde_json; +use plume_api::posts::PostEndpoint; use plume_models::db_conn::DbConn; use plume_models::posts::Post; #[get("/posts/")] fn get(id: i32, conn: DbConn) -> Json { - let post = Post::get(&*conn, id).unwrap(); - Json(post.to_json(&*conn)) + let post = >::get(&*conn, id).ok(); + Json(json!(post)) +} + +// TODO: handle query params +#[get("/posts")] +fn list(conn: DbConn) -> Json { + let post = >::list(&*conn, PostEndpoint::default()); + Json(json!(post)) } diff --git a/src/main.rs b/src/main.rs index 2a499f2a..fe26e038 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate activitypub; extern crate atom_syndication; +extern crate canapi; extern crate chrono; extern crate colored; extern crate diesel; @@ -12,6 +13,7 @@ extern crate gettextrs; extern crate guid_create; extern crate heck; extern crate multipart; +extern crate plume_api; extern crate plume_common; extern crate plume_models; #[macro_use] @@ -144,7 +146,8 @@ fn main() { routes::errors::csrf_violation ]) .mount("/api/v1", routes![ - api::posts::get + api::posts::get, + api::posts::list ]) .catch(catchers![ routes::errors::not_found,