From c235467b060c3848404805217e7ec98e8b74ee24 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 15 Nov 2024 03:16:06 +0100 Subject: [PATCH] `core`: Add queryable `meta` structs --- acrate-core/src/meta.rs | 64 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/acrate-core/src/meta.rs b/acrate-core/src/meta.rs index aadf736..a365579 100644 --- a/acrate-core/src/meta.rs +++ b/acrate-core/src/meta.rs @@ -1,6 +1,6 @@ -use diesel::{Associations, Identifiable, Insertable, Queryable, QueryableByName, Selectable}; +use diesel::{Associations, Identifiable, Insertable, QueryResult, Queryable, QueryableByName, Selectable}; +use diesel_async::AsyncPgConnection; use uuid::Uuid; - use super::schema; @@ -18,7 +18,7 @@ pub struct MetaLink { pub id: Uuid, pub pattern: String, pub rel: String, - pub type_: Option, + pub r#type: Option, pub href: Option, } @@ -39,3 +39,61 @@ pub struct MetaProperty { pub pattern: String, pub value: Option, } + + +impl MetaAlias { + pub async fn query_matching(conn: &mut AsyncPgConnection, subject: &str) -> QueryResult> { + use diesel::prelude::*; + use diesel_async::RunQueryDsl; + use schema::meta_aliases::dsl::*; + + let subject_matches_pattern = subject.into_sql::().ilike(pattern); + + meta_aliases + .filter(subject_matches_pattern) + .select(Self::as_select()) + .load(conn) + .await + } +} + +impl MetaLink { + pub async fn query_matching(conn: &mut AsyncPgConnection, subject: &str) -> QueryResult> { + use diesel::prelude::*; + use diesel_async::RunQueryDsl; + use schema::meta_links::dsl::*; + + let subject_matches_pattern = subject.into_sql::().ilike(pattern); + + meta_links + .filter(subject_matches_pattern) + .select(Self::as_select()) + .load(conn) + .await + } + + pub async fn query_properties(&self, conn: &mut AsyncPgConnection) -> QueryResult> { + use diesel::prelude::*; + use diesel_async::RunQueryDsl; + + MetaLinkProperty::belonging_to(self) + .load(conn) + .await + } +} + +impl MetaProperty { + pub async fn query_matching(conn: &mut AsyncPgConnection, subject: &str) -> QueryResult> { + use diesel::prelude::*; + use diesel_async::RunQueryDsl; + use schema::meta_properties::dsl::*; + + let subject_matches_pattern = subject.into_sql::().ilike(pattern); + + meta_properties + .filter(subject_matches_pattern) + .select(Self::as_select()) + .load(conn) + .await + } +}