better query_as!

This commit is contained in:
Hamcha 2023-06-29 01:11:09 +02:00
parent 0811e89e65
commit 68dce235c1
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89

View file

@ -13,10 +13,10 @@ use figment::{
Figment,
};
use serde::{Deserialize, Serialize};
use sqlx::{postgres::PgPoolOptions, query_as, Pool, Postgres};
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use std::{net::SocketAddr, sync::Arc};
use crate::content::{Page, PageBlock};
use crate::content::Page;
#[derive(Deserialize, Serialize)]
struct Config {
@ -56,23 +56,9 @@ struct AppState {
}
async fn root(State(state): State<Arc<AppState>>) -> Result<String, GenericError> {
let page = query_as!(
Page,
r#"select
id,
author,
title,
description,
tags,
slug,
created_at,
modified_at,
deleted_at,
blocks as "blocks!: sqlx::types::Json<Vec<PageBlock>>"
from pages limit 1"#
)
.fetch_one(&state.database)
.await?;
let select_query: sqlx::query::QueryAs<'_, _, Page, _> =
sqlx::query_as::<_, Page>("SELECT * FROM pages");
let page: Page = select_query.fetch_one(&state.database).await?;
Ok(page.title)
}