def add_item(payload): """Service to add new item to DB. Responses: 500: If any exception occurs and failed to add item to DB. 400: If bad request is sent. 200: Add item to DB successfully. @:param payload: JSON information of new item. @:return API response """ try: new_item = convert_json_to_object(Item(), json.dumps(payload)) if not new_item or not new_item.name: return jsonify(RES_INVALID_BODY_IT), 400 db_session = Session() if new_item.id: sl = db_session.query(Item) \ .filter(Item.id == new_item.id).all() if len(sl) > 0: return jsonify(RES_DUPLICATED), 400 db_session.add(new_item) db_session.commit() except Exception as ex: logger.error(ex, exc_info=True) return jsonify(RES_FAILED), 500 return jsonify(RES_SUCCESS), 200
def add_shopping_list(payload): """Service to add new shopping list to DB. Responses: 500: If any exception occurs and failed to add shopping list to DB. 400: If bad request is sent. 200: Add shopping list to DB successfully. @:param payload: JSON information of new shopping list. @:return API response """ try: new_shopping_list = \ convert_json_to_object(ShoppingList(), json.dumps(payload)) if not new_shopping_list or not new_shopping_list.title \ or not new_shopping_list.store_name: return jsonify(RES_INVALID_BODY_SL), 400 db_session = Session() if new_shopping_list.id: sl = db_session.query(ShoppingList) \ .filter(ShoppingList.id == new_shopping_list.id).all() if len(sl) > 0: return jsonify(RES_DUPLICATED), 400 db_session.add(new_shopping_list) db_session.commit() except Exception as ex: logger.error(ex, exc_info=True) return jsonify(RES_FAILED), 500 return jsonify(RES_SUCCESS), 200
def delete_shopping_list(shopping_list_id): """Service Delete shopping list. Responses: 500: If any exception occurs and failed to delete shopping list. 400: If bad request is sent. 200: Delete shopping list successfully. @:param shopping_list_id: ID of shopping list needed to be deleted. @:return API response """ try: db_session = Session() shopping_lists = db_session.query(ShoppingList) \ .filter(ShoppingList.id == shopping_list_id).all() if not shopping_lists: return jsonify(RES_INVALID_SHOPPING_LIST_ID), 400 for el in shopping_lists: db_session.delete(el) db_session.commit() except Exception as ex: logger.error(ex, exc_info=True) return jsonify(RES_FAILED), 500 return jsonify(RES_SUCCESS), 200
def update_shopping_list(shopping_list_id, payload): """Service to update shopping list. Responses: 500: If any exception occurs and failed to update shopping list. 400: If bad request is sent. 200: Update shopping list successfully. @:param shopping_list_id: ID of shopping list needed to be updated. @:param payload: JSON information of 'store_name' and 'title' of shopping list. @:return API response """ try: db_session = Session() shopping_lists = db_session.query(ShoppingList) \ .filter(ShoppingList.id == shopping_list_id) \ .with_for_update(nowait=False).all() if not shopping_lists: return jsonify(RES_INVALID_SHOPPING_LIST_ID), 400 new_title = payload.get(SL_TITLE) new_store_name = payload.get(SL_STORE_NAME) if not new_title and not new_store_name: return jsonify(RES_UPDATE_WITH_ALL_EMPTY_VALUES), 400 for sl in shopping_lists: if new_title is not None: sl.title = new_title if new_store_name is not None: sl.store_name = new_store_name db_session.commit() except Exception as ex: logger.error(ex, exc_info=True) return jsonify(RES_FAILED), 500 return jsonify(RES_SUCCESS), 200
def add_item_to_shopping_list(payload): """Service to add items to shopping list. Responses: 500: If any exception occurs and failed to add item to shopping list. 400: If bad request is sent. 200: Add item to shopping list successfully. @:param payload: JSON information of 'shopping_list_id' and 'item_ids' @:return Updated shopping list as JSON (if 200). """ try: shopping_list_id = payload.get(REQ_SHOPPING_LIST_ID) item_ids = payload.get(REQ_ITEM_IDS) if not shopping_list_id or not item_ids: return jsonify(RES_REQUIRE_SHOPPING_LIST_ITEM_ID), 400 db_session = Session() shopping_list = db_session.query(ShoppingList) \ .filter(ShoppingList.id == shopping_list_id).first() if not shopping_list: return jsonify(RES_INVALID_SHOPPING_LIST_ID), 400 dict_item_id_2_count = group_by_a_list(item_ids) new_items = db_session.query(Item).filter(Item.id.in_(item_ids)).all() if not new_items or len(dict_item_id_2_count) > len(new_items): return jsonify(RES_INVALID_ITEM_ID), 400 dict_id_2_new_item = dict([(x.id, x) for x in new_items]) current_items = db_session.query(ShoppingListItem).filter( ShoppingListItem.shopping_list_id == shopping_list.id).all() for current_item in current_items: item_id = current_item.item_id if item_id and item_id in dict_id_2_new_item: current_item.quantity += dict_item_id_2_count[item_id] del dict_id_2_new_item[item_id] # these items have never been added to the shopping list if len(dict_id_2_new_item) > 0: for item_id, item in dict_id_2_new_item.items(): ShoppingListItem(shopping_list=shopping_list, item=item, quantity=dict_item_id_2_count[item_id]) db_session.commit() # get updated Shopping List object as JSON string updated_shopping_list = shopping_list.as_dict() shopping_list_items = db_session.query(ShoppingListItem).filter( ShoppingListItem.shopping_list_id == shopping_list.id).all() updated_items = [] for updated_item in shopping_list_items: updated_items.append(updated_item.as_dict()) updated_shopping_list[SL_ITEMS] = updated_items return jsonify(updated_shopping_list), 200 except Exception as ex: logger.error(ex, exc_info=True) return jsonify(RES_FAILED), 500