1
Fork 0

core: Add meta_subjects to handle redirects

This commit is contained in:
Steffo 2024-11-16 06:16:34 +01:00
parent d80888c9db
commit dfee4e510e
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 49 additions and 2 deletions

View file

@ -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;

View file

@ -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,

View file

@ -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<String>,
}
#[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<String>,
}
impl MetaSubject {
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult<Vec<MetaSubject>> {
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::<diesel::sql_types::Text>().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<Vec<MetaAlias>> {
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult<Vec<MetaAlias>> {
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<Vec<MetaLink>> {
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult<Vec<MetaLink>> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use schema::meta_links::dsl::*;

View file

@ -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,
);