Food.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Provides interface for food apis.
  3. */
  4. (function() {
  5. // Basic api template all foods follow
  6. var abstractFood = function($resource, path) {
  7. var methods = {
  8. 'get': {
  9. url: "food/:id",
  10. method: 'GET'
  11. },
  12. 'query': {
  13. url: "food/"+path+"query/:query",
  14. method: 'GET',
  15. isArray: true
  16. },
  17. 'delete': {
  18. url: "food/:id",
  19. method: 'DELETE'
  20. }
  21. };
  22. if (path == "") {
  23. return $resource("food/"+path+':id', {id: "@id"}, methods);
  24. }
  25. else {
  26. // Create/Update requires you to know what type of food you're using
  27. methods.create = {method: 'PUT'};
  28. methods.update = {method: 'POST'};
  29. var res = $resource("food/"+path+':id', {id: "@id"}, methods);
  30. // TODO: Pass arguments generically
  31. res.prototype.$save = function(success, failure) {
  32. if (!this.id) {
  33. return this.$create(success, failure);
  34. }
  35. else {
  36. return this.$update(success, failure);
  37. }
  38. };
  39. return res;
  40. }
  41. };
  42. angular.module('Food', ['ngResource'])
  43. /*
  44. * Collection of basic food apis.
  45. *
  46. * Methods:
  47. * get: Gets information for a basic food by id
  48. * query: Get a list of basic foods whose name contains the given string
  49. * delete: Deletes a basic food with an id
  50. * create: Creates a new basic food
  51. * update: Updates a basic food
  52. * submit: Save a new basic food or update it if it already exists.
  53. * fromNdb (static): Returns a new basic food adapted from an ndb object
  54. */
  55. .factory('BasicFood', ['$resource', function($resource) {
  56. var res = abstractFood($resource, 'basic/');
  57. res.fromNdb = function(ndbItem) {
  58. return new this({
  59. ndbno: parseInt(ndbItem.ndbno),
  60. name: ndbItem.name,
  61. food_group: ndbItem.fg,
  62. default_unit: ndbItem.ru,
  63. calories_p_100: ndbItem.nutrients.find(function (nutrient) {
  64. // TODO: Replace 208 with soft-loaded value from database
  65. // 208 is the id for kCalories
  66. return nutrient.nutrient_id == 208;
  67. }).value
  68. });
  69. };
  70. return res;
  71. }])
  72. /*
  73. * Collection of recipe apis.
  74. *
  75. * Methods:
  76. * get: Gets information for a recipe by id
  77. * query: Get a list of recipes whose name contains the given string
  78. * delete: Deletes a recipe with an id
  79. * create: Creates a new recipe
  80. * update: Updates a recipe
  81. * submit: Save a new recipe or update it if it already exists.
  82. */
  83. .factory('Recipe', ['$resource', function($resource) {
  84. return abstractFood($resource, 'recipe/');
  85. }])
  86. /*
  87. * Generic Food query interface.
  88. *
  89. * Useful for getting a list of mixed food types.
  90. *
  91. * Methods:
  92. * get: Gets information for a specific food by id
  93. * query: Get a list of foods whose name contains the given string
  94. * delete: Deletes a food with an id
  95. * cast: Convert from a generic food to it's designated type
  96. */
  97. .factory('Food', ['$resource', 'BasicFood', 'Recipe', function($resource, BasicFood, Recipe) {
  98. var absFood = abstractFood($resource, "");
  99. absFood.prototype.cast = function() {
  100. switch (this.type) {
  101. case "BasicFood":
  102. return new BasicFood(this);
  103. case "Recipe":
  104. return new Recipe(this);
  105. default:
  106. throw "Unrecognized food type: "+this.type;
  107. }
  108. };
  109. return absFood;
  110. }]);
  111. })();