2023-11-21 13:58:01 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
2023-11-21 02:42:33 +00:00
|
|
|
use sqlx::pool::PoolConnection;
|
|
|
|
use sqlx::Sqlite;
|
2023-11-21 13:58:01 +00:00
|
|
|
|
|
|
|
use crate::db::errors::DatabaseError;
|
|
|
|
use crate::db::models::*;
|
2022-04-02 23:35:51 +00:00
|
|
|
|
2023-11-21 02:42:33 +00:00
|
|
|
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)
|
2022-04-02 23:35:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 02:42:33 +00:00
|
|
|
pub async fn get_tag(
|
|
|
|
conn: &mut PoolConnection<Sqlite>,
|
|
|
|
target_tag: &str,
|
|
|
|
) -> Result<Tag, DatabaseError> {
|
2023-11-20 23:44:45 +00:00
|
|
|
sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag)
|
2023-11-21 02:42:33 +00:00
|
|
|
.fetch_optional(conn.deref_mut())
|
2023-11-20 23:44:45 +00:00
|
|
|
.await
|
|
|
|
.map(|t| t.ok_or(DatabaseError::NotFound))?
|
2022-04-03 16:39:31 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 23:44:45 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub async fn create_junction_tag_relay(
|
2023-11-21 02:42:33 +00:00
|
|
|
conn: &mut PoolConnection<Sqlite>,
|
2022-07-19 22:38:09 +00:00
|
|
|
target_tag: Tag,
|
2023-11-20 23:44:45 +00:00
|
|
|
target_relay: &Relay,
|
2022-07-17 18:19:36 +00:00
|
|
|
) -> Result<JunctionTag, DatabaseError> {
|
2023-11-21 02:42:33 +00:00
|
|
|
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)
|
2023-11-20 23:44:45 +00:00
|
|
|
}
|
2022-04-02 23:35:51 +00:00
|
|
|
|
2023-11-20 23:44:45 +00:00
|
|
|
pub async fn create_junction_tag_schedule(
|
2023-11-21 02:42:33 +00:00
|
|
|
conn: &mut PoolConnection<Sqlite>,
|
2023-11-20 23:44:45 +00:00
|
|
|
target_tag: Tag,
|
|
|
|
target_schedule: &Schedule,
|
|
|
|
) -> Result<JunctionTag, DatabaseError> {
|
2023-11-21 02:42:33 +00:00
|
|
|
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)
|
2022-07-17 18:19:36 +00:00
|
|
|
}
|