feat: use objects from libforms for uniformity
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2022-12-29 13:32:48 +05:30
parent da3d3635b5
commit 7e50f9be0d
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 28 additions and 48 deletions

9
Cargo.lock generated
View file

@ -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"

View file

@ -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]

View file

@ -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::<f64>() {
*self = Self::Num(num);
}
}
}
}
pub type FormValue = HashMap<String, FormDType>;
#[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<serde_json::Value>,
pub time: i64,
pub id: usize,
}
impl From<FormSubmission> 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<FormSubmissionResp> = 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))

View file

@ -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()
}