Skip to content

Commit

Permalink
#1: Allow single dictionary in patch function
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Jun 20, 2019
1 parent fc6b12e commit 07d29d4
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.orm import sessionmaker

from common.constants import Constants
from common.exceptions import MissingRecordError, BadFilterError
from common.exceptions import MissingRecordError, BadFilterError, BadRequestError


def get_record_by_id(table, id):
Expand Down Expand Up @@ -179,15 +179,24 @@ def patch_entities(table, json_list):
"""
Update one or more rows in the given table, from the given list containing json. Each entity must contain its ID
:param table: The table of the entities
:param json_list: the list of updated values
:param json_list: the list of updated values or a dictionary
:return: The list of updated rows.
"""
results = []
for entity in json_list:
for key in entity:
if type(json_list) == dict:
for key in json_list:
if key.upper() == "ID":
update_row_from_id(table, entity[key], entity)
result = get_row_by_id(table, entity[key])
update_row_from_id(table, json_list[key], json_list)
result = get_row_by_id(json_list[key])
results.append(result)
else:
for entity in json_list:
for key in entity:
if key.upper() == "ID":
update_row_from_id(table, entity[key], entity)
result = get_row_by_id(table, entity[key])
results.append(result)
if len(results) == 0:
raise BadRequestError()

return results

0 comments on commit 07d29d4

Please sign in to comment.