2018-10-21 18:22:27 +02:00
|
|
|
use canapi::{Error, Provider};
|
2018-10-21 15:19:07 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-10-21 18:22:27 +02:00
|
|
|
use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods};
|
2018-10-21 15:19:07 +02:00
|
|
|
|
2018-10-21 18:22:27 +02:00
|
|
|
use plume_api::apps::AppEndpoint;
|
2018-10-22 15:30:04 +02:00
|
|
|
use plume_common::utils::random_hex;
|
2018-10-21 18:22:27 +02:00
|
|
|
use Connection;
|
2018-10-21 15:19:07 +02:00
|
|
|
use schema::apps;
|
|
|
|
|
2018-10-21 18:22:27 +02:00
|
|
|
#[derive(Clone, Queryable)]
|
2018-10-21 15:19:07 +02:00
|
|
|
pub struct App {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
|
|
|
pub client_id: String,
|
|
|
|
pub client_secret: String,
|
|
|
|
pub redirect_uri: Option<String>,
|
2018-10-22 15:30:04 +02:00
|
|
|
pub website: Option<String>,
|
2018-10-21 15:19:07 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
|
|
|
}
|
|
|
|
|
2018-10-21 18:22:27 +02:00
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name= "apps"]
|
|
|
|
pub struct NewApp {
|
|
|
|
pub name: String,
|
|
|
|
pub client_id: String,
|
|
|
|
pub client_secret: String,
|
|
|
|
pub redirect_uri: Option<String>,
|
2018-10-22 15:30:04 +02:00
|
|
|
pub website: Option<String>,
|
2018-10-21 18:22:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Provider<Connection> for App {
|
|
|
|
type Data = AppEndpoint;
|
|
|
|
|
2018-10-24 13:21:42 +02:00
|
|
|
fn get(_conn: &Connection, _id: i32) -> Result<AppEndpoint, Error> {
|
2018-10-21 18:22:27 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:21:42 +02:00
|
|
|
fn list(_conn: &Connection, _query: AppEndpoint) -> Vec<AppEndpoint> {
|
2018-10-21 18:22:27 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(conn: &Connection, data: AppEndpoint) -> Result<AppEndpoint, Error> {
|
2018-10-22 15:30:04 +02:00
|
|
|
let client_id = random_hex();
|
|
|
|
|
|
|
|
let client_secret = random_hex();
|
2018-10-21 18:22:27 +02:00
|
|
|
let app = App::insert(conn, NewApp {
|
2018-10-23 11:43:14 +02:00
|
|
|
name: data.name,
|
2018-10-21 18:22:27 +02:00
|
|
|
client_id: client_id,
|
|
|
|
client_secret: client_secret,
|
|
|
|
redirect_uri: data.redirect_uri,
|
|
|
|
website: data.website,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(AppEndpoint {
|
|
|
|
id: Some(app.id),
|
2018-10-23 11:43:14 +02:00
|
|
|
name: app.name,
|
2018-10-21 18:22:27 +02:00
|
|
|
client_id: Some(app.client_id),
|
|
|
|
client_secret: Some(app.client_secret),
|
|
|
|
redirect_uri: app.redirect_uri,
|
|
|
|
website: app.website,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:21:42 +02:00
|
|
|
fn update(_conn: &Connection, _id: i32, _new_data: AppEndpoint) -> Result<AppEndpoint, Error> {
|
2018-10-21 18:22:27 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2018-10-22 15:30:04 +02:00
|
|
|
|
2018-10-24 13:21:42 +02:00
|
|
|
fn delete(_conn: &Connection, _id: i32) {
|
2018-10-21 18:22:27 +02:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:19:07 +02:00
|
|
|
impl App {
|
2018-10-21 18:22:27 +02:00
|
|
|
get!(apps);
|
2018-10-21 15:19:07 +02:00
|
|
|
insert!(apps, NewApp);
|
2018-10-22 15:30:04 +02:00
|
|
|
find_by!(apps, find_by_client_id, client_id as String);
|
|
|
|
}
|