Refactor models names

This commit is contained in:
Tobias Reisinger 2023-11-27 12:49:40 +01:00
parent 76b14ce75b
commit be7f31906c
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
24 changed files with 461 additions and 340 deletions
emgauwa-lib/src/db

View file

@ -5,11 +5,11 @@ use sqlx::pool::PoolConnection;
use sqlx::Sqlite;
use crate::db::errors::DatabaseError;
use crate::db::Tag;
use crate::db::types::ControllerUid;
use crate::db::DbTag;
use crate::types::ControllerUid;
#[derive(Debug, Serialize, Clone)]
pub struct Controller {
pub struct DbController {
pub id: i64,
pub uid: ControllerUid,
pub name: String,
@ -17,11 +17,11 @@ pub struct Controller {
pub active: bool,
}
impl Controller {
impl DbController {
pub async fn get_all(
conn: &mut PoolConnection<Sqlite>,
) -> Result<Vec<Controller>, DatabaseError> {
Ok(sqlx::query_as!(Controller, "SELECT * FROM controllers")
) -> Result<Vec<DbController>, DatabaseError> {
Ok(sqlx::query_as!(DbController, "SELECT * FROM controllers")
.fetch_all(conn.deref_mut())
.await?)
}
@ -29,12 +29,8 @@ impl Controller {
pub async fn get(
conn: &mut PoolConnection<Sqlite>,
id: i64,
) -> Result<Controller, DatabaseError> {
sqlx::query_as!(
Controller,
"SELECT * FROM controllers WHERE id = ?",
id
)
) -> Result<DbController, DatabaseError> {
sqlx::query_as!(DbController, "SELECT * FROM controllers WHERE id = ?", id)
.fetch_optional(conn.deref_mut())
.await
.map(|s| s.ok_or(DatabaseError::NotFound))?
@ -43,9 +39,9 @@ impl Controller {
pub async fn get_by_uid(
conn: &mut PoolConnection<Sqlite>,
filter_uid: &ControllerUid,
) -> Result<Controller, DatabaseError> {
) -> Result<DbController, DatabaseError> {
sqlx::query_as!(
Controller,
DbController,
"SELECT * FROM controllers WHERE uid = ?",
filter_uid
)
@ -56,9 +52,9 @@ impl Controller {
pub async fn get_by_tag(
conn: &mut PoolConnection<Sqlite>,
tag: &Tag,
) -> Result<Vec<Controller>, DatabaseError> {
Ok(sqlx::query_as!(Controller, "SELECT schedule.* FROM controllers AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
tag: &DbTag,
) -> Result<Vec<DbController>, DatabaseError> {
Ok(sqlx::query_as!(DbController, "SELECT schedule.* FROM controllers 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?)
}
@ -81,10 +77,10 @@ impl Controller {
new_uid: &ControllerUid,
new_name: &str,
new_relay_count: i64,
new_active: bool
) -> Result<Controller, DatabaseError> {
new_active: bool,
) -> Result<DbController, DatabaseError> {
sqlx::query_as!(
Controller,
DbController,
"INSERT INTO controllers (uid, name, relay_count, active) VALUES (?, ?, ?, ?) RETURNING *",
new_uid,
new_name,
@ -101,8 +97,8 @@ impl Controller {
conn: &mut PoolConnection<Sqlite>,
new_name: &str,
new_relay_count: i64,
new_active: bool
) -> Result<Controller, DatabaseError> {
new_active: bool,
) -> Result<DbController, DatabaseError> {
sqlx::query!(
"UPDATE controllers SET name = ?, relay_count = ?, active = ? WHERE id = ?",
new_name,