From 65da7e8800c861940fde3c8d40b6947060e125ea Mon Sep 17 00:00:00 2001 From: Ava Gaiety Wroten Date: Tue, 24 Dec 2019 17:25:08 -0600 Subject: [PATCH] Hooked up frontend and backend --- ember-ui/app/adapters/application.js | 2 +- ember-ui/app/adapters/meal.js | 44 ++------------------- ember-ui/app/components/recipe-list.js | 1 - ember-ui/app/components/recipe-preview.hbs | 6 +-- ember-ui/app/models/meal.js | 20 ++++++---- ember-ui/app/templates/meal.hbs | 2 +- python-api/.gitignore | 2 +- python-api/__pycache__/api.cpython-37.pyc | Bin 2565 -> 2792 bytes python-api/api.py | 44 +++++++++++++-------- 9 files changed, 49 insertions(+), 72 deletions(-) diff --git a/ember-ui/app/adapters/application.js b/ember-ui/app/adapters/application.js index 2744f76..52d7328 100644 --- a/ember-ui/app/adapters/application.js +++ b/ember-ui/app/adapters/application.js @@ -1,5 +1,5 @@ import JSONAPIAdapter from '@ember-data/adapter/json-api'; export default class ApplicationAdapter extends JSONAPIAdapter { - host = 'https://www.themealdb.com/api/json/v1/1'; + host = 'http://localhost:8000/'; } diff --git a/ember-ui/app/adapters/meal.js b/ember-ui/app/adapters/meal.js index c6f0189..c9f7e18 100644 --- a/ember-ui/app/adapters/meal.js +++ b/ember-ui/app/adapters/meal.js @@ -1,51 +1,15 @@ import ApplicationAdapter from './application'; -import { dasherize } from '@ember/string'; export default class MealAdapter extends ApplicationAdapter { async findRecord(store, model, id) { - let result = await fetch(`${this.host}/lookup.php?i=${id}`); - let json = await result.json(); - return normalize(json.meals[0]); + let result = await fetch(`${this.host}meal/${id}`); + return await result.json(); } async queryRecord(store, model, query) { if (query === 'random') { - let result = await fetch(`${this.host}/random.php`); - let json = await result.json(); - return normalize(json.meals[0]); + let result = await fetch(`${this.host}meals/random`); + return await result.json(); } } } - -function normalize(obj) { - let attributes = {}; - - Object.keys(obj).forEach(key => attributes[cleanKey(key)] = obj[key]); - attributes.ingredients = groupByKeyPrefix('ingredient', attributes); - - return { - data: { - type: 'meal', - id: attributes.id, - attributes, - }, - }; -} - -function cleanKey(key) { - key = dasherize(key); - key = key.replace('str-', ''); - if (key === 'id-meal') key = 'id'; - if (key === 'meal') key = 'name'; - return key; -} - -function groupByKeyPrefix(keyPrefix, obj) { - let resultingArray = []; - Object.keys(obj).forEach(key => { - let value = obj[key]; - if (!value) return; - if (key.startsWith(keyPrefix)) resultingArray.push(value); - }); - return resultingArray; -} diff --git a/ember-ui/app/components/recipe-list.js b/ember-ui/app/components/recipe-list.js index a0d59d2..fd8ba04 100644 --- a/ember-ui/app/components/recipe-list.js +++ b/ember-ui/app/components/recipe-list.js @@ -14,7 +14,6 @@ export default class RecipeListComponent extends Component { } get sortedItems() { - console.log('sorted items called'); return this.items.sortBy('listOrder'); } diff --git a/ember-ui/app/components/recipe-preview.hbs b/ember-ui/app/components/recipe-preview.hbs index 7b1f59b..c702750 100644 --- a/ember-ui/app/components/recipe-preview.hbs +++ b/ember-ui/app/components/recipe-preview.hbs @@ -1,7 +1,7 @@
- +
@@ -9,9 +9,7 @@ {{@meal.name}}

- {{#each @meal.ingredients as |ingredient|}} - {{ingredient}} - {{/each}} + {{@meal.ingredientsList}}

diff --git a/ember-ui/app/models/meal.js b/ember-ui/app/models/meal.js index cc7aeaf..42643c5 100644 --- a/ember-ui/app/models/meal.js +++ b/ember-ui/app/models/meal.js @@ -2,15 +2,21 @@ import DS from 'ember-data'; const { Model, attr } = DS; export default class MealModel extends Model { - @attr name; - @attr dateModified; - @attr category; + @attr alternateDrink; @attr area; - @attr mealThumb; + @attr category; + @attr dateModified; @attr instructions; - @attr ingredients; + @attr name; + @attr sourceUrl; @attr tags; - @attr youtube; - @attr source; + @attr thumbnailUrl; + @attr youtubeUrl; + @attr ingredients; + @attr('number', { defaultValue: Infinity }) listOrder; + + get ingredientsList() { + return this.ingredients.map(ingredient => ingredient.name).join(', '); + } } diff --git a/ember-ui/app/templates/meal.hbs b/ember-ui/app/templates/meal.hbs index 9eb6472..e655a71 100644 --- a/ember-ui/app/templates/meal.hbs +++ b/ember-ui/app/templates/meal.hbs @@ -2,7 +2,7 @@ {{@model.name}} - +

{{@model.instructions}} diff --git a/python-api/.gitignore b/python-api/.gitignore index 225fc6f..bee8a64 100644 --- a/python-api/.gitignore +++ b/python-api/.gitignore @@ -1 +1 @@ -/__pycache__ +__pycache__ diff --git a/python-api/__pycache__/api.cpython-37.pyc b/python-api/__pycache__/api.cpython-37.pyc index 0eafcb0f8d4e022e8026778fba5ff8f9f429c261..f72b2832dcef60df14d89da4e1349b4d456b4ae3 100644 GIT binary patch delta 1153 zcmZuw>uMA+6rN%Nte@2DD5!X*=!g%#zHa4lDi9 z_5o!6h$ytaf%wxO;sf{s`vii|AbL(lDu{t|NpjA~`MxCkgWrd0OVz5Y!PtFIpZzSo zu6KinN@dk@g+aM{2@BUl&ax`b8u=UgVW(%bpmGy?$AQ)aSq71Vpcv^AMn|+R=>m#( zdPtyC<9^S85`nNF!qb)U-5^axxYkLz>>26HHW%e$7isD_g3ERiOYXa^gz;##h?s{x z6(=efRf-eQ3ZgLMBQTW?D2-BL5lXV!QXu)vQ9yoGUmq7W2q^OLu%d|FjCc@t+NVE# z(S1T{`KUQ(4#4(w{={5zh)VLD{H-}#?#C>|DF*UyrQ!UC=~l2`hZxl9bKU%xxqM<6 z7Mc_k4rB;nD~9Isr`FgtfYOcIx70(rn1I{0o>O&r!eiz$7;vHg9xD7AgEnOT{6e-5=AF3-W5<loO9}>S>&D?IZDYu5$yt!A)*~H@Uq*s=1RQEz%oa!EePvcKle> zxNLTIz5XbcYr=0m-%i?_UOqrqPHOur;CIkLK}N1-g*`hLC@1iMK%7jB17nhU9c*NxPnH8Ed? zgsC2jy;$N^QiCV$FskYjJK|ZmvDW0NEkzUtok+-n@^ga=Q?Pm)6yc=_EmN1eY`CdD#z54z4}gr;DYlp1i(RKb_8@g zI;TUVM|X@KJ)po&FW6be4z$2Nb;eH5*&XS3c+f_6ISv`CwwwSbD`+OsAdJL9$nq7m zSx>IQtg2;Ykk{~Q27VX!J_4PAmrlW*XQ%(jxc?9QVj508#FJ;h7}r4;1KAfrQ3cH3 zM=!Jwqyc>vI{GaJk6weW>Z#_3q574p44MB{7*j(*Z#k908JMnsB78wMgF?@!hI4Zy a1JCMUWSh}s$7Y~AL+YzjHfD>tqWuTfpuji) diff --git a/python-api/api.py b/python-api/api.py index 8f45ede..496bcc8 100644 --- a/python-api/api.py +++ b/python-api/api.py @@ -14,13 +14,24 @@ class Base(Handler): ["strCategory", "category"], ["strArea", "area"], ["strInstructions", "instructions"], - ["strDrinkAlternate", "alternate_drink"], - ["strMealThumb", "thumbnail_url"], + ["strDrinkAlternate", "alternate-drink"], + ["strMealThumb", "thumbnail-url"], ["strTags", "tags"], - ["strYoutube", "youtube_url"], - ["strSource", "source_url"], - ["dateModified", "date_modified"], + ["strYoutube", "youtube-url"], + ["strSource", "source-url"], + ["dateModified", "date-modified"], ]) + + def normalize(self, response, model_type = "meal"): + return { + "data": { + "id": response["id"], + "attributes": response, + "type": model_type, + } + } + + def cleanup(self, meal): self.replacable_keys.do_replace(meal) self.cleanup_ingredients(meal) @@ -49,23 +60,22 @@ class Base(Handler): class Details(Base): def get(self, meal_id = ''): - response = json.loads(requests.get(self.hostname + "lookup.php?i=" + meal_id).text) - for meal in response["meals"]: - self.cleanup(meal) + response = json.loads(requests.get(self.hostname + "lookup.php?i=" + meal_id).text)["meals"][0] + self.cleanup(response) - return json.dumps(response) + return self.normalize(response) class Random(Base): def get(self): - response = json.loads(requests.get(self.hostname + "random.php").text) - for meal in response["meals"]: - self.cleanup(meal) + response = json.loads(requests.get(self.hostname + "random.php").text)["meals"][0] + self.cleanup(response) - return json.dumps(response) + return self.normalize(response) class app(WSGI): - routes = [ - ("/meals/random", Random()), - ("/meal/([\w]+)", Details()) - ] + headers = [("Access-Control-Allow-Origin", "*")] + routes = [ + ("/meals/random", Random()), + ("/meal/([\w]+)", Details()) + ]