Add ControllerWsAction

This commit is contained in:
Tobias Reisinger 2023-11-27 17:21:40 +01:00
parent cb47dcda5c
commit 3b596de06f
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
10 changed files with 134 additions and 34 deletions

View file

@ -1,4 +1,4 @@
use serde_derive::Serialize;
use serde_derive::{Deserialize, Serialize};
use std::ops::DerefMut;
use sqlx::pool::PoolConnection;
@ -8,9 +8,11 @@ use crate::db::errors::DatabaseError;
use crate::db::DbTag;
use crate::types::ControllerUid;
#[derive(Debug, Serialize, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DbController {
#[serde(skip)]
pub id: i64,
#[serde(rename = "id")]
pub uid: ControllerUid,
pub name: String,
pub relay_count: i64,
@ -51,6 +53,19 @@ impl DbController {
.map_err(DatabaseError::from)
}
pub async fn get_by_uid_or_create(
conn: &mut PoolConnection<Sqlite>,
uid: &ControllerUid,
new_name: &str,
new_relay_count: i64,
new_active: bool,
) -> Result<DbController, DatabaseError> {
match DbController::get_by_uid(conn, uid).await? {
Some(tag) => Ok(tag),
None => DbController::create(conn, uid, new_name, new_relay_count, new_active).await,
}
}
pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>,
tag: &DbTag,

View file

@ -1,4 +1,4 @@
use serde_derive::Serialize;
use serde_derive::{Deserialize, Serialize};
use std::ops::DerefMut;
use crate::db::DbController;
@ -8,7 +8,7 @@ use sqlx::Sqlite;
use crate::db::errors::DatabaseError;
use crate::db::DbTag;
#[derive(Debug, Serialize, Clone, sqlx::FromRow)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DbRelay {
#[serde(skip)]
pub id: i64,
@ -51,6 +51,18 @@ impl DbRelay {
.await?)
}
pub async fn get_by_controller_and_num_or_create(
conn: &mut PoolConnection<Sqlite>,
controller: &DbController,
number: i64,
new_name: &str,
) -> Result<DbRelay, DatabaseError> {
match DbRelay::get_by_controller_and_num(conn, controller, number).await? {
Some(relay) => Ok(relay),
None => DbRelay::create(conn, new_name, number, controller).await,
}
}
pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>,
tag: &DbTag,

View file

@ -10,11 +10,11 @@ use crate::db::model_utils::Period;
use crate::db::DbTag;
use crate::types::ScheduleUid;
#[derive(Debug, Serialize, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DbSchedule {
#[serde(skip)]
pub id: i64,
#[serde(rename(serialize = "id"))]
#[serde(rename = "id")]
pub uid: ScheduleUid,
pub name: String,
pub periods: DbPeriods,