finally auto-generating the offered IDs
This commit is contained in:
parent
13f208496f
commit
ab4d4f4309
3 changed files with 33 additions and 8 deletions
|
@ -18,6 +18,22 @@ pub async fn get(mut db: DollTagsDb, ident: i64) -> sqlx::Result<Option<DollProf
|
|||
.fetch_optional(&mut **db).await
|
||||
}
|
||||
|
||||
pub async fn check_ids(mut db: DollTagsDb, idents: &Vec<i64>) -> sqlx::Result<Vec<i64>> {
|
||||
let idents_sql_input = idents.iter().map(|_| "?").collect::<Vec<&str>>().join(", ");
|
||||
|
||||
let sql = format!(
|
||||
r#"select id from doll_profiles where id in ({})"#,
|
||||
idents_sql_input
|
||||
);
|
||||
let mut query = sqlx::query_scalar(&sql);
|
||||
|
||||
for ident in idents {
|
||||
query = query.bind(ident);
|
||||
}
|
||||
|
||||
query.fetch_all(&mut **db).await
|
||||
}
|
||||
|
||||
pub async fn create(mut db: DollTagsDb, doll: CreateDollProfile<'_>) -> sqlx::Result<i64> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -2,9 +2,10 @@
|
|||
extern crate rocket;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use db::doll;
|
||||
use db::doll::{self, check_ids};
|
||||
use db::schema::{CreateDollProfile, DollTags, DollTagsDb};
|
||||
use regex::Regex;
|
||||
use rng::generate_ids;
|
||||
use rocket::form::Form;
|
||||
use rocket::fs::{relative, FileServer};
|
||||
use rocket::response::Redirect;
|
||||
|
@ -37,8 +38,18 @@ fn index() -> Template {
|
|||
}
|
||||
|
||||
#[get("/register")]
|
||||
fn show_register() -> Template {
|
||||
Template::render("register", context! {})
|
||||
async fn show_register(db: DollTagsDb) -> Template {
|
||||
let mut ids_bundle = generate_ids();
|
||||
let occupied_ids = check_ids(db, &ids_bundle).await.expect("fuck");
|
||||
ids_bundle.retain(|&id| !occupied_ids.contains(&id));
|
||||
let ids = ids_bundle.iter().take(5).collect::<Vec<&i64>>();
|
||||
|
||||
Template::render(
|
||||
"register",
|
||||
context! {
|
||||
ids
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: Validation
|
||||
|
|
|
@ -34,11 +34,9 @@
|
|||
<p>Or pick one of those pre-generated ones</p>
|
||||
|
||||
<div id="pregen_ids">
|
||||
<button type="button">720-812</button>
|
||||
<button type="button">423-158</button>
|
||||
<button type="button">863-633</button>
|
||||
<button type="button">139-317</button>
|
||||
<button type="button">848-339</button>
|
||||
{% for id in ids %}
|
||||
<button type="button">{{id | pretty_id}}</button>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue