From 5e47b6cd7a8ec3bcb52090d6b7ef40673505710c Mon Sep 17 00:00:00 2001 From: briankelleher Date: Tue, 6 Dec 2016 23:18:08 -0500 Subject: [PATCH] get all foods by offset --- api/Foodbank.postman_collection.json | 14 +++++++++ api/routes/food.php | 45 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/api/Foodbank.postman_collection.json b/api/Foodbank.postman_collection.json index 63dbc4c..239b7d5 100644 --- a/api/Foodbank.postman_collection.json +++ b/api/Foodbank.postman_collection.json @@ -34,6 +34,20 @@ "description": "" }, "response": [] + }, + { + "name": "GET All Food with Limit & Offset", + "request": { + "url": "http://localhost:8000/food/all/1/0", + "method": "GET", + "header": [], + "body": { + "mode": "formdata", + "formdata": [] + }, + "description": "First parameter after /food/all is LIMIT\nSecond parameter after /food/all is OFFSET" + }, + "response": [] } ] } \ No newline at end of file diff --git a/api/routes/food.php b/api/routes/food.php index 94194bb..ba74c4a 100644 --- a/api/routes/food.php +++ b/api/routes/food.php @@ -38,4 +38,49 @@ } $response = $response->withJSON($response_json); return $response; +}); + +/** + * GET All Food With Offset + * @param int limit + * @param int offset + */ +$app->get('/food/all/{limit}/{offset}', function( $request, $response ) { + $q = new FoodQuery(); + $limit = $request->getAttribute('limit'); + $offset = $request->getAttribute('offset'); + $foods = $q::create() + ->useRankQuery() + ->endUse() + ->useCategoryQuery() + ->endUse() + ->limit($limit) + ->offset($offset) + ->find(); + $response_json = [ + "status" => [ + "code" => $response->getStatusCode(), + "message" => "OK" + ], + "data" => [] + ]; + foreach( $foods as $food ) { + $rank = $food->getRank(); + $category = $food->getCategory(); + $response_json["data"][] = [ + "id" => $food->getId(), + "name" => $food->getName(), + "barcode" => $food->getBarcode(), + "rank" => [ + "id" => $food->getRankId(), + "name" => $rank->getName() + ], + "category" => [ + "id" => $food->getCategoryId(), + "name" => $category->getName() + ] + ]; + } + $response = $response->withJSON($response_json); + return $response; }); \ No newline at end of file