| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /**
- * Provides interface for food apis.
- */
- (function() {
- // Basic api template all foods follow
- var abstractFood = function($resource, path) {
- var methods = {
- 'get': {
- url: "food/:id",
- method: 'GET'
- },
- 'query': {
- url: "food/"+path+"query/:query",
- method: 'GET',
- isArray: true
- },
- 'delete': {
- url: "food/:id",
- method: 'DELETE'
- }
- };
- if (path == "") {
- return $resource("food/"+path+':id', {id: "@id"}, methods);
- }
- else {
- // Create/Update requires you to know what type of food you're using
- methods.create = {method: 'PUT'};
- methods.update = {method: 'POST'};
- var res = $resource("food/"+path+':id', {id: "@id"}, methods);
- // TODO: Pass arguments generically
- res.prototype.$save = function(success, failure) {
- if (!this.id) {
- return this.$create(success, failure);
- }
- else {
- return this.$update(success, failure);
- }
- };
- return res;
- }
- };
- angular.module('Food', ['ngResource'])
- /*
- * Collection of basic food apis.
- *
- * Methods:
- * get: Gets information for a basic food by id
- * query: Get a list of basic foods whose name contains the given string
- * delete: Deletes a basic food with an id
- * create: Creates a new basic food
- * update: Updates a basic food
- * submit: Save a new basic food or update it if it already exists.
- * fromNdb (static): Returns a new basic food adapted from an ndb object
- */
- .factory('BasicFood', ['$resource', function($resource) {
- var res = abstractFood($resource, 'basic/');
- res.fromNdb = function(ndbItem) {
- return new this({
- ndbno: parseInt(ndbItem.ndbno),
- name: ndbItem.name,
- food_group: ndbItem.fg,
- default_unit: ndbItem.ru,
- calories_p_100: ndbItem.nutrients.find(function (nutrient) {
- // TODO: Replace 208 with soft-loaded value from database
- // 208 is the id for kCalories
- return nutrient.nutrient_id == 208;
- }).value
- });
- };
- return res;
- }])
- /*
- * Collection of recipe apis.
- *
- * Methods:
- * get: Gets information for a recipe by id
- * query: Get a list of recipes whose name contains the given string
- * delete: Deletes a recipe with an id
- * create: Creates a new recipe
- * update: Updates a recipe
- * submit: Save a new recipe or update it if it already exists.
- */
- .factory('Recipe', ['$resource', function($resource) {
- return abstractFood($resource, 'recipe/');
- }])
- /*
- * Generic Food query interface.
- *
- * Useful for getting a list of mixed food types.
- *
- * Methods:
- * get: Gets information for a specific food by id
- * query: Get a list of foods whose name contains the given string
- * delete: Deletes a food with an id
- * cast: Convert from a generic food to it's designated type
- */
- .factory('Food', ['$resource', 'BasicFood', 'Recipe', function($resource, BasicFood, Recipe) {
- var absFood = abstractFood($resource, "");
- absFood.prototype.cast = function() {
- switch (this.type) {
- case "BasicFood":
- return new BasicFood(this);
- case "Recipe":
- return new Recipe(this);
- default:
- throw "Unrecognized food type: "+this.type;
- }
- };
- return absFood;
- }]);
- })();
|