feat: get form submissions and include hostname and webpage path in
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
query while submitting forms
This commit is contained in:
parent
777cea74e3
commit
49f48e69c0
1 changed files with 87 additions and 15 deletions
|
@ -16,9 +16,10 @@
|
|||
*/
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix_web::{web, HttpRequest, HttpResponse, Responder};
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::db::FormSubmission;
|
||||
use crate::errors::*;
|
||||
use crate::AppCtx;
|
||||
use crate::*;
|
||||
|
@ -28,18 +29,25 @@ pub mod routes {
|
|||
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Forms {
|
||||
pub submit: &'static str,
|
||||
pub get_all: &'static str,
|
||||
}
|
||||
impl Forms {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
submit: "/api/v1/forms/submit",
|
||||
get_all: "/api/v1/forms/list",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_submit(&self, host: &str, path: &str) -> String {
|
||||
format!("{}?host={}&path={}", self.submit, host, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(upload);
|
||||
cfg.service(list_all);
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
|
@ -61,19 +69,59 @@ impl FormDType {
|
|||
|
||||
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,
|
||||
}
|
||||
|
||||
impl From<FormSubmission> for FormSubmissionResp {
|
||||
fn from(f: FormSubmission) -> Self {
|
||||
Self {
|
||||
value: f.value,
|
||||
time: f.time.unix_timestamp(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web_codegen_const_routes::post(path = "API_V1_ROUTES.forms.get_all")]
|
||||
async fn list_all(
|
||||
ctx: AppCtx,
|
||||
payload: web::Json<Table>,
|
||||
page: web::Query<Page>,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
let mut subs = ctx
|
||||
.db
|
||||
.get_form_submissions(page.page, &payload.host, &payload.path)
|
||||
.await?;
|
||||
let mut resp: Vec<FormSubmissionResp> = Vec::with_capacity(subs.len());
|
||||
for sub in subs.drain(0..) {
|
||||
resp.push(sub.into());
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(resp))
|
||||
}
|
||||
|
||||
#[actix_web_codegen_const_routes::post(path = "API_V1_ROUTES.forms.submit")]
|
||||
async fn upload(
|
||||
req: HttpRequest,
|
||||
ctx: AppCtx,
|
||||
query: web::Query<Table>,
|
||||
payload: web::Either<web::Json<serde_json::Value>, web::Form<FormValue>>,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
let host = {
|
||||
let c = req.connection_info();
|
||||
c.host().to_owned()
|
||||
};
|
||||
let path = req.uri();
|
||||
let host = &query.host;
|
||||
let path = &query.path;
|
||||
|
||||
//ctx.db.add_site(host).await.unwrap();
|
||||
let data = match payload {
|
||||
web::Either::Left(json) => json.into_inner(),
|
||||
web::Either::Right(form) => {
|
||||
|
@ -86,12 +134,10 @@ async fn upload(
|
|||
};
|
||||
|
||||
ctx.db
|
||||
.add_form_submission(&data, &host, path.path())
|
||||
.add_form_submission(&data, &host, path)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("{:?}", data);
|
||||
//ctx.gitea.report(&payload).await;
|
||||
Ok(HttpResponse::Ok().json(data))
|
||||
}
|
||||
|
||||
|
@ -104,7 +150,7 @@ pub mod tests {
|
|||
|
||||
use super::*;
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize, PartialEq)]
|
||||
#[derive(Serialize, Clone, Debug, Deserialize, PartialEq)]
|
||||
struct Foo {
|
||||
foo: String,
|
||||
num: f64,
|
||||
|
@ -124,6 +170,11 @@ pub mod tests {
|
|||
)
|
||||
.await;
|
||||
|
||||
let host = "localhost:8008";
|
||||
let path = "/foo";
|
||||
let upload_path = API_V1_ROUTES.forms.get_submit(host, path);
|
||||
println!("{upload_path}");
|
||||
|
||||
let foo = Foo {
|
||||
foo: "Foo".into(),
|
||||
num: 2.33,
|
||||
|
@ -133,7 +184,7 @@ pub mod tests {
|
|||
let upload_json = test::call_service(
|
||||
&app,
|
||||
test::TestRequest::post()
|
||||
.uri(API_V1_ROUTES.forms.submit)
|
||||
.uri(&upload_path)
|
||||
.set_json(&foo)
|
||||
.to_request(),
|
||||
)
|
||||
|
@ -150,8 +201,7 @@ pub mod tests {
|
|||
let upload_form = test::call_service(
|
||||
&app,
|
||||
test::TestRequest::post()
|
||||
.uri(API_V1_ROUTES.forms.submit)
|
||||
.uri(API_V1_ROUTES.forms.submit)
|
||||
.uri(&upload_path)
|
||||
.set_form(&foo)
|
||||
.to_request(),
|
||||
)
|
||||
|
@ -164,5 +214,27 @@ pub mod tests {
|
|||
assert_eq!(upload_form.status(), StatusCode::OK);
|
||||
let form: serde_json::Value = test::read_body_json(upload_form).await;
|
||||
assert_eq!(form, json);
|
||||
|
||||
let get_sub_route = format!("{}?page={}", API_V1_ROUTES.forms.get_all, 0);
|
||||
let payload = Table {
|
||||
host: host.into(),
|
||||
path: path.to_owned(),
|
||||
};
|
||||
println!("{get_sub_route}");
|
||||
let get_subs = test::call_service(
|
||||
&app,
|
||||
test::TestRequest::post()
|
||||
.set_json(&payload)
|
||||
.uri(&get_sub_route)
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
println!("{:?}", get_subs);
|
||||
assert_eq!(get_subs.status(), StatusCode::OK);
|
||||
let subs: Vec<FormSubmissionResp> = test::read_body_json(get_subs).await;
|
||||
let foo_as_json_value = serde_json::to_value(&foo).unwrap();
|
||||
assert_eq!(subs.len(), 2);
|
||||
assert_eq!(subs[0].value.as_ref().unwrap(), &foo_as_json_value);
|
||||
assert_eq!(subs[1].value.as_ref().unwrap(), &foo_as_json_value);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue