Moved Article data type specific stuff to the database package
This commit is contained in:
parent
3657e66612
commit
993181ba0d
7 changed files with 138 additions and 108 deletions
135
Cargo.lock
generated
135
Cargo.lock
generated
|
|
@ -6,6 +6,7 @@ version = 3
|
|||
name = "achubb_backend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"achubb_database",
|
||||
"anyhow",
|
||||
"askama",
|
||||
"axum",
|
||||
|
|
@ -21,6 +22,19 @@ dependencies = [
|
|||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "achubb_database"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"futures-util",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
"time",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.21.0"
|
||||
|
|
@ -64,6 +78,55 @@ version = "0.2.18"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5"
|
||||
dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.82"
|
||||
|
|
@ -286,6 +349,52 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
|
||||
dependencies = [
|
||||
"heck 0.5.0",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.60",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
|
||||
|
||||
[[package]]
|
||||
name = "const-oid"
|
||||
version = "0.9.6"
|
||||
|
|
@ -600,6 +709,12 @@ dependencies = [
|
|||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.3.9"
|
||||
|
|
@ -731,6 +846,12 @@ dependencies = [
|
|||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.12.1"
|
||||
|
|
@ -1555,7 +1676,7 @@ checksum = "5833ef53aaa16d860e92123292f1f6a3d53c34ba8b1969f152ef1a7bb803f3c8"
|
|||
dependencies = [
|
||||
"dotenvy",
|
||||
"either",
|
||||
"heck",
|
||||
"heck 0.4.1",
|
||||
"hex",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
|
|
@ -1690,6 +1811,12 @@ dependencies = [
|
|||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.5.0"
|
||||
|
|
@ -2058,6 +2185,12 @@ version = "2.1.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
achubb_database = { path = "../achubb_database" }
|
||||
anyhow = "1.0.75"
|
||||
askama = "0.12.1"
|
||||
axum = "0.6"
|
||||
|
|
|
|||
|
|
@ -1,90 +0,0 @@
|
|||
use futures_util::TryStreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::postgres::PgPool;
|
||||
use std::error::Error;
|
||||
use time::Date;
|
||||
|
||||
pub trait PsqlData {
|
||||
async fn read_all(pool: &PgPool) -> Result<Vec<Box<Self>>, Box<dyn Error>>;
|
||||
|
||||
async fn read(pool: &PgPool, id: i32) -> Result<Box<Self>, Box<dyn Error>>;
|
||||
|
||||
async fn insert(&self, pool: &PgPool) -> Result<(), Box<dyn Error>>;
|
||||
|
||||
async fn update(&self, pool: &PgPool) -> Result<(), Box<dyn Error>>;
|
||||
|
||||
async fn delete(&self, pool: &PgPool) -> Result<(), Box<dyn Error>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
||||
pub struct Article {
|
||||
id: i32,
|
||||
pub reference: String,
|
||||
pub title: String,
|
||||
pub content: String,
|
||||
pub previous: Option<String>,
|
||||
pub next: Option<String>,
|
||||
pub date: Option<Date>,
|
||||
}
|
||||
|
||||
impl Article {
|
||||
pub async fn read_by_reference(pool: &PgPool, reference: &String) -> Result<Box<Self>, Box<dyn Error>> {
|
||||
let result = sqlx::query_as!(
|
||||
Self,
|
||||
"SELECT * FROM articles WHERE reference = $1",
|
||||
reference
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(Box::new(result))
|
||||
}
|
||||
}
|
||||
|
||||
impl PsqlData for Article {
|
||||
async fn read_all(pool: &PgPool) -> Result<Vec<Box<Self>>, Box<dyn Error>> {
|
||||
crate::psql_read_all!(Self, pool, "articles")
|
||||
}
|
||||
|
||||
async fn read(pool: &PgPool, id: i32) -> Result<Box<Self>, Box<dyn Error>> {
|
||||
crate::psql_read!(Self, pool, id, "articles")
|
||||
}
|
||||
|
||||
async fn insert(&self, pool: &PgPool) -> Result<(), Box<dyn Error>> {
|
||||
sqlx::query!(
|
||||
"INSERT INTO articles (reference, title, content, previous, next, date) VALUES ($1, $2, $3, $4, $5, $6)",
|
||||
self.reference,
|
||||
self.title,
|
||||
self.content,
|
||||
self.previous,
|
||||
self.next,
|
||||
self.date,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update(&self, pool: &PgPool) -> Result<(), Box<dyn Error>> {
|
||||
sqlx::query!(
|
||||
"UPDATE articles SET reference=$1, title=$2, content=$3, previous=$4, next=$5, date=$6 WHERE id=$7",
|
||||
self.reference,
|
||||
self.title,
|
||||
self.content,
|
||||
self.previous,
|
||||
self.next,
|
||||
self.date,
|
||||
self.id
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(&self, pool: &PgPool) -> Result<(), Box<dyn Error>> {
|
||||
let id = &self.id;
|
||||
crate::psql_delete!(id, pool, "articles")
|
||||
}
|
||||
}
|
||||
|
|
@ -2,8 +2,6 @@ use sqlx::postgres::{PgPool, PgPoolOptions};
|
|||
use std::env;
|
||||
use std::error::Error;
|
||||
|
||||
pub mod data;
|
||||
|
||||
pub async fn establish_connection() -> Result<PgPool, Box<dyn Error>> {
|
||||
let db_url = match env::var("DATABASE_URL") {
|
||||
Ok(s) => s,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::database::data::{Article, PsqlData};
|
||||
use achubb_database::data::{article::Article, PsqlData};
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
extract::{Extension, Path},
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ pub mod root;
|
|||
pub mod blog;
|
||||
pub mod projects;
|
||||
pub mod api;
|
||||
|
||||
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
|
||||
pub struct HtmlTemplate<T>(pub T);
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ async fn home(state: Extension<AppState>) -> impl IntoResponse {
|
|||
.await
|
||||
.expect("couldn't get articles");
|
||||
|
||||
let (head, tail) = list.split_at(5);
|
||||
let (head, _) = list.split_at(5);
|
||||
|
||||
let template = HomeTemplate {
|
||||
active_navbar: NavBar::HOME,
|
||||
|
|
@ -59,19 +59,6 @@ struct HomeTemplate {
|
|||
recent_blogs: String,
|
||||
}
|
||||
|
||||
pub async fn projects() -> impl IntoResponse {
|
||||
let template = ProjectsTemplate {
|
||||
active_navbar: NavBar::PROJECTS,
|
||||
};
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "projects.html")]
|
||||
struct ProjectsTemplate {
|
||||
active_navbar: &'static str,
|
||||
}
|
||||
|
||||
async fn now() -> impl IntoResponse {
|
||||
let template = NowTemplate {
|
||||
active_navbar: NavBar::NOW,
|
||||
|
|
|
|||
Loading…
Reference in a new issue