-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new response table, and a new DB endpoint to write to it with
- Loading branch information
1 parent
de93cf2
commit 1974139
Showing
5 changed files
with
134 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Creating response table | ||
Revision ID: 0a8cb28dc397 | ||
Revises: b5c8e1cfcb42 | ||
Create Date: 2023-11-22 12:27:50.848006 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "0a8cb28dc397" | ||
down_revision: Union[str, None] = "b5c8e1cfcb42" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table("response") | ||
op.add_column( | ||
"response", | ||
sa.Column("interaction_id", sa.String(), primary_key=True, nullable=False), | ||
) | ||
op.add_column("response", sa.Column("response_id", sa.String(), nullable=False)) | ||
op.add_column("response", sa.Column("survey_id", sa.String(), nullable=False)) | ||
op.add_column("response", sa.Column("session_id", sa.String(), nullable=False)) | ||
op.add_column("response", sa.Column("dist", sa.String(), nullable=False)) | ||
# Add foreign key to interaction | ||
|
||
|
||
def downgrade() -> None: | ||
# Drop foreign key from interaction | ||
op.drop_column("response", "interaction_id") | ||
op.drop_column("response", "response_id") | ||
op.drop_column("response", "survey_id") | ||
op.drop_column("response", "session_id") | ||
op.drop_column("response", "dist") | ||
op.drop_table("response") |
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,61 @@ | ||
import logging | ||
|
||
import fastapi | ||
from fastapi import responses | ||
from pydantic import BaseModel | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from gdrive.database import database, models, crud | ||
|
||
log = logging.getLogger(__name__) | ||
router = fastapi.APIRouter() | ||
|
||
|
||
class CreateResponseRequest(BaseModel): | ||
interactionId: str | ||
responseId: str | ||
sessionId: str | ||
surveyId: str | ||
dist: str | ||
|
||
|
||
@router.post("/database") | ||
async def database_info(): | ||
if database.SessionLocal(): | ||
log.info(database.engine) | ||
return responses.JSONResponse( | ||
status_code=202, content="Database connected as %s" % (database.engine.name) | ||
) | ||
|
||
return responses.JSONResponse(status_code=404, content="No database session found.") | ||
|
||
|
||
@router.post("/database/response") | ||
async def process_response_header(req: CreateResponseRequest): | ||
try: | ||
# Create new response record | ||
if database.SessionLocal(): | ||
record = create_response(req) | ||
log.info("New response (interaction_id: %s)" % (record.interaction_id)) | ||
return responses.JSONResponse( | ||
status_code=202, content="Successfully created new response record" | ||
) | ||
except SQLAlchemyError as sql_err: | ||
log.error(sql_err) | ||
return responses.JSONResponse( | ||
status_code=500, content="Error trying to complete response create" | ||
) | ||
|
||
return responses.JSONResponse(status_code=404, content="No database session found.") | ||
|
||
|
||
def create_response(resp_req: CreateResponseRequest): | ||
return crud.create_response( | ||
models.ResponseModel( | ||
interaction_id=resp_req.interactionId, | ||
response_id=resp_req.responseId, | ||
session_id=resp_req.sessionId, | ||
survey_id=resp_req.surveyId, | ||
dist=resp_req.dist, | ||
) | ||
) |
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