generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement forms API endpoints, complete with API schema, and simplify…
… controller logic. Also fix small NoneType bug in AWSMockService.
- Loading branch information
1 parent
b64c04e
commit 8d5ce36
Showing
10 changed files
with
228 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,18 @@ | ||
from sqlalchemy import func, select, delete | ||
from openapi_server.repositories.forms import FormsRepository | ||
from openapi_server.models.database import DataAccessLayer | ||
|
||
from openapi_server.models.database import DataAccessLayer, Form, Response | ||
from openapi_server.models.schema import form_schema, response_schema, ResponseSchema, Response | ||
def create_form(body): | ||
forms_repo = FormsRepository(DataAccessLayer.session()) | ||
|
||
def create_form(form_json): | ||
new_form = form_schema.load(form_json) | ||
with DataAccessLayer.session() as session: | ||
session.add(new_form) | ||
return form_schema.dump(new_form), 200 | ||
form_id = forms_repo.add_form(body) | ||
form = forms_repo.get_form_json(form_id) | ||
if form: | ||
return form, 200 | ||
return {}, 404 | ||
|
||
def get_form(form_id): | ||
form = None | ||
with DataAccessLayer.session() as session: | ||
form = session.get(Form, form_id) | ||
forms_repo = FormsRepository(DataAccessLayer.session()) | ||
form = forms_repo.get_form_json(form_id) | ||
if form: | ||
return form_schema.dump(form), 200 | ||
return dict(), 404 | ||
|
||
def update_responses(response_json, user_id): | ||
for response in response_json: | ||
response["user_id"] = user_id | ||
|
||
with DataAccessLayer.session() as session: | ||
new_responses = response_schema.load(response_json) | ||
field_ids = [r.field_id for r in new_responses] | ||
session.execute( | ||
delete(Response)\ | ||
.where(Response.user_id == user_id)\ | ||
.where(Response.field_id.in_(field_ids)) | ||
) | ||
for new_response in new_responses: | ||
session.add(new_response) | ||
|
||
def get_responses(form_id, user_id): | ||
with DataAccessLayer.session() as session: | ||
form = session.get(Form, form_id) | ||
field_ids = set() | ||
for group in form.field_groups: | ||
for field in group.fields: | ||
field_ids.add(field.field_id) | ||
|
||
responses = session.execute( | ||
select(Response).\ | ||
where(Response.user_id == user_id).\ | ||
where(Response.field_id.in_(field_ids)) | ||
) | ||
|
||
return response_schema.dump(responses) | ||
return form, 200 | ||
return f"Form with id {form_id} does not exist.", 404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from openapi_server.repositories.forms import FormsRepository | ||
from openapi_server.repositories.user_repo import UserRepository | ||
from openapi_server.models.database import DataAccessLayer | ||
|
||
def update_responses(body, form_id, token_info): | ||
with DataAccessLayer.session() as session: | ||
user_repo = UserRepository(session) | ||
forms_repo = FormsRepository(session) | ||
user = user_repo.get_user(token_info['Username']) | ||
|
||
form = forms_repo.get_form(form_id) | ||
if not form: | ||
return f"Form with id {form_id} does not exist.", 404 | ||
|
||
valid_field_ids = form.get_field_ids() | ||
for response in body: | ||
response["user_id"] = user.id | ||
if response["field_id"] not in valid_field_ids: | ||
return f"Form {form_id} does not contain field id {response['field_id']}", 400 | ||
|
||
forms_repo.add_user_responses(user.id, body) | ||
|
||
return {}, 204 | ||
|
||
def get_responses(form_id, token_info): | ||
with DataAccessLayer.session() as session: | ||
user_repo = UserRepository(session) | ||
forms_repo = FormsRepository(session) | ||
|
||
form = forms_repo.get_form_json(form_id) | ||
if not form: | ||
return f"Form with id {form_id} does not exist.", 404 | ||
|
||
user = user_repo.get_user(token_info['Username']) | ||
responses = forms_repo.get_user_responses(user.id, form_id) | ||
if responses: | ||
return responses, 200 | ||
return [], 202 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
get: | ||
summary: Get a form by ID | ||
operationId: get_form | ||
parameters: | ||
- name: form_id | ||
in: path | ||
required: true | ||
schema: | ||
type: integer | ||
responses: | ||
'200': | ||
description: Form data retrieved successfully | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../schemas/_index.yaml#/Form' | ||
'404': | ||
description: Form not found | ||
'401': | ||
description: Authentication error | ||
x-openapi-router-controller: openapi_server.controllers.forms_controller | ||
security: | ||
- jwt: ["secret"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
post: | ||
summary: Create a new form | ||
operationId: create_form | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../schemas/_index.yaml#/Form' | ||
responses: | ||
'200': | ||
description: Successfully created a form | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../schemas/_index.yaml#/Form' | ||
'401': | ||
description: Authentication error | ||
'404': | ||
description: Not found | ||
x-openapi-router-controller: openapi_server.controllers.forms_controller | ||
security: | ||
- jwt: ["secret"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
put: | ||
summary: Update user responses for a specific form | ||
operationId: update_responses | ||
parameters: | ||
- name: form_id | ||
in: path | ||
required: true | ||
schema: | ||
type: integer | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../schemas/_index.yaml#/FormResponses' | ||
responses: | ||
'401': | ||
description: Authentication error | ||
'204': | ||
description: Responses updated successfully | ||
x-openapi-router-controller: openapi_server.controllers.responses_controller | ||
security: | ||
- jwt: ["secret"] | ||
get: | ||
summary: Get responses for a specific form and user | ||
operationId: get_responses | ||
parameters: | ||
- name: form_id | ||
in: path | ||
required: true | ||
schema: | ||
type: integer | ||
responses: | ||
'200': | ||
description: Successfully retrieved responses | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '../schemas/_index.yaml#/FormResponses' | ||
'401': | ||
description: Authentication error | ||
'404': | ||
description: Responses not found | ||
x-openapi-router-controller: openapi_server.controllers.responses_controller | ||
security: | ||
- jwt: ["secret"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters