feat: list forms for hostname REST API

This commit is contained in:
Aravinth Manivannan 2022-12-31 01:26:03 +05:30
parent 0e08a294fc
commit efc8ce5a27
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -27,13 +27,15 @@ pub mod routes {
pub struct Forms {
pub list_submissions: &'static str,
pub delete_submission: &'static str,
pub get_forms_for_host: &'static str,
}
impl Forms {
pub const fn new() -> Self {
Self {
list_submissions: "/api/v1/forms/list",
list_submissions: "/api/v1/forms/submissions/list",
delete_submission: "/api/v1/forms/delete/{id}",
get_forms_for_host: "/api/v1/forms/list",
}
}
@ -41,6 +43,10 @@ pub mod routes {
format!("{}?page={}", self.list_submissions, page)
}
pub fn get_forms_for_host_route(&self, host: &str) -> String {
format!("{}?host={}", self.get_forms_for_host, host)
}
pub fn get_delete(&self, id: usize, host: &str, path: &str) -> String {
let del = self.delete_submission.replace("{id}", &id.to_string());
format!("{}?host={}&path={}", del, host, path)
@ -88,9 +94,30 @@ async fn delete_form_submission(
Ok(HttpResponse::Ok())
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Host {
pub host: String,
}
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.forms.get_forms_for_host",
wrap = "get_auth_middleware()"
)]
#[tracing::instrument(name = "Get forms belonging to hostname", skip(ctx, id))]
async fn list_all_forms(
id: Identity,
ctx: AppCtx,
query: web::Query<Host>,
) -> ServiceResult<impl Responder> {
let owner = id.identity().unwrap();
let forms = ctx.get_all_forms_for_host(&owner, &query.host).await?;
Ok(HttpResponse::Ok().json(forms))
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(list_submission);
cfg.service(delete_form_submission);
cfg.service(list_all_forms);
}
#[cfg(test)]
@ -129,6 +156,19 @@ mod tests {
.await
.unwrap();
// get all forms for host
let list_forms_resp = test::call_service(
&app,
test::TestRequest::get()
.uri(&V1_API_ROUTES.forms.get_forms_for_host_route(&page.domain))
.cookie(cookies.clone())
.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![site_info.path.to_string()]);
// list subs using REST API
let list_form_submissions = test::call_service(
&app,