Skip to content

Commit

Permalink
Standardized the Replacement API return data schema
Browse files Browse the repository at this point in the history
  • Loading branch information
brystmar committed Jan 2, 2021
1 parent aaa34b4 commit 5347f25
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions .idea/breadsheet.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from backend.config import Config, local
from backend.functions import generate_new_id
from datetime import datetime, timedelta
from operator import attrgetter
# from operator import attrgetter
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute, UTCDateTimeAttribute, NumberAttribute, \
MapAttribute, ListAttribute, BooleanAttribute
Expand Down Expand Up @@ -236,6 +236,8 @@ class Meta:
if local: # Use the local DynamoDB instance when running locally
host = 'http://localhost:8008'

# TODO: Add an id attribute to simplify identifying each record
# id = UnicodeAttribute() <-- shouldn't this be the primary key?
scope = UnicodeAttribute(hash_key=True)
old = UnicodeAttribute(range_key=True)
new = UnicodeAttribute()
Expand All @@ -250,5 +252,12 @@ def to_dict(self) -> dict:
def __init__(self, **kwargs):
super().__init__(**kwargs)

def __getitem__(self, scope):
try:
return self.scope
except TypeError as e:
logger.error(f"Unsupported __getitem__ request: {e}")
raise e

def __repr__(self) -> str:
return f'<Replacement Text | scope: {self.scope}, old: {self.old}, new: {self.new}>'
17 changes: 14 additions & 3 deletions backend/replacement_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

class ReplacementCollectionApi(Resource):
"""
Endpoints: /api/v1/replacements/<scope>,
Endpoints: /api/v1/replacements/all,
/api/v1/replacements/<scope>,
/api/v1/replacements/<scope>/<old_value>
"""

Expand Down Expand Up @@ -41,9 +42,19 @@ def get(self, scope='all') -> json:
logger.debug(f"{error_msg}\n{e}")
return {'message': 'Error', 'data': error_msg}, 404

output = []
ingredients = []
directions = []
for i in items:
output.append(i.to_dict())
if i['scope'] == 'ingredients':
ingredients.append(i.to_dict())
if i['scope'] == 'directions':
directions.append(i.to_dict())

output = {}
if ingredients:
output['ingredients'] = ingredients
if directions:
output['directions'] = directions

logger.debug(f"End of ReplacementCollectionApi.get({scope})")
return {'message': 'Success', 'data': output}, 200
Expand Down

0 comments on commit 5347f25

Please sign in to comment.