feat: rest api to list forms
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
9e4482780d
commit
936c4babdc
2 changed files with 46 additions and 9 deletions
|
@ -32,16 +32,22 @@ pub mod routes {
|
|||
pub submit: &'static str,
|
||||
pub get_all: &'static str,
|
||||
pub delete: &'static str,
|
||||
pub get_forms_for_host: &'static str,
|
||||
}
|
||||
impl Forms {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
submit: "/api/v1/forms/submit",
|
||||
get_all: "/api/v1/forms/list",
|
||||
get_forms_for_host: "/api/v1/forms/host",
|
||||
delete: "/api/v1/forms/delete/{id}",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_forms_for_host_route(&self, host: &str) -> String {
|
||||
format!("{}?host={}", self.get_forms_for_host, host)
|
||||
}
|
||||
|
||||
pub fn get_submit(&self, host: &str, path: &str) -> String {
|
||||
format!("{}?host={}&path={}", self.submit, host, path)
|
||||
}
|
||||
|
@ -57,6 +63,7 @@ pub fn services(cfg: &mut web::ServiceConfig) {
|
|||
cfg.service(upload);
|
||||
cfg.service(list_all);
|
||||
cfg.service(delete_submission);
|
||||
cfg.service(list_all_forms);
|
||||
}
|
||||
|
||||
#[actix_web_codegen_const_routes::post(
|
||||
|
@ -131,6 +138,21 @@ async fn upload(
|
|||
Ok(HttpResponse::Ok().json(data))
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Host {
|
||||
host: String,
|
||||
}
|
||||
|
||||
#[actix_web_codegen_const_routes::get(
|
||||
path = "API_V1_ROUTES.forms.get_forms_for_host",
|
||||
wrap = "HttpAuthentication::bearer(bearerauth)"
|
||||
)]
|
||||
#[tracing::instrument(name = "Get forms belonging to hostname", skip(ctx))]
|
||||
async fn list_all_forms(ctx: AppCtx, query: web::Query<Host>) -> ServiceResult<impl Responder> {
|
||||
let forms = ctx.db.list_forms(&query.host).await?;
|
||||
Ok(HttpResponse::Ok().json(forms))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use actix_web::{
|
||||
|
@ -185,10 +207,25 @@ pub mod tests {
|
|||
let resp_err: crate::errors::ErrorToResponse = test::read_body_json(upload_json).await;
|
||||
panic!("{:?}", resp_err.error);
|
||||
}
|
||||
|
||||
assert_eq!(upload_json.status(), StatusCode::OK);
|
||||
let json: serde_json::Value = test::read_body_json(upload_json).await;
|
||||
|
||||
// get all forms for host
|
||||
let list_forms_resp = test::call_service(
|
||||
&app,
|
||||
test::TestRequest::get()
|
||||
.uri(&API_V1_ROUTES.forms.get_forms_for_host_route(host))
|
||||
.insert_header((
|
||||
header::AUTHORIZATION,
|
||||
format!("Bearer {}", ctx.settings.dash.api_key),
|
||||
))
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(list_forms_resp.status(), StatusCode::OK);
|
||||
let forms: Vec<String> = test::read_body_json(list_forms_resp).await;
|
||||
assert_eq!(forms, vec![path.to_string()]);
|
||||
|
||||
// upload url encoded
|
||||
let upload_form = test::call_service(
|
||||
&app,
|
||||
|
|
16
src/db.rs
16
src/db.rs
|
@ -116,9 +116,9 @@ impl Database {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Get forms belonging to a host
|
||||
/// Get forms belonging to a host
|
||||
pub async fn list_forms(&self, host: &str) -> ServiceResult<Vec<String>> {
|
||||
struct S{
|
||||
struct S {
|
||||
website_path: String,
|
||||
}
|
||||
let mut forms = sqlx::query_as!(
|
||||
|
@ -128,11 +128,12 @@ impl Database {
|
|||
FROM
|
||||
forms_submissions
|
||||
WHERE
|
||||
website_id = (SELECT ID FROM forms_websites WHERE hostname = $1)",
|
||||
host)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
website_id = (SELECT ID FROM forms_websites WHERE hostname = $1)",
|
||||
host
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut resp = Vec::with_capacity(forms.len());
|
||||
for f in forms.drain(0..) {
|
||||
resp.push(f.website_path);
|
||||
|
@ -288,7 +289,6 @@ mod tests {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
|
||||
assert_eq!(db.list_forms(url).await.unwrap().len(), 1);
|
||||
|
||||
let subs = db.get_form_submissions(0, url, path).await.unwrap();
|
||||
|
|
Loading…
Reference in a new issue