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,
// 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::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(())
}

View file

@ -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<Sqlite>,
new_tag: &str,
) -> Result<Tag, DatabaseError> {
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<Sqlite>,
target_tag: &str,
) -> Result<Tag, DatabaseError> {
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<i64>,
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())
.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<Sqlite>,
target_tag: Tag,
target_relay: &Relay,
) -> Result<JunctionTag, DatabaseError> {
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<Sqlite>,
target_tag: &str,
) -> Result<Tag, DatabaseError> {
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<Sqlite>,
target_tag: Tag,
target_schedule: &Schedule,
) -> Result<JunctionTag, DatabaseError> {
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<Sqlite>,
target_relay: &Relay,
) -> Result<JunctionTag, DatabaseError> {
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<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::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?;