mirror of https://github.com/raftario/filite.git
Fixed random id generation
This commit is contained in:
parent
8da71eb0df
commit
fc79a7fa53
|
@ -186,12 +186,13 @@ macro_rules! delete {
|
||||||
/// Verify if an entry exists
|
/// Verify if an entry exists
|
||||||
macro_rules! random_id {
|
macro_rules! random_id {
|
||||||
($m:ident) => {
|
($m:ident) => {
|
||||||
use rand::Rng;
|
use rand::distributions::Distribution;
|
||||||
|
|
||||||
pub async fn random_id(pool: &actix_web::web::Data<Pool>) -> Result<i32, actix_web::Error> {
|
pub async fn random_id(pool: &actix_web::web::Data<Pool>) -> Result<i32, actix_web::Error> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
let distribution = rand::distributions::Uniform::from(0..i32::max_value());
|
||||||
loop {
|
loop {
|
||||||
let id = rng.gen();
|
let id = distribution.sample(&mut rng);
|
||||||
let pool = pool.clone();
|
let pool = pool.clone();
|
||||||
match actix_web::web::block(move || crate::queries::$m::find(id, pool)).await {
|
match actix_web::web::block(move || crate::queries::$m::find(id, pool)).await {
|
||||||
Ok(_) => continue,
|
Ok(_) => continue,
|
||||||
|
@ -646,7 +647,7 @@ pub mod texts {
|
||||||
|
|
||||||
/// Request body when PUTting texts
|
/// Request body when PUTting texts
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct PutText {
|
pub struct PutPostText {
|
||||||
pub contents: String,
|
pub contents: String,
|
||||||
pub highlight: bool,
|
pub highlight: bool,
|
||||||
}
|
}
|
||||||
|
@ -655,7 +656,7 @@ pub mod texts {
|
||||||
pub async fn put(
|
pub async fn put(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
path: web::Path<String>,
|
path: web::Path<String>,
|
||||||
body: web::Json<PutText>,
|
body: web::Json<PutPostText>,
|
||||||
pool: web::Data<Pool>,
|
pool: web::Data<Pool>,
|
||||||
identity: Identity,
|
identity: Identity,
|
||||||
password_hash: web::Data<Vec<u8>>,
|
password_hash: web::Data<Vec<u8>>,
|
||||||
|
@ -672,7 +673,7 @@ pub mod texts {
|
||||||
/// PUT a new text entry
|
/// PUT a new text entry
|
||||||
pub async fn post(
|
pub async fn post(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
body: web::Json<PutText>,
|
body: web::Json<PutPostText>,
|
||||||
pool: web::Data<Pool>,
|
pool: web::Data<Pool>,
|
||||||
identity: Identity,
|
identity: Identity,
|
||||||
password_hash: web::Data<Vec<u8>>,
|
password_hash: web::Data<Vec<u8>>,
|
||||||
|
|
Loading…
Reference in New Issue