48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
use diesel::prelude::*;
|
|
use dotenvy::dotenv;
|
|
use std::env;
|
|
|
|
pub mod models;
|
|
pub mod schema;
|
|
|
|
pub fn establish_connection() -> SqliteConnection {
|
|
dotenv().ok();
|
|
|
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
|
SqliteConnection::establish(&database_url)
|
|
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
|
}
|
|
|
|
pub fn create_app(
|
|
conn: &mut SqliteConnection,
|
|
name: &str,
|
|
address: &str,
|
|
description: Option<&str>,
|
|
ssh_address: Option<&str>,
|
|
) -> models::App {
|
|
use crate::db::schema::app;
|
|
|
|
let new_app = models::App {
|
|
id: uuid::Uuid::new_v4().to_string(),
|
|
name: name.to_string(),
|
|
description: description.map(|d| d.to_string()),
|
|
address: address.to_string(),
|
|
ssh_address: ssh_address.map(|s| s.to_string()),
|
|
};
|
|
|
|
diesel::insert_into(app::table)
|
|
.values(&new_app)
|
|
.execute(conn)
|
|
.expect("Error saving new app");
|
|
|
|
new_app
|
|
}
|
|
|
|
pub fn get_apps(conn: &mut SqliteConnection) -> Vec<models::App> {
|
|
use crate::db::schema::app;
|
|
|
|
app::table
|
|
.load::<models::App>(conn)
|
|
.expect("Error loading apps")
|
|
}
|