tirocinio-canali-steffo-acrate/acrate_database/src/meta.rs

167 lines
4.5 KiB
Rust
Raw Normal View History

2024-11-16 05:45:58 +00:00
use diesel::{Associations, Identifiable, Insertable, QueryResult, Queryable, QueryableByName, Selectable, pg::Pg};
2024-11-15 02:16:06 +00:00
use diesel_async::AsyncPgConnection;
2024-11-15 01:16:16 +00:00
use uuid::Uuid;
use super::schema;
#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)]
#[diesel(table_name = schema::meta_subjects)]
2024-11-16 05:45:58 +00:00
#[diesel(check_for_backend(Pg))]
pub struct MetaSubject {
pub id: Uuid,
pub document: String,
pub pattern: String,
2024-11-16 06:43:54 +00:00
pub subject: Option<String>,
pub redirect: Option<String>,
}
2024-11-15 01:16:16 +00:00
#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)]
#[diesel(table_name = schema::meta_aliases)]
2024-11-16 05:45:58 +00:00
#[diesel(check_for_backend(Pg))]
2024-11-15 01:16:16 +00:00
pub struct MetaAlias {
pub id: Uuid,
2024-11-15 04:21:05 +00:00
pub document: String,
2024-11-15 01:16:16 +00:00
pub pattern: String,
pub alias: String,
}
#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)]
#[diesel(table_name = schema::meta_links)]
2024-11-16 05:45:58 +00:00
#[diesel(check_for_backend(Pg))]
2024-11-15 01:16:16 +00:00
pub struct MetaLink {
pub id: Uuid,
2024-11-15 04:21:05 +00:00
pub document: String,
2024-11-15 01:16:16 +00:00
pub pattern: String,
pub rel: String,
2024-11-15 19:44:06 +00:00
pub type_: Option<String>,
2024-11-15 01:16:16 +00:00
pub href: Option<String>,
2024-11-15 19:53:59 +00:00
pub template: Option<String>,
2024-11-15 01:16:16 +00:00
}
#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable, Associations)]
#[diesel(belongs_to(MetaLink))]
#[diesel(table_name = schema::meta_link_properties)]
2024-11-16 05:45:58 +00:00
#[diesel(check_for_backend(Pg))]
2024-11-15 01:16:16 +00:00
pub struct MetaLinkProperty {
pub id: Uuid,
pub meta_link_id: Uuid,
pub rel: String,
pub value: Option<String>,
}
2024-11-15 19:53:59 +00:00
#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable, Associations)]
#[diesel(belongs_to(MetaLink))]
#[diesel(table_name = schema::meta_link_titles)]
2024-11-16 05:45:58 +00:00
#[diesel(check_for_backend(Pg))]
2024-11-15 19:53:59 +00:00
pub struct MetaLinkTitle {
pub id: Uuid,
pub meta_link_id: Uuid,
pub language: String,
pub value: String,
}
2024-11-15 01:16:16 +00:00
#[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)]
#[diesel(table_name = schema::meta_properties)]
2024-11-16 05:45:58 +00:00
#[diesel(check_for_backend(Pg))]
2024-11-15 01:16:16 +00:00
pub struct MetaProperty {
pub id: Uuid,
2024-11-15 04:21:05 +00:00
pub document: String,
2024-11-15 01:16:16 +00:00
pub pattern: String,
pub rel: String,
2024-11-15 01:16:16 +00:00
pub value: Option<String>,
}
2024-11-15 02:16:06 +00:00
impl MetaSubject {
2024-11-16 06:49:12 +00:00
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject_: &str) -> QueryResult<Vec<Self>> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use schema::meta_subjects::dsl::*;
let document_is_equal = document.eq(doc);
2024-11-16 06:49:12 +00:00
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
}
}
2024-11-15 02:16:06 +00:00
impl MetaAlias {
2024-11-16 05:45:58 +00:00
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
2024-11-15 02:16:06 +00:00
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use schema::meta_aliases::dsl::*;
let document_is_equal = document.eq(doc);
2024-11-15 02:16:06 +00:00
let subject_matches_pattern = subject.into_sql::<diesel::sql_types::Text>().ilike(pattern);
meta_aliases
.filter(document_is_equal)
2024-11-15 02:16:06 +00:00
.filter(subject_matches_pattern)
.select(Self::as_select())
.load(conn)
.await
}
}
impl MetaLink {
2024-11-16 05:45:58 +00:00
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
2024-11-15 02:16:06 +00:00
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use schema::meta_links::dsl::*;
let document_is_equal = document.eq(doc);
2024-11-15 02:16:06 +00:00
let subject_matches_pattern = subject.into_sql::<diesel::sql_types::Text>().ilike(pattern);
meta_links
.filter(document_is_equal)
2024-11-15 02:16:06 +00:00
.filter(subject_matches_pattern)
.select(Self::as_select())
.load(conn)
.await
}
}
impl MetaLinkProperty {
2024-11-16 05:45:58 +00:00
pub async fn query_by_link(conn: &mut AsyncPgConnection, links: &[MetaLink]) -> QueryResult<Vec<Self>> {
2024-11-15 02:16:06 +00:00
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
Self::belonging_to(links)
2024-11-15 02:16:06 +00:00
.load(conn)
.await
}
}
impl MetaLinkTitle {
2024-11-16 05:45:58 +00:00
pub async fn query_by_link(conn: &mut AsyncPgConnection, links: &[MetaLink]) -> QueryResult<Vec<Self>> {
2024-11-15 19:53:59 +00:00
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
Self::belonging_to(links)
2024-11-15 19:53:59 +00:00
.load(conn)
.await
}
2024-11-15 02:16:06 +00:00
}
impl MetaProperty {
2024-11-16 05:45:58 +00:00
pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
2024-11-15 02:16:06 +00:00
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use schema::meta_properties::dsl::*;
let document_is_equal = document.eq(doc);
2024-11-15 02:16:06 +00:00
let subject_matches_pattern = subject.into_sql::<diesel::sql_types::Text>().ilike(pattern);
meta_properties
.filter(document_is_equal)
2024-11-15 02:16:06 +00:00
.filter(subject_matches_pattern)
.select(Self::as_select())
.load(conn)
.await
}
}