Skip to content

Commit

Permalink
Avoid hardcode of field list in EdgeDB query
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquan committed Sep 1, 2023
1 parent 63c8236 commit 0916e9d
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 79 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ edgedb-derive = "0.5.1"
edgedb-errors = { version = "0.4.1", features = ["miette"] }
edgedb-protocol = { version = "0.6.0", features = ["all-types"] }
edgedb-tokio = { version = "0.5.0", features = ["serde_json", "miette-errors"] }
field_names = "0.2.0"
fluent-bundle = "0.15.2"
fluent-templates = "0.8.0"
fred = { version = "6.3.0", features = ["serde-json", "partial-tracing"] }
Expand Down
20 changes: 18 additions & 2 deletions src/models/blogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use serde_json::Value as JValue;
use strum_macros::{Display, EnumString, IntoStaticStr};
use uuid::Uuid;
use atom_syndication::{Entry as AtomEntry, EntryBuilder, LinkBuilder, Category as AtomCategory, CategoryBuilder, Text, Person};
use field_names::FieldNames;

use crate::types::conversions::{
serialize_edge_datetime, serialize_optional_edge_datetime,
};
use crate::types::EdgeSelectable;
use super::users::MiniUser;
use super::feeds::{JsonAuthor, JsonItem};
use crate::utils::html::strip_tags;
Expand Down Expand Up @@ -173,13 +175,20 @@ impl From<MediumBlogPost> for JsonItem {
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, Queryable)]
#[derive(Debug, Default, Clone, Serialize, Deserialize, Queryable, FieldNames)]
pub struct BlogCategory {
pub id: Uuid,
pub title: String,
pub slug: String,
}

impl EdgeSelectable for BlogCategory {
fn fields_as_shape() -> String {
let fields = Self::FIELDS.join(", ");
format!("{{ {fields} }}")
}
}

impl From<BlogCategory> for AtomCategory {
fn from(value: BlogCategory) -> Self {
let BlogCategory { title, slug, .. } = value;
Expand Down Expand Up @@ -246,7 +255,7 @@ impl Default for DetailedBlogPost {
}

// Struct to represent a BlogPost in the database, with just a few fields enough to build links.
#[derive(Debug, Serialize, Queryable)]
#[derive(Debug, Clone, Serialize, Queryable, FieldNames)]
pub struct MiniBlogPost {
pub id: Uuid,
pub title: String,
Expand All @@ -256,3 +265,10 @@ pub struct MiniBlogPost {
#[serde(serialize_with = "serialize_optional_edge_datetime")]
pub updated_at: Option<EDatetime>,
}

impl EdgeSelectable for MiniBlogPost {
fn fields_as_shape() -> String {
let fields = Self::FIELDS.join(", ");
format!("{{ {fields} }}")
}
}
Loading

0 comments on commit 0916e9d

Please sign in to comment.