Improve controller endpoint and model
This commit is contained in:
parent
2f51ebf91e
commit
6400b7745c
5 changed files with 103 additions and 7 deletions
emgauwa-lib/src/models
|
@ -4,8 +4,9 @@ use sqlx::pool::PoolConnection;
|
|||
use sqlx::Sqlite;
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::{DbController, DbRelay, DbSchedule};
|
||||
use crate::types::ControllerUid;
|
||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
|
||||
use crate::types::{ControllerUid, Weekday};
|
||||
use crate::utils;
|
||||
|
||||
pub trait FromDbModel {
|
||||
type DbModel: Clone;
|
||||
|
@ -40,6 +41,8 @@ pub struct Relay {
|
|||
pub r: DbRelay,
|
||||
pub controller: DbController,
|
||||
pub controller_id: ControllerUid,
|
||||
pub schedules: Vec<DbSchedule>,
|
||||
pub active_schedule: DbSchedule,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
|
@ -96,10 +99,23 @@ impl FromDbModel for Relay {
|
|||
let tags = executor::block_on(db_model.get_tags(conn))?;
|
||||
let controller_id = cache.uid.clone();
|
||||
|
||||
let schedules =
|
||||
executor::block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
|
||||
|
||||
let weekday = utils::get_weekday();
|
||||
let active_schedule = executor::block_on(DbJunctionRelaySchedule::get_schedule(
|
||||
conn,
|
||||
&db_model,
|
||||
weekday as Weekday,
|
||||
))?
|
||||
.ok_or(DatabaseError::NotFound)?;
|
||||
|
||||
Ok(Relay {
|
||||
r: db_model,
|
||||
controller: cache,
|
||||
controller_id,
|
||||
schedules,
|
||||
active_schedule,
|
||||
tags,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue