moved to clean separated sqlx stuff and simpler structs

This commit is contained in:
Artemis 2025-01-19 17:07:11 +01:00
parent 2c248c0d70
commit 6ec0bcb6c9
7 changed files with 193 additions and 145 deletions

1
.env Normal file
View file

@ -0,0 +1 @@
DATABASE_URL="sqlite://dolltags.sqlite"

45
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'dolltags'",
"cargo": {
"args": [
"build",
"--bin=dolltags",
"--package=dolltags"
],
"filter": {
"name": "dolltags",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'dolltags'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=dolltags",
"--package=dolltags"
],
"filter": {
"name": "dolltags",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

View file

@ -1,88 +0,0 @@
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)
}

44
src/db/doll.rs Normal file
View file

@ -0,0 +1,44 @@
use crate::db::schema::DollProfile;
use sqlx::types::chrono;
use super::schema::{CreateDollProfile, DollTagsDb};
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)
}

2
src/db/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod doll;
pub mod schema;

48
src/db/schema.rs Normal file
View file

@ -0,0 +1,48 @@
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<'a> {
pub id: i64,
pub name: &'a str,
pub pronouns: &'a str,
pub handler_name: &'a str,
pub handler_url: Option<&'a str>,
pub kind: Option<&'a str>,
pub breed: Option<&'a str>,
pub behaviour: Option<&'a str>,
pub description: Option<&'a str>,
pub chassis_type: Option<&'a str>,
pub chassis_id: Option<&'a str>,
pub chassis_color: Option<&'a str>,
pub microchipped: bool,
}

View file

@ -1,12 +1,12 @@
#[macro_use]
extern crate rocket;
use db::{DollTags, DollTagsDb};
use db::doll;
use db::schema::{CreateDollProfile, DollTags, DollTagsDb};
use regex::Regex;
use rocket::form::Form;
use rocket::fs::{relative, FileServer};
use rocket_db_pools::{sqlx, Database};
use rocket_db_pools::Database;
use rocket_dyn_templates::{context, Template};
use sqlx::types::chrono;
pub mod db;
@ -35,80 +35,76 @@ fn show_register() -> Template {
// TODO: Validation
#[derive(Debug, FromForm)]
struct TagForm {
pub ident: String,
struct TagForm<'a> {
pub ident: &'a str,
pub name: String,
pub pronoun_subject: String,
pub pronoun_object: String,
pub pronoun_possessive: String,
pub name: &'a str,
pub pronoun_subject: &'a str,
pub pronoun_object: &'a str,
pub pronoun_possessive: &'a str,
pub handler_name: String,
pub handler_link: String,
pub handler_name: &'a str,
pub handler_link: &'a str,
pub kind: String,
pub breed: String,
pub behaviour: String,
pub kind: &'a str,
pub breed: &'a str,
pub behaviour: &'a str,
}
#[post("/register", data = "<tag>")]
async fn handle_register(mut db: DollTagsDb, tag: Form<TagForm>) -> Template {
async fn handle_register(db: DollTagsDb, tag: Form<TagForm<'_>>) -> Template {
println!("register: {:?}", tag);
let id = id_public_to_db(&tag.ident).expect("wrong fmt");
let pronouns = format!(
"{}/{}/{}",
tag.pronoun_subject, tag.pronoun_object, tag.pronoun_possessive
);
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
(?,
?, ?, ?, ?,
?, ?, ?,
?, ?, ?, ?, ?)"#,
tag.ident,
tag.name,
pronouns,
tag.handler_name,
tag.handler_link,
tag.kind,
tag.breed,
tag.behaviour,
"",
"",
"",
"",
1
doll::create(
db,
CreateDollProfile {
id,
name: tag.name,
pronouns: pronouns.as_str(),
handler_name: tag.handler_name,
handler_url: if tag.handler_link.len() != 0 {
Some(tag.handler_link)
} else {
None
},
kind: if tag.kind.len() != 0 {
Some(tag.kind)
} else {
None
},
breed: if tag.breed.len() != 0 {
Some(tag.breed)
} else {
None
},
behaviour: if tag.behaviour.len() != 0 {
Some(tag.behaviour)
} else {
None
},
description: None,
chassis_type: None,
chassis_id: None,
chassis_color: None,
microchipped: false,
},
)
.execute(&mut **db)
.await
.expect("fuck");
Template::render("register", context! {})
}
#[get("/profile?<ident>")]
async fn show_profile(mut db: DollTagsDb, ident: &str) -> Template {
#[derive(Debug)]
struct Tag {
pub id: i64,
pub created_at: chrono::NaiveDateTime,
}
let profile = sqlx::query_as!(
Tag,
r#"select id, created_at as "created_at!: chrono::NaiveDateTime" from doll_profiles where id = ?"#,
423158
)
.fetch_one(&mut **db)
.await
.expect("fuck");
async fn show_profile(db: DollTagsDb, ident: &str) -> Template {
let profile = doll::get(db, 423158).await.expect("fuck");
println!("{:?}", profile);
Template::render(
"show_profile",
context! {