From 2c248c0d707827e50a7fb6216bcc3725fba53804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Art=C3=A9mis?= Date: Sat, 11 Jan 2025 17:18:25 +0100 Subject: [PATCH] some cleanup and progress on solidifying the DB --- Cargo.lock | 1 + Cargo.toml | 1 + schema.sql | 16 +++++----- src/db.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 25 +++++++++++---- 5 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 src/db.rs diff --git a/Cargo.lock b/Cargo.lock index 443fdbc..a8cbe4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -449,6 +449,7 @@ dependencies = [ name = "dolltags" version = "0.1.0" dependencies = [ + "regex", "rocket", "rocket_db_pools", "rocket_dyn_templates", diff --git a/Cargo.toml b/Cargo.toml index f35eff6..32a436c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ rocket = "0.5.1" rocket_dyn_templates = { version = "0.2.0", features = ["tera"] } rocket_db_pools = { version = "0.2.0", features = ["sqlx_sqlite"] } sqlx = { version = "0.7", default-features = false, features = ["sqlite", "macros", "chrono"] } +regex = "1.11" diff --git a/schema.sql b/schema.sql index 6e9b3e9..5e5e1a4 100644 --- a/schema.sql +++ b/schema.sql @@ -10,17 +10,17 @@ create table doll_profiles ( name text not null, pronouns text not null, -- format as `{subject}/{object}/{possessive}` eg `she/her/hers` handler_name text not null, - handler_url text not null, + handler_url text, -- customisation options for various entities - description text not null, - kind text not null, - breed text not null, - chassis_type text not null, - chassis_id text not null, - chassis_color text not null, + description text, + kind text, + breed text, + chassis_type text, + chassis_id text, + chassis_color text, -- ID'ing - behaviour text not null, + behaviour text, microchipped integer not null ) strict; diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..b6f13c0 --- /dev/null +++ b/src/db.rs @@ -0,0 +1,88 @@ +use rocket_db_pools::{Connection, Database}; +use sqlx::types::chrono; + +#[derive(Database)] +#[database("dolltags")] +pub struct DollTags(sqlx::SqlitePool); +pub type DollTagsDb = Connection; + +// Doll Profiles stuff +#[derive(Debug)] +pub struct DollProfile { + pub id: i64, + pub created_at: chrono::NaiveDateTime, + pub updated_at: Option, + + pub name: String, + pub pronouns: String, + pub handler_name: String, + pub handler_url: Option, + + pub kind: Option, + pub breed: Option, + pub behaviour: Option, + pub description: Option, + pub chassis_type: Option, + pub chassis_id: Option, + pub chassis_color: Option, + pub microchipped: bool, +} + +#[derive(Debug)] +pub struct CreateDollProfile { + pub id: i64, + + pub name: String, + pub pronouns: String, + pub handler_name: String, + pub handler_url: Option, + + pub kind: Option, + pub breed: Option, + pub behaviour: Option, + pub description: Option, + pub chassis_type: Option, + pub chassis_id: Option, + pub chassis_color: Option, + pub microchipped: bool, +} + +pub async fn get(mut db: DollTagsDb, ident: i64) -> sqlx::Result> { + sqlx::query_as!( + DollProfile, + r#" + select + id, name, pronouns, handler_name, handler_url, kind, breed, behaviour, description, chassis_type, chassis_id, chassis_color, + microchipped as "microchipped!: bool", + created_at as "created_at!: chrono::NaiveDateTime", updated_at as "updated_at!: Option" + from doll_profiles where id = ? + "#, + ident + ) + .fetch_optional(&mut **db).await +} + +pub async fn create(mut db: DollTagsDb, doll: CreateDollProfile) -> sqlx::Result { + sqlx::query!( + r#" + insert into doll_profiles + (id, name, pronouns, handler_name, handler_url, kind, breed, behaviour, description, chassis_type, chassis_id, chassis_color, microchipped) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + "#, + doll.id, + doll.name, + doll.pronouns, + doll.handler_name, + doll.handler_url, + doll.kind, + doll.breed, + doll.behaviour, + doll.description, + doll.chassis_type, + doll.chassis_id, + doll.chassis_color, + doll.microchipped, + ).execute(&mut **db).await?; + + Ok(doll.id) +} diff --git a/src/main.rs b/src/main.rs index 5637725..c118153 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,27 @@ #[macro_use] extern crate rocket; +use db::{DollTags, DollTagsDb}; +use regex::Regex; use rocket::form::Form; use rocket::fs::{relative, FileServer}; -use rocket_db_pools::{sqlx, Connection, Database}; +use rocket_db_pools::{sqlx, Database}; use rocket_dyn_templates::{context, Template}; use sqlx::types::chrono; -#[derive(Database)] -#[database("dolltags")] -struct DollTags(sqlx::SqlitePool); +pub mod db; + +pub fn id_public_to_db(id: &str) -> Option { + let id_re = Regex::new(r"^\d{6}$").unwrap(); + + if id_re.is_match(id) { + id.parse::().ok() + } else { + None + } +} +pub fn id_db_to_public(id: i64) -> String { + format!("{:0>6}", id) +} #[get("/")] fn index() -> Template { @@ -39,7 +52,7 @@ struct TagForm { } #[post("/register", data = "")] -async fn handle_register(mut db: Connection, tag: Form) -> Template { +async fn handle_register(mut db: DollTagsDb, tag: Form) -> Template { println!("register: {:?}", tag); let pronouns = format!( "{}/{}/{}", @@ -79,7 +92,7 @@ async fn handle_register(mut db: Connection, tag: Form) -> Te } #[get("/profile?")] -async fn show_profile(mut db: Connection, ident: &str) -> Template { +async fn show_profile(mut db: DollTagsDb, ident: &str) -> Template { #[derive(Debug)] struct Tag { pub id: i64,