Add tags for schedules
This commit is contained in:
parent
f3f3d36eed
commit
75f8afd624
10 changed files with 375 additions and 184 deletions
src
70
src/db.rs
70
src/db.rs
|
@ -1,18 +1,15 @@
|
|||
use std::env;
|
||||
|
||||
use diesel::dsl::sql;
|
||||
use diesel::prelude::*;
|
||||
use diesel_migrations::embed_migrations;
|
||||
use dotenv::dotenv;
|
||||
|
||||
use crate::types::EmgauwaUid;
|
||||
use errors::DatabaseError;
|
||||
use models::*;
|
||||
use schema::schedules::dsl::*;
|
||||
|
||||
pub mod errors;
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
pub mod schedule;
|
||||
pub mod tag;
|
||||
|
||||
mod model_utils;
|
||||
|
||||
embed_migrations!("migrations");
|
||||
|
@ -28,63 +25,4 @@ fn get_connection() -> SqliteConnection {
|
|||
pub fn run_migrations() {
|
||||
let connection = get_connection();
|
||||
embedded_migrations::run(&connection).expect("Failed to run migrations.");
|
||||
}
|
||||
|
||||
pub fn get_schedules() -> Vec<Schedule> {
|
||||
let connection = get_connection();
|
||||
schedules
|
||||
.load::<Schedule>(&connection)
|
||||
.expect("Error loading schedules")
|
||||
}
|
||||
|
||||
pub fn get_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
let result = schedules
|
||||
.filter(uid.eq(filter_uid))
|
||||
.first::<Schedule>(&connection)
|
||||
.or(Err(DatabaseError::NotFound))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn delete_schedule_by_uid(filter_uid: EmgauwaUid) -> Result<(), DatabaseError> {
|
||||
let filter_uid = match filter_uid {
|
||||
EmgauwaUid::Off => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::On => Err(DatabaseError::Protected),
|
||||
EmgauwaUid::Any(_) => Ok(filter_uid)
|
||||
}?;
|
||||
|
||||
let connection = get_connection();
|
||||
match diesel::delete(schedules.filter(uid.eq(filter_uid))).execute(&connection) {
|
||||
Ok(rows) => {
|
||||
if rows != 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DatabaseError::DeleteError)
|
||||
}
|
||||
}
|
||||
Err(_) => Err(DatabaseError::DeleteError),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_schedule(new_name: &str, new_periods: &Periods) -> Result<Schedule, DatabaseError> {
|
||||
let connection = get_connection();
|
||||
|
||||
let new_schedule = NewSchedule {
|
||||
uid: &EmgauwaUid::default(),
|
||||
name: new_name,
|
||||
periods: new_periods,
|
||||
};
|
||||
|
||||
diesel::insert_into(schedules)
|
||||
.values(&new_schedule)
|
||||
.execute(&connection)
|
||||
.or(Err(DatabaseError::InsertError))?;
|
||||
|
||||
let result = schedules
|
||||
.find(sql("last_insert_rowid()"))
|
||||
.get_result::<Schedule>(&connection)
|
||||
.or(Err(DatabaseError::InsertGetError))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue