From 07f2c979ec14ed277e3d9ccd13e2e9bb139a7db9 Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 29 Sep 2018 18:14:48 +0100 Subject: [PATCH] Make the REST API compatible with SQlite --- plume-models/src/posts.rs | 12 ++++++------ src/api/posts.rs | 12 +++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index b0d164c8..727db413 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -57,10 +57,10 @@ pub struct NewPost { pub source: String, } -impl Provider for Post { +impl Provider for Post { type Data = PostEndpoint; - fn get(conn: &PgConnection, id: i32) -> Result { + fn get(conn: &Connection, id: i32) -> Result { Post::get(conn, id).map(|p| Ok(PostEndpoint { id: Some(p.id), title: Some(p.title.clone()), @@ -69,7 +69,7 @@ impl Provider for Post { })).unwrap_or(Err(Error::NotFound("Get Post".to_string()))) } - fn list(conn: &PgConnection, filter: PostEndpoint) -> Vec { + fn list(conn: &Connection, filter: PostEndpoint) -> Vec { let mut query = posts::table.into_boxed(); if let Some(title) = filter.title { query = query.filter(posts::title.eq(title)); @@ -92,15 +92,15 @@ impl Provider for Post { ).unwrap_or(vec![]) } - fn create(_conn: &PgConnection, _query: PostEndpoint) -> Result { + fn create(_conn: &Connection, _query: PostEndpoint) -> Result { unimplemented!() } - fn update(_conn: &PgConnection, _id: i32, _new_data: PostEndpoint) -> Result { + fn update(_conn: &Connection, _id: i32, _new_data: PostEndpoint) -> Result { unimplemented!() } - fn delete(conn: &PgConnection, id: i32) { + fn delete(conn: &Connection, id: i32) { Post::get(conn, id).map(|p| p.delete(conn)); } } diff --git a/src/api/posts.rs b/src/api/posts.rs index bfd96dfd..f29c402a 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -1,23 +1,25 @@ use canapi::Provider; -use diesel::PgConnection; use rocket::http::uri::Origin; use rocket_contrib::Json; use serde_json; use serde_qs; use plume_api::posts::PostEndpoint; -use plume_models::db_conn::DbConn; -use plume_models::posts::Post; +use plume_models::{ + Connection, + db_conn::DbConn, + posts::Post, +}; #[get("/posts/")] fn get(id: i32, conn: DbConn) -> Json { - let post = >::get(&*conn, id).ok(); + let post = >::get(&*conn, id).ok(); Json(json!(post)) } #[get("/posts")] fn list(conn: DbConn, uri: &Origin) -> Json { let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("Invalid query string"); - let post = >::list(&*conn, query); + let post = >::list(&*conn, query); Json(json!(post)) }