|
|
@@ -0,0 +1,201 @@
|
|
|
+package name.tflucke.ieat2.controllers;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.PutMapping;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+import name.tflucke.ieat2.models.BasicFood;
|
|
|
+import name.tflucke.ieat2.models.Food;
|
|
|
+import name.tflucke.ieat2.models.Recipe;
|
|
|
+import name.tflucke.ieat2.errors.ResourceWrongTypeException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Provides APIs for managing Food objects and it's subclasses.
|
|
|
+ *
|
|
|
+ * @author Thomas Flucke
|
|
|
+ * @since 2.0.0
|
|
|
+ */
|
|
|
+@RestController("/food/")
|
|
|
+public class FoodController extends AbstractController<Food> {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates a new controller
|
|
|
+ */
|
|
|
+ public FoodController()
|
|
|
+ {
|
|
|
+ super(Food.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Generic Food APIs */
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @GetMapping("/food/{id}")
|
|
|
+ public Food get(@PathVariable("id") final String id) {
|
|
|
+ return super.get(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of the most common foods.
|
|
|
+ *
|
|
|
+ * Provides a default behavior for empty queries.
|
|
|
+ *
|
|
|
+ * @return A list of the most common foods
|
|
|
+ */
|
|
|
+ @GetMapping("/food/query")
|
|
|
+ public List<Food> query() {
|
|
|
+ return db.find(Food.class).asList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of foods whose name contains a string.
|
|
|
+ *
|
|
|
+ * The input is case insensitive.
|
|
|
+ *
|
|
|
+ * @param query A name-fragment to search
|
|
|
+ * @return A list of foods with the query in it's name
|
|
|
+ */
|
|
|
+ @GetMapping("/food/query/{query}")
|
|
|
+ public List<Food> query(@PathVariable("query") final String query) {
|
|
|
+ return db.find(Food.class).field("name").containsIgnoreCase(query).asList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @DeleteMapping("/food/{id}")
|
|
|
+ public Food delete(@PathVariable("id") final String id) {
|
|
|
+ return super.delete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* BasicFood APIs */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of the most common BasicFoods.
|
|
|
+ *
|
|
|
+ * Provides a default behavior for empty queries.
|
|
|
+ *
|
|
|
+ * @return A list of the most common basic foods
|
|
|
+ */
|
|
|
+ @GetMapping("/food/basic/query")
|
|
|
+ public List<BasicFood> queryBasicFoods() {
|
|
|
+ return db.find(BasicFood.class).asList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of basic foods whose name contains a string.
|
|
|
+ *
|
|
|
+ * The input is case insensitive.
|
|
|
+ *
|
|
|
+ * @param query A name-fragment to search
|
|
|
+ * @return A list of basic foods with the query in it's name
|
|
|
+ */
|
|
|
+ @GetMapping("/food/basic/query/{query}")
|
|
|
+ public List<BasicFood> queryBasicFoods(@PathVariable("query") final String query) {
|
|
|
+ return db.find(BasicFood.class).field("name").containsIgnoreCase(query).asList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Insert a new basic food into the database.
|
|
|
+ *
|
|
|
+ * Any provided id is ignored as a new one will be assigned automatically.
|
|
|
+ *
|
|
|
+ * @param newElement A basic food to insert into the database
|
|
|
+ * @return The original element inserted with a new id
|
|
|
+ */
|
|
|
+ @PutMapping("/food/basic")
|
|
|
+ public BasicFood insert(@RequestBody BasicFood newElement) {
|
|
|
+ return (BasicFood) super.insert(newElement);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Save a basic food's not-null fields to the database with a given id.
|
|
|
+ *
|
|
|
+ * Any fields with a null/missing value will be ignored.
|
|
|
+ *
|
|
|
+ * An element with the provided id must exist to be updated.
|
|
|
+ *
|
|
|
+ * This operation increments the version counter.
|
|
|
+ *
|
|
|
+ * @param id The id of the basic food to update
|
|
|
+ * @param element A reference basic food containing the changes to make
|
|
|
+ * @return The newly updated basic food
|
|
|
+ */
|
|
|
+ @PostMapping("/food/basic/{id}")
|
|
|
+ public BasicFood update(@PathVariable("id") final String id,
|
|
|
+ @RequestBody final BasicFood element) {
|
|
|
+ if (get(id) instanceof BasicFood) {
|
|
|
+ return (BasicFood) super.update(id, element);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ throw new ResourceWrongTypeException(id+" is not a Recipe.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Recipe APIs */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of the most common recipes.
|
|
|
+ *
|
|
|
+ * Provides a default behavior for empty queries.
|
|
|
+ *
|
|
|
+ * @return A list of the most common recipes
|
|
|
+ */
|
|
|
+ @GetMapping("/food/recipe/query")
|
|
|
+ public List<Recipe> queryRecipes() {
|
|
|
+ return db.find(Recipe.class).asList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a list of recipes whose name contains a string.
|
|
|
+ *
|
|
|
+ * The input is case insensitive.
|
|
|
+ *
|
|
|
+ * @param query A name-fragment to search
|
|
|
+ * @return A list of recipes with the query in it's name
|
|
|
+ */
|
|
|
+ @GetMapping("/food/recipe/query/{query}")
|
|
|
+ public List<Recipe> queryRecipes(@PathVariable("query") final String query) {
|
|
|
+ return db.find(Recipe.class).field("name").containsIgnoreCase(query).asList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Insert a recipe into the database.
|
|
|
+ *
|
|
|
+ * Any provided id is ignored as a new one will be assigned automatically.
|
|
|
+ *
|
|
|
+ * @param newElement A recipe to insert into the database
|
|
|
+ * @return The original recipe inserted with a new id
|
|
|
+ */
|
|
|
+ @PutMapping("/food/recipe")
|
|
|
+ public Recipe insert(@RequestBody Recipe newElement) {
|
|
|
+ return (Recipe) super.insert(newElement);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Save a recipe's not-null fields to the database with a given id.
|
|
|
+ *
|
|
|
+ * Any fields with a null/missing value will be ignored.
|
|
|
+ *
|
|
|
+ * An element with the provided id must exist to be updated.
|
|
|
+ *
|
|
|
+ * This operation increments the version counter.
|
|
|
+ *
|
|
|
+ * @param id The id of the recipe to update
|
|
|
+ * @param element A reference recipe containing the changes to make
|
|
|
+ * @return The newly updated recipe
|
|
|
+ */
|
|
|
+ @PostMapping("/food/recipe/{id}")
|
|
|
+ public Recipe update(@PathVariable("id") final String id,
|
|
|
+ @RequestBody final Recipe element) {
|
|
|
+ if (get(id) instanceof Recipe) {
|
|
|
+ return (Recipe) super.update(id, element);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ throw new ResourceWrongTypeException(id+" is not a Recipe.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|