Food.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. nutrients: ndbItem.nutrients.reduce(function(r, n) {
  69. r[n.nutrient_id] = n.value;
  70. return r;
  71. }, {})
  72. });
  73. };
  74. return res;
  75. }])
  76. /*
  77. * Collection of recipe apis.
  78. *
  79. * Methods:
  80. * get: Gets information for a recipe by id
  81. * query: Get a list of recipes whose name contains the given string
  82. * delete: Deletes a recipe with an id
  83. * create: Creates a new recipe
  84. * update: Updates a recipe
  85. * submit: Save a new recipe or update it if it already exists.
  86. */
  87. .factory('Recipe', ['$resource', function($resource) {
  88. return abstractFood($resource, 'recipe/');
  89. }])
  90. /*
  91. * Generic Food query interface.
  92. *
  93. * Useful for getting a list of mixed food types.
  94. *
  95. * Methods:
  96. * get: Gets information for a specific food by id
  97. * query: Get a list of foods whose name contains the given string
  98. * delete: Deletes a food with an id
  99. * cast: Convert from a generic food to it's designated type
  100. */
  101. .factory('Food', ['$resource', 'BasicFood', 'Recipe', function($resource, BasicFood, Recipe) {
  102. var absFood = abstractFood($resource, "");
  103. absFood.prototype.cast = function() {
  104. switch (this.type) {
  105. case "BasicFood":
  106. return new BasicFood(this);
  107. case "Recipe":
  108. return new Recipe(this);
  109. default:
  110. throw "Unrecognized food type: "+this.type;
  111. }
  112. };
  113. return absFood;
  114. }]);
  115. })();