Add WIP relays to database and api
This commit is contained in:
parent
4e3df272c3
commit
734f8b291c
14 changed files with 387 additions and 88 deletions
emgauwa-lib/src
|
@ -1,8 +1,9 @@
|
|||
use crate::db::Schedule;
|
||||
use crate::db::{Controller, Relay, Schedule};
|
||||
use futures::executor;
|
||||
use serde::Serialize;
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
use crate::db::types::ControllerUid;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ReturnSchedule {
|
||||
|
@ -12,22 +13,46 @@ pub struct ReturnSchedule {
|
|||
}
|
||||
|
||||
impl ReturnSchedule {
|
||||
pub fn load_tags(&mut self, conn: &mut PoolConnection<Sqlite>) {
|
||||
self.tags = executor::block_on(self.schedule.get_tags(conn)).unwrap();
|
||||
}
|
||||
}
|
||||
pub fn from_schedule(schedule: Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
|
||||
let schedule = schedule.clone();
|
||||
let tags = executor::block_on(schedule.get_tags(conn)).unwrap();
|
||||
|
||||
impl From<Schedule> for ReturnSchedule {
|
||||
fn from(schedule: Schedule) -> Self {
|
||||
ReturnSchedule {
|
||||
schedule,
|
||||
tags: vec![],
|
||||
tags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_schedule_ref(schedule: &Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
|
||||
Self::from_schedule(schedule.clone(), conn)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Schedule> for ReturnSchedule {
|
||||
fn from(schedule: &Schedule) -> Self {
|
||||
ReturnSchedule::from(schedule.clone())
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ReturnRelay {
|
||||
#[serde(flatten)]
|
||||
pub relay: Relay,
|
||||
pub controller: Controller,
|
||||
pub controller_id: ControllerUid,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
impl ReturnRelay {
|
||||
pub fn from_relay(relay: Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
|
||||
let relay = relay.clone();
|
||||
let controller = executor::block_on(Controller::get(conn, relay.controller_id)).unwrap();
|
||||
let controller_uid = controller.uid.clone();
|
||||
let tags = executor::block_on(relay.get_tags(conn)).unwrap();
|
||||
|
||||
ReturnRelay {
|
||||
relay,
|
||||
controller,
|
||||
controller_id: controller_uid,
|
||||
tags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_relay_ref(relay: &Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
|
||||
Self::from_relay(relay.clone(), conn)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue