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/db
|
@ -1,12 +1,11 @@
|
|||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_derive::Serialize;
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::model_utils::Period;
|
||||
use crate::db::tag::Tag;
|
||||
use crate::db::Tag;
|
||||
use crate::db::types::ControllerUid;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
|
@ -18,9 +17,6 @@ pub struct Controller {
|
|||
pub active: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
||||
pub struct Periods(pub Vec<Period>);
|
||||
|
||||
impl Controller {
|
||||
pub async fn get_all(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
|
@ -117,6 +113,6 @@ impl Controller {
|
|||
.execute(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
Controller::get_by_uid(conn, &self.uid).await
|
||||
Self::get(conn, self.id).await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,14 +10,16 @@ use crate::db::types::ScheduleUid;
|
|||
|
||||
pub mod errors;
|
||||
mod model_utils;
|
||||
mod models;
|
||||
mod schedules;
|
||||
pub mod tag;
|
||||
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;
|
||||
|
||||
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
|
||||
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Relay {
|
||||
#[serde(skip)]
|
||||
pub id: i64,
|
||||
// TODO
|
||||
}
|
130
emgauwa-lib/src/db/relays.rs
Normal file
130
emgauwa-lib/src/db/relays.rs
Normal file
|
@ -0,0 +1,130 @@
|
|||
use serde_derive::Serialize;
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use sqlx::pool::PoolConnection;
|
||||
use sqlx::Sqlite;
|
||||
use crate::db::Controller;
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::Tag;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct Relay {
|
||||
#[serde(skip)]
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub number: i64,
|
||||
#[serde(skip)]
|
||||
pub controller_id: i64,
|
||||
}
|
||||
|
||||
impl Relay {
|
||||
pub async fn get_all(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
) -> Result<Vec<Relay>, DatabaseError> {
|
||||
Ok(sqlx::query_as!(Relay, "SELECT * FROM relays")
|
||||
.fetch_all(conn.deref_mut())
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn get(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
id: i64,
|
||||
) -> Result<Relay, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
Relay,
|
||||
"SELECT * FROM relays WHERE id = ?",
|
||||
id
|
||||
)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await
|
||||
.map(|s| s.ok_or(DatabaseError::NotFound))?
|
||||
}
|
||||
|
||||
pub async fn get_by_tag(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
tag: &Tag,
|
||||
) -> Result<Vec<Relay>, DatabaseError> {
|
||||
Ok(sqlx::query_as!(Relay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
|
||||
.fetch_all(conn.deref_mut())
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn create(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
new_name: &str,
|
||||
new_number: i64,
|
||||
new_controller: &Controller,
|
||||
) -> Result<Relay, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
Relay,
|
||||
"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?) RETURNING *",
|
||||
new_name,
|
||||
new_number,
|
||||
new_controller.id,
|
||||
)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
sqlx::query!("DELETE FROM relays WHERE id = ?", self.id)
|
||||
.execute(conn.deref_mut())
|
||||
.await
|
||||
.map(|res| match res.rows_affected() {
|
||||
0 => Err(DatabaseError::DeleteError),
|
||||
_ => Ok(()),
|
||||
})?
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
new_name: &str,
|
||||
new_number: i64,
|
||||
new_controller: &Controller,
|
||||
) -> Result<Relay, DatabaseError> {
|
||||
sqlx::query!(
|
||||
"UPDATE relays SET name = ?, number = ?, controller_id = ? WHERE id = ?",
|
||||
new_name,
|
||||
new_number,
|
||||
new_controller.id,
|
||||
self.id,
|
||||
)
|
||||
.execute(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
Relay::get(conn, self.id).await
|
||||
}
|
||||
|
||||
pub async fn get_controller(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Controller, DatabaseError> {
|
||||
Controller::get(conn, self.controller_id).await
|
||||
}
|
||||
|
||||
pub async fn get_tags(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Vec<String>, DatabaseError> {
|
||||
Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
|
||||
.fetch_all(conn.deref_mut())
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn set_tags(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
new_tags: &[String],
|
||||
) -> Result<(), DatabaseError> {
|
||||
sqlx::query!("DELETE FROM junction_tag WHERE relay_id = ?", self.id)
|
||||
.execute(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
for new_tag in new_tags {
|
||||
let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
|
||||
tag.link_relay(conn, self).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use sqlx::Sqlite;
|
|||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::model_utils::Period;
|
||||
use crate::db::tag::Tag;
|
||||
use crate::db::Tag;
|
||||
use crate::db::types::ScheduleUid;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
|
@ -34,7 +34,7 @@ impl Schedule {
|
|||
|
||||
pub async fn get(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
id: &i64,
|
||||
id: i64,
|
||||
) -> Result<Schedule, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
Schedule,
|
||||
|
@ -127,7 +127,7 @@ impl Schedule {
|
|||
.execute(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
Schedule::get_by_uid(conn, &self.uid).await
|
||||
Schedule::get(conn, self.id).await
|
||||
}
|
||||
|
||||
pub async fn get_tags(
|
||||
|
@ -149,16 +149,7 @@ impl Schedule {
|
|||
.await?;
|
||||
|
||||
for new_tag in new_tags {
|
||||
let tag: Option<Tag> =
|
||||
sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", new_tag)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await?;
|
||||
|
||||
let tag = match tag {
|
||||
Some(id) => id,
|
||||
None => Tag::create(conn, new_tag).await?,
|
||||
};
|
||||
|
||||
let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
|
||||
tag.link_schedule(conn, self).await?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -5,8 +5,7 @@ use sqlx::pool::PoolConnection;
|
|||
use sqlx::Sqlite;
|
||||
|
||||
use crate::db::errors::DatabaseError;
|
||||
use crate::db::models::*;
|
||||
use crate::db::Schedule;
|
||||
use crate::db::{Relay, Schedule};
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct Tag {
|
||||
|
@ -46,6 +45,17 @@ impl Tag {
|
|||
.map(|t| t.ok_or(DatabaseError::NotFound))?
|
||||
}
|
||||
|
||||
pub async fn get_by_tag_or_create(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
target_tag: &str,
|
||||
) -> Result<Tag, DatabaseError> {
|
||||
match Tag::get_by_tag(conn, target_tag).await {
|
||||
Ok(tag) => Ok(tag),
|
||||
Err(DatabaseError::NotFound) => Tag::create(conn, target_tag).await,
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_by_tag(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
target_tag: &str,
|
||||
|
@ -56,7 +66,6 @@ impl Tag {
|
|||
.map(|t| t.ok_or(DatabaseError::NotFound))?
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn link_relay(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue