lemmy/server/src/lib.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate diesel;
extern crate dotenv;
2019-03-04 16:39:07 +00:00
use diesel::*;
use diesel::pg::PgConnection;
2019-03-04 16:39:07 +00:00
use diesel::result::Error;
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;
}
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))
}