some cleanup and progress on solidifying the DB

This commit is contained in:
Artémis 2025-01-11 17:18:25 +01:00
parent 13e7e98b8c
commit 2c248c0d70
5 changed files with 117 additions and 14 deletions

1
Cargo.lock generated
View file

@ -449,6 +449,7 @@ dependencies = [
name = "dolltags"
version = "0.1.0"
dependencies = [
"regex",
"rocket",
"rocket_db_pools",
"rocket_dyn_templates",

View file

@ -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"

View file

@ -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;

88
src/db.rs Normal file
View file

@ -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<DollTags>;
// Doll Profiles stuff
#[derive(Debug)]
pub struct DollProfile {
pub id: i64,
pub created_at: chrono::NaiveDateTime,
pub updated_at: Option<chrono::NaiveDateTime>,
pub name: String,
pub pronouns: String,
pub handler_name: String,
pub handler_url: Option<String>,
pub kind: Option<String>,
pub breed: Option<String>,
pub behaviour: Option<String>,
pub description: Option<String>,
pub chassis_type: Option<String>,
pub chassis_id: Option<String>,
pub chassis_color: Option<String>,
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<String>,
pub kind: Option<String>,
pub breed: Option<String>,
pub behaviour: Option<String>,
pub description: Option<String>,
pub chassis_type: Option<String>,
pub chassis_id: Option<String>,
pub chassis_color: Option<String>,
pub microchipped: bool,
}
pub async fn get(mut db: DollTagsDb, ident: i64) -> sqlx::Result<Option<DollProfile>> {
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<chrono::NaiveDateTime>"
from doll_profiles where id = ?
"#,
ident
)
.fetch_optional(&mut **db).await
}
pub async fn create(mut db: DollTagsDb, doll: CreateDollProfile) -> sqlx::Result<i64> {
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)
}

View file

@ -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<i64> {
let id_re = Regex::new(r"^\d{6}$").unwrap();
if id_re.is_match(id) {
id.parse::<i64>().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 = "<tag>")]
async fn handle_register(mut db: Connection<DollTags>, tag: Form<TagForm>) -> Template {
async fn handle_register(mut db: DollTagsDb, tag: Form<TagForm>) -> Template {
println!("register: {:?}", tag);
let pronouns = format!(
"{}/{}/{}",
@ -79,7 +92,7 @@ async fn handle_register(mut db: Connection<DollTags>, tag: Form<TagForm>) -> Te
}
#[get("/profile?<ident>")]
async fn show_profile(mut db: Connection<DollTags>, ident: &str) -> Template {
async fn show_profile(mut db: DollTagsDb, ident: &str) -> Template {
#[derive(Debug)]
struct Tag {
pub id: i64,