diff --git a/Cargo.lock b/Cargo.lock index 18b51ee..f5384bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,6 +784,7 @@ dependencies = [ "derive_more", "futures-util", "lazy_static", + "libforms", "pretty_env_logger", "serde", "serde_json", @@ -1146,6 +1147,14 @@ version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "libforms" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "linked-hash-map" version = "0.5.6" diff --git a/Cargo.toml b/Cargo.toml index f7f1c57..7a9a140 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ serde_json = { version ="1", features = ["raw_value"]} sqlx = { version = "0.6.2", features = ["runtime-actix-rustls", "postgres", "time", "offline", "json"] } clap = { vesrion = "3.2.20", features = ["derive"]} tracing = { version = "0.1.37", features = ["log"] } +libforms = { path = "./env/libforms" } [build-dependencies] diff --git a/src/api/v1/forms.rs b/src/api/v1/forms.rs index 039b142..aa51c54 100644 --- a/src/api/v1/forms.rs +++ b/src/api/v1/forms.rs @@ -18,6 +18,7 @@ use std::collections::HashMap; use actix_web::{web, HttpResponse, Responder}; use actix_web_httpauth::middleware::HttpAuthentication; +use libforms::*; use serde::{Deserialize, Serialize}; use crate::db::FormSubmission; @@ -61,53 +62,6 @@ pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(delete_submission); } -#[derive(Deserialize, Serialize, Debug)] -#[serde(untagged)] -enum FormDType { - Num(f64), - Str(String), -} - -impl FormDType { - fn apply_types(&mut self) { - if let Self::Str(data) = self { - if let Ok(num) = data.parse::() { - *self = Self::Num(num); - } - } - } -} - -pub type FormValue = HashMap; - -#[derive(Serialize, Deserialize, Clone, Debug)] -struct Page { - page: usize, -} - -#[derive(Serialize, Deserialize, Clone, Debug)] -pub struct Table { - pub host: String, - pub path: String, -} - -#[derive(Serialize, Deserialize, Clone, Debug, Default)] -pub struct FormSubmissionResp { - pub value: Option, - pub time: i64, - pub id: usize, -} - -impl From for FormSubmissionResp { - fn from(f: FormSubmission) -> Self { - Self { - value: f.value, - time: f.time.unix_timestamp(), - id: f.id as usize, - } - } -} - #[actix_web_codegen_const_routes::post( path = "API_V1_ROUTES.forms.delete", wrap = "HttpAuthentication::bearer(bearerauth)" @@ -124,6 +78,11 @@ async fn delete_submission( Ok(HttpResponse::Ok()) } +#[derive(Serialize, Deserialize, Clone, Debug)] +struct Page { + page: usize, +} + #[actix_web_codegen_const_routes::post( path = "API_V1_ROUTES.forms.get_all", wrap = "HttpAuthentication::bearer(bearerauth)" @@ -140,7 +99,7 @@ async fn list_all( .await?; let mut resp: Vec = Vec::with_capacity(subs.len()); for sub in subs.drain(0..) { - resp.push(sub.into()); + resp.push(sub.to_resp()); } Ok(HttpResponse::Ok().json(resp)) diff --git a/src/db.rs b/src/db.rs index de774b7..886a2c3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -16,6 +16,7 @@ */ use std::str::FromStr; +use libforms::FormSubmissionResp; use sqlx::postgres::PgPoolOptions; use sqlx::types::time::OffsetDateTime; //use sqlx::types::Json; @@ -203,6 +204,16 @@ pub struct FormSubmission { pub time: OffsetDateTime, } +impl FormSubmission { + pub fn to_resp(self) -> FormSubmissionResp { + FormSubmissionResp { + value: self.value, + time: self.time.unix_timestamp(), + id: self.id as usize, + } + } +} + fn now_unix_time_stamp() -> OffsetDateTime { OffsetDateTime::now_utc() }