lemmy/server/src/lib.rs
2019-03-04 08:39:07 -08:00

42 lines
1.1 KiB
Rust

#[macro_use]
extern crate diesel;
extern crate dotenv;
use diesel::*;
use diesel::pg::PgConnection;
use diesel::result::Error;
use dotenv::dotenv;
use std::env;
pub mod schema;
pub mod models;
pub mod actions;
// 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;
}
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))
}