From efdf72f299da0040989c1ae8aec9ac97262cb28d Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sun, 13 Dec 2020 20:52:14 +0200 Subject: Move form creation to index file --- backend/routes/forms/index.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'backend/routes/forms/index.py') diff --git a/backend/routes/forms/index.py b/backend/routes/forms/index.py index f1df210..5cc6837 100644 --- a/backend/routes/forms/index.py +++ b/backend/routes/forms/index.py @@ -1,6 +1,7 @@ """ Return a list of all forms to authenticated users. """ +from pydantic import ValidationError from starlette.authentication import requires from starlette.requests import Request from starlette.responses import JSONResponse @@ -14,7 +15,7 @@ class FormsList(Route): List all available forms for administrator viewing. """ - name = "forms_list" + name = "forms_list_create" path = "/" @requires(["authenticated", "admin"]) @@ -31,3 +32,19 @@ class FormsList(Route): return JSONResponse( forms ) + + @requires(["authenticated", "admin"]) + async def post(self, request: Request) -> JSONResponse: + form_data = await request.json() + try: + form = Form(**form_data) + except ValidationError as e: + return JSONResponse(e.errors()) + + if await request.state.db.forms.find_one({"_id": form.id}): + return JSONResponse({ + "error": "Form with same ID already exists." + }, status_code=400) + + await request.state.db.forms.insert_one(form.dict(by_alias=True)) + return JSONResponse(form.dict()) -- cgit v1.2.3