Merge pull request #245 from Plume-org/rest-api
Some API endpoints for articles
This commit is contained in:
@@ -7,6 +7,7 @@ authors = ["Baptiste Gelez <baptiste@gelez.xyz>"]
|
||||
activitypub = "0.1.1"
|
||||
ammonia = "1.2.0"
|
||||
bcrypt = "0.2"
|
||||
canapi = "0.1"
|
||||
heck = "0.3.0"
|
||||
lazy_static = "*"
|
||||
openssl = "0.10.11"
|
||||
@@ -25,6 +26,9 @@ version = "0.4"
|
||||
features = ["postgres", "r2d2", "chrono"]
|
||||
version = "1.3.2"
|
||||
|
||||
[dependencies.plume-api]
|
||||
path = "../plume-api"
|
||||
|
||||
[dependencies.plume-common]
|
||||
path = "../plume-common"
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
extern crate activitypub;
|
||||
extern crate ammonia;
|
||||
extern crate bcrypt;
|
||||
extern crate canapi;
|
||||
extern crate chrono;
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
@@ -10,6 +11,7 @@ extern crate heck;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate openssl;
|
||||
extern crate plume_api;
|
||||
extern crate plume_common;
|
||||
extern crate reqwest;
|
||||
extern crate rocket;
|
||||
|
||||
@@ -3,11 +3,13 @@ use activitypub::{
|
||||
link,
|
||||
object::{Article, Tombstone}
|
||||
};
|
||||
use canapi::{Error, Provider};
|
||||
use chrono::{NaiveDateTime, TimeZone, Utc};
|
||||
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any};
|
||||
use heck::KebabCase;
|
||||
use serde_json;
|
||||
|
||||
use plume_api::posts::PostEndpoint;
|
||||
use plume_common::activity_pub::{
|
||||
Hashtag, Source,
|
||||
PUBLIC_VISIBILTY, Id, IntoId,
|
||||
@@ -55,6 +57,54 @@ pub struct NewPost {
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
impl Provider<PgConnection> for Post {
|
||||
type Data = PostEndpoint;
|
||||
|
||||
fn get(conn: &PgConnection, id: i32) -> Result<PostEndpoint, Error> {
|
||||
Post::get(conn, id).map(|p| Ok(PostEndpoint {
|
||||
id: Some(p.id),
|
||||
title: Some(p.title.clone()),
|
||||
subtitle: Some(p.subtitle.clone()),
|
||||
content: Some(p.content.get().clone())
|
||||
})).unwrap_or(Err(Error::NotFound("Get Post".to_string())))
|
||||
}
|
||||
|
||||
fn list(conn: &PgConnection, filter: PostEndpoint) -> Vec<PostEndpoint> {
|
||||
let mut query = posts::table.into_boxed();
|
||||
if let Some(title) = filter.title {
|
||||
query = query.filter(posts::title.eq(title));
|
||||
}
|
||||
if let Some(subtitle) = filter.subtitle {
|
||||
query = query.filter(posts::subtitle.eq(subtitle));
|
||||
}
|
||||
if let Some(content) = filter.content {
|
||||
query = query.filter(posts::content.eq(content));
|
||||
}
|
||||
|
||||
query.get_results::<Post>(conn).map(|ps| ps.into_iter()
|
||||
.map(|p| PostEndpoint {
|
||||
id: Some(p.id),
|
||||
title: Some(p.title.clone()),
|
||||
subtitle: Some(p.subtitle.clone()),
|
||||
content: Some(p.content.get().clone())
|
||||
})
|
||||
.collect()
|
||||
).unwrap_or(vec![])
|
||||
}
|
||||
|
||||
fn create(_conn: &PgConnection, _query: PostEndpoint) -> Result<PostEndpoint, Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn update(_conn: &PgConnection, _id: i32, _new_data: PostEndpoint) -> Result<PostEndpoint, Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn delete(conn: &PgConnection, id: i32) {
|
||||
Post::get(conn, id).map(|p| p.delete(conn));
|
||||
}
|
||||
}
|
||||
|
||||
impl Post {
|
||||
insert!(posts, NewPost);
|
||||
get!(posts);
|
||||
|
||||
Reference in New Issue
Block a user