2019-02-28 06:02:55 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
extern crate dotenv;
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
use diesel::*;
|
2019-02-28 06:02:55 +00:00
|
|
|
use diesel::pg::PgConnection;
|
2019-03-04 16:39:07 +00:00
|
|
|
use diesel::result::Error;
|
2019-02-28 06:02:55 +00:00
|
|
|
use dotenv::dotenv;
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
pub mod schema;
|
|
|
|
pub mod models;
|
|
|
|
pub mod actions;
|
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
// pub trait Likeable;
|
|
|
|
pub trait Crud<T> {
|
|
|
|
fn create(conn: &PgConnection, form: T) -> Result<Self, Error> where Self: Sized;
|
|
|
|
fn read(conn: &PgConnection, id: i32) -> Self;
|
|
|
|
fn update(conn: &PgConnection, id: i32, form: T) -> Self;
|
|
|
|
fn delete(conn: &PgConnection, id: i32) -> usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Followable<T> {
|
|
|
|
fn follow(conn: &PgConnection, form: T) -> Result<Self, Error> where Self: Sized;
|
|
|
|
fn ignore(conn: &PgConnection, form: T) -> usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Joinable<T> {
|
|
|
|
fn join(conn: &PgConnection, form: T) -> Result<Self, Error> where Self: Sized;
|
|
|
|
fn leave(conn: &PgConnection, form: T) -> usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-28 06:02:55 +00:00
|
|
|
pub fn establish_connection() -> PgConnection {
|
|
|
|
dotenv().ok();
|
|
|
|
|
|
|
|
let database_url = env::var("DATABASE_URL")
|
|
|
|
.expect("DATABASE_URL must be set");
|
|
|
|
PgConnection::establish(&database_url)
|
|
|
|
.expect(&format!("Error connecting to {}", database_url))
|
|
|
|
}
|
|
|
|
|