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 0eafcb0..f72b283 100644 Binary files a/python-api/__pycache__/api.cpython-37.pyc and b/python-api/__pycache__/api.cpython-37.pyc differ 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()) + ]