62 lines
1.5 KiB
Rust
62 lines
1.5 KiB
Rust
|
|
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(())
|
||
|
|
}
|