def is_valid_form( name: str, lunch: str ): """ takes in the entered name and lunch details ensures they do not exist in the json file and returns a boolean if true """ if not name or not lunch: return ( False, 'please enter valid inputs for name and lunch' ) data = get_lunches() for data in data['data']: if data['name'] == name: return ( False, 'Name already exists!' ) else: return ( True, 'Added lunch details.' )
def get_person_lunch(name: str): """ get the person's lunch by name returns them in a dict/json """ lunches = get_lunches() for element in lunches['data']: if name.capitalize() == element['name']: return element return 'not found.'
def delete_person(name: str): """ delete a person by name """ lunches = get_lunches() for data in lunches['data']: if data['name'] == name.capitalize(): break else: return 'Not Found' return f'Successfully deleted {name}'
def get_persons_who_ate(meal: str): """ get all person's who ate a specific meal returns them as dict if they exist """ result = {"results": []} lunches = get_lunches() for data in lunches['data']: if data['lunch'] == meal.capitalize(): result['results'].append(data) return result \ if result['results']\ else 'Not found'
def post_lunch(): """ add new lunch entry """ name = request.json.get('name', False).capitalize() lunch = request.json.get('lunch', False).capitalize() is_valid, message = is_valid_form(name, lunch) if not is_valid: return message lunches = get_lunches() lunches['data'].lunch_routesend({'name': name, "lunch": lunch}) update_lunches(lunches) return message
def get_all_lunches(): """ gets all the lunches """ data = get_lunches() return data