Accessing articles from database
This commit is contained in:
parent
9ef63a7de1
commit
3bb48c3c59
2 changed files with 67 additions and 0 deletions
61
src/database/mod.rs
Normal file
61
src/database/mod.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use surrealdb::{
|
||||||
|
engine::remote::ws::{Client, Ws},
|
||||||
|
opt::auth::Root,
|
||||||
|
sql::Thing,
|
||||||
|
Result, Surreal,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Article {
|
||||||
|
title: String,
|
||||||
|
content: String,
|
||||||
|
previous: Option<String>,
|
||||||
|
next: Option<String>,
|
||||||
|
date: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ArticleMetadata {
|
||||||
|
pub id: Thing,
|
||||||
|
title: String,
|
||||||
|
previous: Option<String>,
|
||||||
|
next: Option<String>,
|
||||||
|
date: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct Record {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
id: Thing,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connect() -> Result<Surreal<Client>> {
|
||||||
|
// Connect to the server
|
||||||
|
let db = Surreal::new::<Ws>("127.0.0.1:8080").await?;
|
||||||
|
|
||||||
|
// Signin as a namespace, database, or root user
|
||||||
|
db.signin(Root {
|
||||||
|
username: "root",
|
||||||
|
password: "root",
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Select a specific namespace / database
|
||||||
|
db.use_ns("test").use_db("test").await?;
|
||||||
|
Ok(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_articles(connection: &Surreal<Client>) -> Result<Vec<ArticleMetadata>> {
|
||||||
|
let mut response = connection
|
||||||
|
.query("SELECT id, title, previous, next, date FROM article ORDER BY date DESC")
|
||||||
|
.await?;
|
||||||
|
let articles: Vec<ArticleMetadata> = response.take(0)?;
|
||||||
|
Ok(articles)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_article(connection: &Surreal<Client>, id: String) -> Result<()> {
|
||||||
|
let response: Vec<Article> = connection.select(("article", id)).await?.expect("Hopefully something else has not gone terribly wrong");
|
||||||
|
dbg!(response);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,15 @@
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
use surrealdb::{engine::remote::ws::Client, Surreal};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
mod html;
|
mod html;
|
||||||
|
mod database;
|
||||||
|
|
||||||
pub async fn run() -> anyhow::Result<()> {
|
pub async fn run() -> anyhow::Result<()> {
|
||||||
|
let connection: Surreal<Client> = database::connect().await?;
|
||||||
|
let mut articles: Vec<database::ArticleMetadata> = database::get_all_articles(&connection).await?;
|
||||||
|
database::get_article(&connection, articles.pop().unwrap().id.id.to_string()).await?;
|
||||||
|
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(
|
.with(
|
||||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue