Skip to content

Commit

Permalink
get all foods by offset
Browse files Browse the repository at this point in the history
  • Loading branch information
briankelleher committed Dec 7, 2016
1 parent 846d90b commit 5e47b6c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/Foodbank.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
]
}
45 changes: 45 additions & 0 deletions api/routes/food.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

0 comments on commit 5e47b6c

Please sign in to comment.