Make the REST API compatible with SQlite

This commit is contained in:
Bat 2018-09-29 18:14:48 +01:00
parent bd80430b1b
commit 07f2c979ec
2 changed files with 13 additions and 11 deletions

View File

@ -57,10 +57,10 @@ pub struct NewPost {
pub source: String, pub source: String,
} }
impl Provider<PgConnection> for Post { impl Provider<Connection> for Post {
type Data = PostEndpoint; type Data = PostEndpoint;
fn get(conn: &PgConnection, id: i32) -> Result<PostEndpoint, Error> { fn get(conn: &Connection, id: i32) -> Result<PostEndpoint, Error> {
Post::get(conn, id).map(|p| Ok(PostEndpoint { Post::get(conn, id).map(|p| Ok(PostEndpoint {
id: Some(p.id), id: Some(p.id),
title: Some(p.title.clone()), title: Some(p.title.clone()),
@ -69,7 +69,7 @@ impl Provider<PgConnection> for Post {
})).unwrap_or(Err(Error::NotFound("Get Post".to_string()))) })).unwrap_or(Err(Error::NotFound("Get Post".to_string())))
} }
fn list(conn: &PgConnection, filter: PostEndpoint) -> Vec<PostEndpoint> { fn list(conn: &Connection, filter: PostEndpoint) -> Vec<PostEndpoint> {
let mut query = posts::table.into_boxed(); let mut query = posts::table.into_boxed();
if let Some(title) = filter.title { if let Some(title) = filter.title {
query = query.filter(posts::title.eq(title)); query = query.filter(posts::title.eq(title));
@ -92,15 +92,15 @@ impl Provider<PgConnection> for Post {
).unwrap_or(vec![]) ).unwrap_or(vec![])
} }
fn create(_conn: &PgConnection, _query: PostEndpoint) -> Result<PostEndpoint, Error> { fn create(_conn: &Connection, _query: PostEndpoint) -> Result<PostEndpoint, Error> {
unimplemented!() unimplemented!()
} }
fn update(_conn: &PgConnection, _id: i32, _new_data: PostEndpoint) -> Result<PostEndpoint, Error> { fn update(_conn: &Connection, _id: i32, _new_data: PostEndpoint) -> Result<PostEndpoint, Error> {
unimplemented!() unimplemented!()
} }
fn delete(conn: &PgConnection, id: i32) { fn delete(conn: &Connection, id: i32) {
Post::get(conn, id).map(|p| p.delete(conn)); Post::get(conn, id).map(|p| p.delete(conn));
} }
} }

View File

@ -1,23 +1,25 @@
use canapi::Provider; use canapi::Provider;
use diesel::PgConnection;
use rocket::http::uri::Origin; use rocket::http::uri::Origin;
use rocket_contrib::Json; use rocket_contrib::Json;
use serde_json; use serde_json;
use serde_qs; use serde_qs;
use plume_api::posts::PostEndpoint; use plume_api::posts::PostEndpoint;
use plume_models::db_conn::DbConn; use plume_models::{
use plume_models::posts::Post; Connection,
db_conn::DbConn,
posts::Post,
};
#[get("/posts/<id>")] #[get("/posts/<id>")]
fn get(id: i32, conn: DbConn) -> Json<serde_json::Value> { fn get(id: i32, conn: DbConn) -> Json<serde_json::Value> {
let post = <Post as Provider<PgConnection>>::get(&*conn, id).ok(); let post = <Post as Provider<Connection>>::get(&*conn, id).ok();
Json(json!(post)) Json(json!(post))
} }
#[get("/posts")] #[get("/posts")]
fn list(conn: DbConn, uri: &Origin) -> Json<serde_json::Value> { fn list(conn: DbConn, uri: &Origin) -> Json<serde_json::Value> {
let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("Invalid query string"); let query: PostEndpoint = serde_qs::from_str(uri.query().unwrap_or("")).expect("Invalid query string");
let post = <Post as Provider<PgConnection>>::list(&*conn, query); let post = <Post as Provider<Connection>>::list(&*conn, query);
Json(json!(post)) Json(json!(post))
} }