Plume/plume-models/src/apps.rs

81 lines
2.0 KiB
Rust
Raw Normal View History

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