From 80d1fa32a0ba56dcc6d612214e47bbb4417f79e6 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Tue, 21 Nov 2023 16:46:12 +0100 Subject: [PATCH] Cleanup db/tag namespace --- src/db/models.rs | 13 ---- src/db/schedules.rs | 7 +-- src/db/tag.rs | 116 ++++++++++++++++++++--------------- src/handlers/v1/schedules.rs | 4 +- 4 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/db/models.rs b/src/db/models.rs index 1c7e5c7..6176d2f 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -6,16 +6,3 @@ pub struct Relay { pub id: i64, // TODO } - -#[derive(Debug, Serialize, Clone)] -pub struct Tag { - pub id: i64, - pub tag: String, -} - -pub struct JunctionTag { - pub id: i64, - pub tag_id: i64, - pub relay_id: Option, - pub schedule_id: Option, -} diff --git a/src/db/schedules.rs b/src/db/schedules.rs index ae94555..2800ff0 100644 --- a/src/db/schedules.rs +++ b/src/db/schedules.rs @@ -7,8 +7,7 @@ use sqlx::Sqlite; use crate::db::errors::DatabaseError; use crate::db::model_utils::Period; -use crate::db::models::*; -use crate::db::tag::{create_junction_tag_schedule, create_tag}; +use crate::db::tag::Tag; use crate::types::EmgauwaUid; #[derive(Debug, Serialize, Clone)] @@ -143,10 +142,10 @@ impl Schedule { let tag = match tag { Some(id) => id, - None => create_tag(conn, new_tag).await?, + None => Tag::create(conn, new_tag).await?, }; - create_junction_tag_schedule(conn, tag, self).await?; + tag.link_schedule(conn, self).await?; } Ok(()) } diff --git a/src/db/tag.rs b/src/db/tag.rs index e612bed..2f10318 100644 --- a/src/db/tag.rs +++ b/src/db/tag.rs @@ -1,3 +1,4 @@ +use serde_derive::Serialize; use std::ops::DerefMut; use sqlx::pool::PoolConnection; @@ -7,59 +8,74 @@ use crate::db::errors::DatabaseError; use crate::db::models::*; use crate::db::schedules::Schedule; -pub async fn create_tag( - conn: &mut PoolConnection, - new_tag: &str, -) -> Result { - sqlx::query_as!( - Tag, - "INSERT INTO tags (tag) VALUES (?) RETURNING *", - new_tag - ) - .fetch_optional(conn.deref_mut()) - .await? - .ok_or(DatabaseError::InsertGetError) +#[derive(Debug, Serialize, Clone)] +pub struct Tag { + pub id: i64, + pub tag: String, } -pub async fn get_tag( - conn: &mut PoolConnection, - target_tag: &str, -) -> Result { - sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag) +pub struct JunctionTag { + pub id: i64, + pub tag_id: i64, + pub relay_id: Option, + pub schedule_id: Option, +} + +impl Tag { + pub async fn create( + conn: &mut PoolConnection, + new_tag: &str, + ) -> Result { + sqlx::query_as!( + Tag, + "INSERT INTO tags (tag) VALUES (?) RETURNING *", + new_tag + ) .fetch_optional(conn.deref_mut()) - .await - .map(|t| t.ok_or(DatabaseError::NotFound))? -} + .await? + .ok_or(DatabaseError::InsertGetError) + } -#[allow(dead_code)] -pub async fn create_junction_tag_relay( - conn: &mut PoolConnection, - target_tag: Tag, - target_relay: &Relay, -) -> Result { - sqlx::query_as!( - JunctionTag, - "INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *", - target_tag.id, - target_relay.id - ) - .fetch_optional(conn.deref_mut()) - .await? - .ok_or(DatabaseError::InsertGetError) -} + pub async fn get( + conn: &mut PoolConnection, + target_tag: &str, + ) -> Result { + sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag) + .fetch_optional(conn.deref_mut()) + .await + .map(|t| t.ok_or(DatabaseError::NotFound))? + } -pub async fn create_junction_tag_schedule( - conn: &mut PoolConnection, - target_tag: Tag, - target_schedule: &Schedule, -) -> Result { - sqlx::query_as!( - JunctionTag, - "INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *", - target_tag.id, - target_schedule.id - ) - .fetch_optional(conn.deref_mut()) - .await? - .ok_or(DatabaseError::InsertGetError) + #[allow(dead_code)] + pub async fn link_relay( + &self, + conn: &mut PoolConnection, + target_relay: &Relay, + ) -> Result { + sqlx::query_as!( + JunctionTag, + "INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *", + self.id, + target_relay.id + ) + .fetch_optional(conn.deref_mut()) + .await? + .ok_or(DatabaseError::InsertGetError) + } + + pub async fn link_schedule( + &self, + conn: &mut PoolConnection, + target_schedule: &Schedule, + ) -> Result { + sqlx::query_as!( + JunctionTag, + "INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *", + self.id, + target_schedule.id + ) + .fetch_optional(conn.deref_mut()) + .await? + .ok_or(DatabaseError::InsertGetError) + } } diff --git a/src/handlers/v1/schedules.rs b/src/handlers/v1/schedules.rs index 9a5b285..1835f25 100644 --- a/src/handlers/v1/schedules.rs +++ b/src/handlers/v1/schedules.rs @@ -8,7 +8,7 @@ use sqlx::{Pool, Sqlite}; use crate::db::errors::DatabaseError; use crate::db::schedules::*; -use crate::db::tag::get_tag; +use crate::db::tag::Tag; use crate::handlers::errors::ApiError; use crate::return_models::ReturnSchedule; use crate::types::EmgauwaUid; @@ -44,7 +44,7 @@ pub async fn tagged( let mut pool_conn = pool.acquire().await?; let (tag,) = path.into_inner(); - let tag_db = get_tag(&mut pool_conn, &tag).await?; + let tag_db = Tag::get(&mut pool_conn, &tag).await?; let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;