diff --git a/acrate-core/migrations/2024-11-14-031744_meta/down.sql b/acrate-core/migrations/2024-11-14-031744_meta/down.sql index 2c09392..39d835e 100644 --- a/acrate-core/migrations/2024-11-14-031744_meta/down.sql +++ b/acrate-core/migrations/2024-11-14-031744_meta/down.sql @@ -3,3 +3,4 @@ DROP TABLE IF EXISTS meta_link_properties CASCADE; DROP TABLE IF EXISTS meta_link_titles CASCADE; DROP TABLE IF EXISTS meta_links CASCADE; DROP TABLE IF EXISTS meta_aliases CASCADE; +DROP TABLE IF EXISTS meta_subjects CASCADE; diff --git a/acrate-core/migrations/2024-11-14-031744_meta/up.sql b/acrate-core/migrations/2024-11-14-031744_meta/up.sql index 012feab..c0f6c46 100644 --- a/acrate-core/migrations/2024-11-14-031744_meta/up.sql +++ b/acrate-core/migrations/2024-11-14-031744_meta/up.sql @@ -1,3 +1,12 @@ +CREATE TABLE IF NOT EXISTS meta_subjects ( + id UUID DEFAULT gen_random_uuid(), + document BPCHAR NOT NULL, + pattern BPCHAR NOT NULL, + redirect BPCHAR NOT NULL, + + PRIMARY KEY (id) +); + CREATE TABLE IF NOT EXISTS meta_aliases ( id UUID DEFAULT gen_random_uuid(), document BPCHAR NOT NULL, diff --git a/acrate-core/src/meta.rs b/acrate-core/src/meta.rs index ef66ab7..aca144c 100644 --- a/acrate-core/src/meta.rs +++ b/acrate-core/src/meta.rs @@ -4,6 +4,15 @@ use uuid::Uuid; use super::schema; +#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)] +#[diesel(table_name = schema::meta_subjects)] +pub struct MetaSubject { + pub id: Uuid, + pub document: String, + pub pattern: String, + pub redirect: Option, +} + #[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)] #[diesel(table_name = schema::meta_aliases)] pub struct MetaAlias { @@ -55,8 +64,26 @@ pub struct MetaProperty { pub value: Option, } +impl MetaSubject { + pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { + use diesel::prelude::*; + use diesel_async::RunQueryDsl; + use schema::meta_subjects::dsl::*; + + let document_is_equal = document.eq(doc); + let subject_matches_pattern = subject.into_sql::().ilike(pattern); + + meta_subjects + .filter(document_is_equal) + .filter(subject_matches_pattern) + .select(Self::as_select()) + .load(conn) + .await + } +} + impl MetaAlias { - pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { + pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { use diesel::prelude::*; use diesel_async::RunQueryDsl; use schema::meta_aliases::dsl::*; @@ -74,7 +101,7 @@ impl MetaAlias { } impl MetaLink { - pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { + pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { use diesel::prelude::*; use diesel_async::RunQueryDsl; use schema::meta_links::dsl::*; diff --git a/acrate-core/src/schema.rs b/acrate-core/src/schema.rs index e6544b5..4e9855d 100644 --- a/acrate-core/src/schema.rs +++ b/acrate-core/src/schema.rs @@ -50,6 +50,15 @@ diesel::table! { } } +diesel::table! { + meta_subjects (id) { + id -> Uuid, + document -> Bpchar, + pattern -> Bpchar, + redirect -> Bpchar, + } +} + diesel::joinable!(meta_link_properties -> meta_links (meta_link_id)); diesel::joinable!(meta_link_titles -> meta_links (meta_link_id)); @@ -59,4 +68,5 @@ diesel::allow_tables_to_appear_in_same_query!( meta_link_titles, meta_links, meta_properties, + meta_subjects, );