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
emgauwa-lib/src/db

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,