Cleanup db/tag namespace

This commit is contained in:
Tobias Reisinger 2023-11-21 16:46:12 +01:00
parent effd3f3b18
commit 80d1fa32a0
4 changed files with 71 additions and 69 deletions

View file

@ -6,16 +6,3 @@ pub struct Relay {
pub id: i64, pub id: i64,
// TODO // 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<i64>,
pub schedule_id: Option<i64>,
}

View file

@ -7,8 +7,7 @@ use sqlx::Sqlite;
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::model_utils::Period; use crate::db::model_utils::Period;
use crate::db::models::*; use crate::db::tag::Tag;
use crate::db::tag::{create_junction_tag_schedule, create_tag};
use crate::types::EmgauwaUid; use crate::types::EmgauwaUid;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]
@ -143,10 +142,10 @@ impl Schedule {
let tag = match tag { let tag = match tag {
Some(id) => id, 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(()) Ok(())
} }

View file

@ -1,3 +1,4 @@
use serde_derive::Serialize;
use std::ops::DerefMut; use std::ops::DerefMut;
use sqlx::pool::PoolConnection; use sqlx::pool::PoolConnection;
@ -7,59 +8,74 @@ use crate::db::errors::DatabaseError;
use crate::db::models::*; use crate::db::models::*;
use crate::db::schedules::Schedule; use crate::db::schedules::Schedule;
pub async fn create_tag( #[derive(Debug, Serialize, Clone)]
conn: &mut PoolConnection<Sqlite>, pub struct Tag {
new_tag: &str, pub id: i64,
) -> Result<Tag, DatabaseError> { pub tag: String,
sqlx::query_as!(
Tag,
"INSERT INTO tags (tag) VALUES (?) RETURNING *",
new_tag
)
.fetch_optional(conn.deref_mut())
.await?
.ok_or(DatabaseError::InsertGetError)
} }
pub async fn get_tag( pub struct JunctionTag {
conn: &mut PoolConnection<Sqlite>, pub id: i64,
target_tag: &str, pub tag_id: i64,
) -> Result<Tag, DatabaseError> { pub relay_id: Option<i64>,
sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag) pub schedule_id: Option<i64>,
}
impl Tag {
pub async fn create(
conn: &mut PoolConnection<Sqlite>,
new_tag: &str,
) -> Result<Tag, DatabaseError> {
sqlx::query_as!(
Tag,
"INSERT INTO tags (tag) VALUES (?) RETURNING *",
new_tag
)
.fetch_optional(conn.deref_mut()) .fetch_optional(conn.deref_mut())
.await .await?
.map(|t| t.ok_or(DatabaseError::NotFound))? .ok_or(DatabaseError::InsertGetError)
} }
#[allow(dead_code)] pub async fn get(
pub async fn create_junction_tag_relay( conn: &mut PoolConnection<Sqlite>,
conn: &mut PoolConnection<Sqlite>, target_tag: &str,
target_tag: Tag, ) -> Result<Tag, DatabaseError> {
target_relay: &Relay, sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag)
) -> Result<JunctionTag, DatabaseError> { .fetch_optional(conn.deref_mut())
sqlx::query_as!( .await
JunctionTag, .map(|t| t.ok_or(DatabaseError::NotFound))?
"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 create_junction_tag_schedule( #[allow(dead_code)]
conn: &mut PoolConnection<Sqlite>, pub async fn link_relay(
target_tag: Tag, &self,
target_schedule: &Schedule, conn: &mut PoolConnection<Sqlite>,
) -> Result<JunctionTag, DatabaseError> { target_relay: &Relay,
sqlx::query_as!( ) -> Result<JunctionTag, DatabaseError> {
JunctionTag, sqlx::query_as!(
"INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *", JunctionTag,
target_tag.id, "INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *",
target_schedule.id self.id,
) target_relay.id
.fetch_optional(conn.deref_mut()) )
.await? .fetch_optional(conn.deref_mut())
.ok_or(DatabaseError::InsertGetError) .await?
.ok_or(DatabaseError::InsertGetError)
}
pub async fn link_schedule(
&self,
conn: &mut PoolConnection<Sqlite>,
target_schedule: &Schedule,
) -> Result<JunctionTag, DatabaseError> {
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)
}
} }

View file

@ -8,7 +8,7 @@ use sqlx::{Pool, Sqlite};
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::schedules::*; use crate::db::schedules::*;
use crate::db::tag::get_tag; use crate::db::tag::Tag;
use crate::handlers::errors::ApiError; use crate::handlers::errors::ApiError;
use crate::return_models::ReturnSchedule; use crate::return_models::ReturnSchedule;
use crate::types::EmgauwaUid; use crate::types::EmgauwaUid;
@ -44,7 +44,7 @@ pub async fn tagged(
let mut pool_conn = pool.acquire().await?; let mut pool_conn = pool.acquire().await?;
let (tag,) = path.into_inner(); 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?; let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;