Cleanup db/tag namespace
This commit is contained in:
parent
effd3f3b18
commit
80d1fa32a0
4 changed files with 71 additions and 69 deletions
|
@ -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>,
|
||||
}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use serde_derive::Serialize;
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use sqlx::pool::PoolConnection;
|
||||
|
@ -7,10 +8,24 @@ use crate::db::errors::DatabaseError;
|
|||
use crate::db::models::*;
|
||||
use crate::db::schedules::Schedule;
|
||||
|
||||
pub async fn create_tag(
|
||||
#[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>,
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
pub async fn create(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
new_tag: &str,
|
||||
) -> Result<Tag, DatabaseError> {
|
||||
) -> Result<Tag, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
Tag,
|
||||
"INSERT INTO tags (tag) VALUES (?) RETURNING *",
|
||||
|
@ -19,47 +34,48 @@ pub async fn create_tag(
|
|||
.fetch_optional(conn.deref_mut())
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_tag(
|
||||
pub async fn get(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
target_tag: &str,
|
||||
) -> Result<Tag, DatabaseError> {
|
||||
) -> 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))?
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn create_junction_tag_relay(
|
||||
#[allow(dead_code)]
|
||||
pub async fn link_relay(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
target_tag: Tag,
|
||||
target_relay: &Relay,
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
JunctionTag,
|
||||
"INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *",
|
||||
target_tag.id,
|
||||
self.id,
|
||||
target_relay.id
|
||||
)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_junction_tag_schedule(
|
||||
pub async fn link_schedule(
|
||||
&self,
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
target_tag: Tag,
|
||||
target_schedule: &Schedule,
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
) -> Result<JunctionTag, DatabaseError> {
|
||||
sqlx::query_as!(
|
||||
JunctionTag,
|
||||
"INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *",
|
||||
target_tag.id,
|
||||
self.id,
|
||||
target_schedule.id
|
||||
)
|
||||
.fetch_optional(conn.deref_mut())
|
||||
.await?
|
||||
.ok_or(DatabaseError::InsertGetError)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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?;
|
||||
|
||||
|
|
Loading…
Reference in a new issue