cleaner tag id

This commit is contained in:
Artemis 2025-01-23 18:33:12 +01:00
parent 690d9b043d
commit b4c02a685d
4 changed files with 18 additions and 3 deletions

1
Cargo.lock generated
View file

@ -458,6 +458,7 @@ dependencies = [
"rocket",
"rocket_db_pools",
"rocket_dyn_templates",
"serde_json",
"sqlx",
]

View file

@ -15,3 +15,4 @@ sqlx = { version = "0.7", default-features = false, features = [
chrono = { version = "0.4", features = ["serde"] }
regex = "1.11"
rand = "0.8"
serde_json = "1.0"

View file

@ -1,5 +1,7 @@
#[macro_use]
extern crate rocket;
use std::collections::HashMap;
use db::doll;
use db::schema::{CreateDollProfile, DollTags, DollTagsDb};
use regex::Regex;
@ -7,7 +9,9 @@ use rocket::form::Form;
use rocket::fs::{relative, FileServer};
use rocket::response::Redirect;
use rocket_db_pools::Database;
use rocket_dyn_templates::tera::try_get_value;
use rocket_dyn_templates::{context, Template};
use serde_json::{to_value, Value};
pub mod db;
pub mod rng;
@ -22,7 +26,9 @@ pub fn id_public_to_db(id: &str) -> Option<i64> {
}
}
pub fn id_db_to_public(id: i64) -> String {
format!("{:0>6}", id)
let first = id / 1000;
let second = id % 1000;
format!("{:0>3}-{:0>3}", first, second)
}
#[get("/")]
@ -122,7 +128,14 @@ async fn show_profile(db: DollTagsDb, ident: &str) -> Result<Template, Redirect>
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(Template::fairing())
.attach(Template::custom(|engines| {
engines
.tera
.register_filter("pretty_id", |v: &Value, _: &HashMap<String, Value>| {
let value = try_get_value!("pretty_id", "value", i64, v);
Ok(to_value(id_db_to_public(value)).unwrap())
});
}))
.attach(DollTags::init())
.mount("/assets", FileServer::from(relative!("/assets")))
.mount(

View file

@ -4,7 +4,7 @@
<section class="raised profile">
<header>
<p class="ident">
Tag ID <span class="id">{{profile.id}}</span><br />
Tag ID <span class="id">{{profile.id | pretty_id}}</span><br />
{% if profile.microchipped %}
Microchipped
{% else %}