chyabrung-cli 0.2.4

The `chy` command line tool for Chyabrung: scaffolding, dev server with hot reload, builds, generators, migrations and one-command deploys
//! Site statistics, cached for a minute.

use crate::models::{Post, User};
use chyabrung::prelude::*;
use std::time::Duration;

pub struct StatsController;

#[controller]
impl StatsController {
    /// How many users and posts there are.
    async fn index(cache: Cache) -> Result<Json<serde_json::Value>> {
        let stats = cache
            .remember("stats", Some(Duration::from_secs(60)), || async {
                Ok(json!({ "users": User::query().count().await?, "posts": Post::query().count().await? }))
            })
            .await?;
        Ok(Json(stats))
    }
}