use super::UserResource;
use crate::models::Post;
use chyabrung::prelude::*;
#[resource(Post)]
pub struct PostResource;
impl JsonResource for PostResource {
fn to_array(&self) -> JsonValue {
json!({
"id": self.id,
"title": self.title,
"body": self.body,
"excerpt": excerpt(&self.body),
"published": self.published,
"author": UserResource::when_loaded(&self.author),
"created_at": self.created_at,
"updated_at": self.updated_at,
})
}
}
fn excerpt(body: &str) -> String {
let mut short: String = body.chars().take(120).collect();
if body.chars().count() > 120 {
short.push('…');
}
short
}