Refactor models names

This commit is contained in:
Tobias Reisinger 2023-11-27 12:49:40 +01:00
parent 76b14ce75b
commit be7f31906c
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
24 changed files with 461 additions and 340 deletions
emgauwa-lib/src/db

View file

@ -6,20 +6,19 @@ use std::str::FromStr;
use crate::db::errors::DatabaseError;
use crate::db::model_utils::Period;
use crate::db::types::ScheduleUid;
use crate::types::ScheduleUid;
mod controllers;
pub mod errors;
mod model_utils;
mod relays;
mod schedules;
mod tag;
pub mod types;
mod controllers;
mod relays;
pub use controllers::Controller;
pub use relays::Relay;
pub use schedules::{Periods, Schedule};
pub use tag::Tag;
pub use controllers::DbController;
pub use relays::DbRelay;
pub use schedules::{DbPeriods, DbSchedule};
pub use tag::DbTag;
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
@ -32,16 +31,16 @@ async fn init_schedule(
pool: &Pool<Sqlite>,
uid: &ScheduleUid,
name: &str,
periods: Periods,
periods: DbPeriods,
) -> Result<(), DatabaseError> {
trace!("Initializing schedule {:?}", name);
match Schedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await {
match DbSchedule::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,
DbSchedule,
"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
uid,
name,
@ -71,7 +70,7 @@ pub async fn init(db: &str) -> Pool<Sqlite> {
run_migrations(&pool).await;
init_schedule(&pool, &ScheduleUid::Off, "Off", Periods(vec![]))
init_schedule(&pool, &ScheduleUid::Off, "Off", DbPeriods(vec![]))
.await
.expect("Error initializing schedule Off");
@ -79,7 +78,7 @@ pub async fn init(db: &str) -> Pool<Sqlite> {
&pool,
&ScheduleUid::On,
"On",
Periods(vec![Period::new_on()]),
DbPeriods(vec![Period::new_on()]),
)
.await
.expect("Error initializing schedule On");