Add minor fixes
This commit is contained in:
parent
80d1fa32a0
commit
ee5d4e2126
6 changed files with 7 additions and 7 deletions
1
.env
1
.env
|
|
@ -1 +1,2 @@
|
|||
# This is usually used in development by sqlx. It is not used in production
|
||||
DATABASE_URL=sqlite://emgauwa-core.sqlite
|
||||
|
|
|
|||
3
.gitattributes
vendored
Normal file
3
.gitattributes
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
* text=auto
|
||||
|
||||
Cargo.lock -diff
|
||||
|
|
@ -4,11 +4,6 @@ version = "0.1.0"
|
|||
edition = "2018"
|
||||
authors = ["Tobias Reisinger <tobias@msrg.cc>"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
#[profile.release]
|
||||
#panic = 'abort'
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.4"
|
||||
|
||||
|
|
|
|||
78
src/db/mod.rs
Normal file
78
src/db/mod.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
use log::{info, trace};
|
||||
use sqlx::migrate::Migrator;
|
||||
use sqlx::sqlite::SqlitePoolOptions;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::model_utils::Period;
|
||||
use crate::db::schedules::{Periods, Schedule};
|
||||
use crate::types::EmgauwaUid;
|
||||
|
||||
pub mod errors;
|
||||
pub mod models;
|
||||
pub mod schedules;
|
||||
pub mod tag;
|
||||
|
||||
mod model_utils;
|
||||
|
||||
static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations"
|
||||
|
||||
pub async fn run_migrations(pool: &Pool<Sqlite>) {
|
||||
info!("Running migrations");
|
||||
MIGRATOR.run(pool).await.expect("Failed to run migrations.");
|
||||
}
|
||||
|
||||
async fn init_schedule(
|
||||
pool: &Pool<Sqlite>,
|
||||
uid: &EmgauwaUid,
|
||||
name: &str,
|
||||
periods: Periods,
|
||||
) -> Result<(), DatabaseError> {
|
||||
trace!("Initializing schedule {:?}", name);
|
||||
match Schedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => match err {
|
||||
DatabaseError::NotFound => {
|
||||
trace!("Schedule {:?} not found, inserting", name);
|
||||
sqlx::query_as!(
|
||||
Schedule,
|
||||
"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
|
||||
uid,
|
||||
name,
|
||||
periods,
|
||||
)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
.map(|_| ())
|
||||
}
|
||||
_ => Err(err),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn init(db: &str) -> Pool<Sqlite> {
|
||||
let pool: Pool<Sqlite> = SqlitePoolOptions::new()
|
||||
.acquire_timeout(std::time::Duration::from_secs(1))
|
||||
.max_connections(5)
|
||||
.connect(db)
|
||||
.await
|
||||
.expect("Error connecting to database.");
|
||||
|
||||
run_migrations(&pool).await;
|
||||
|
||||
init_schedule(&pool, &EmgauwaUid::Off, "Off", Periods(vec![]))
|
||||
.await
|
||||
.expect("Error initializing schedule Off");
|
||||
|
||||
init_schedule(
|
||||
&pool,
|
||||
&EmgauwaUid::On,
|
||||
"On",
|
||||
Periods(vec![Period::new_on()]),
|
||||
)
|
||||
.await
|
||||
.expect("Error initializing schedule On");
|
||||
|
||||
pool
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ pub enum ApiError {
|
|||
BadUid,
|
||||
ProtectedSchedule,
|
||||
DatabaseError(DatabaseError),
|
||||
InternalError(String),
|
||||
}
|
||||
|
||||
impl ApiError {
|
||||
|
|
@ -18,6 +19,7 @@ impl ApiError {
|
|||
ApiError::BadUid => StatusCode::BAD_REQUEST,
|
||||
ApiError::ProtectedSchedule => StatusCode::FORBIDDEN,
|
||||
ApiError::DatabaseError(db_error) => db_error.get_code(),
|
||||
ApiError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,6 +42,7 @@ impl From<&ApiError> for String {
|
|||
ApiError::BadUid => String::from("the uid is in a bad format"),
|
||||
ApiError::ProtectedSchedule => String::from("the targeted schedule is protected"),
|
||||
ApiError::DatabaseError(db_err) => String::from(db_err),
|
||||
ApiError::InternalError(msg) => msg.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
extern crate dotenv;
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
use actix_web::middleware::TrailingSlash;
|
||||
|
|
|
|||
Loading…
Reference in a new issue