feat: rest api to list forms
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Aravinth Manivannan 2022-12-30 19:08:29 +05:30
parent 9e4482780d
commit 936c4babdc
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 46 additions and 9 deletions

View File

@ -32,16 +32,22 @@ pub mod routes {
pub submit: &'static str, pub submit: &'static str,
pub get_all: &'static str, pub get_all: &'static str,
pub delete: &'static str, pub delete: &'static str,
pub get_forms_for_host: &'static str,
} }
impl Forms { impl Forms {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
submit: "/api/v1/forms/submit", submit: "/api/v1/forms/submit",
get_all: "/api/v1/forms/list", get_all: "/api/v1/forms/list",
get_forms_for_host: "/api/v1/forms/host",
delete: "/api/v1/forms/delete/{id}", 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 { pub fn get_submit(&self, host: &str, path: &str) -> String {
format!("{}?host={}&path={}", self.submit, host, path) format!("{}?host={}&path={}", self.submit, host, path)
} }
@ -57,6 +63,7 @@ pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(upload); cfg.service(upload);
cfg.service(list_all); cfg.service(list_all);
cfg.service(delete_submission); cfg.service(delete_submission);
cfg.service(list_all_forms);
} }
#[actix_web_codegen_const_routes::post( #[actix_web_codegen_const_routes::post(
@ -131,6 +138,21 @@ async fn upload(
Ok(HttpResponse::Ok().json(data)) 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)] #[cfg(test)]
pub mod tests { pub mod tests {
use actix_web::{ use actix_web::{
@ -185,10 +207,25 @@ pub mod tests {
let resp_err: crate::errors::ErrorToResponse = test::read_body_json(upload_json).await; let resp_err: crate::errors::ErrorToResponse = test::read_body_json(upload_json).await;
panic!("{:?}", resp_err.error); panic!("{:?}", resp_err.error);
} }
assert_eq!(upload_json.status(), StatusCode::OK); assert_eq!(upload_json.status(), StatusCode::OK);
let json: serde_json::Value = test::read_body_json(upload_json).await; 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 // upload url encoded
let upload_form = test::call_service( let upload_form = test::call_service(
&app, &app,

View File

@ -116,9 +116,9 @@ impl Database {
Ok(()) Ok(())
} }
/// Get forms belonging to a host /// Get forms belonging to a host
pub async fn list_forms(&self, host: &str) -> ServiceResult<Vec<String>> { pub async fn list_forms(&self, host: &str) -> ServiceResult<Vec<String>> {
struct S{ struct S {
website_path: String, website_path: String,
} }
let mut forms = sqlx::query_as!( let mut forms = sqlx::query_as!(
@ -128,11 +128,12 @@ impl Database {
FROM FROM
forms_submissions forms_submissions
WHERE WHERE
website_id = (SELECT ID FROM forms_websites WHERE hostname = $1)", website_id = (SELECT ID FROM forms_websites WHERE hostname = $1)",
host) host
.fetch_all(&self.pool) )
.await .fetch_all(&self.pool)
.unwrap(); .await
.unwrap();
let mut resp = Vec::with_capacity(forms.len()); let mut resp = Vec::with_capacity(forms.len());
for f in forms.drain(0..) { for f in forms.drain(0..) {
resp.push(f.website_path); resp.push(f.website_path);
@ -288,7 +289,6 @@ mod tests {
.await .await
.unwrap(); .unwrap();
assert_eq!(db.list_forms(url).await.unwrap().len(), 1); assert_eq!(db.list_forms(url).await.unwrap().len(), 1);
let subs = db.get_form_submissions(0, url, path).await.unwrap(); let subs = db.get_form_submissions(0, url, path).await.unwrap();