Skip to content

Commit

Permalink
food by id and food by barcode
Browse files Browse the repository at this point in the history
  • Loading branch information
briankelleher committed Dec 7, 2016
1 parent f7fb36d commit f42cf1d
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions api/routes/food.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,104 @@
}
$response = $response->withJSON($response_json);
return $response;
});


/**
* GET a Food By ID
* @param int limit
* @param int offset
*/
$app->get('/food/{id}', function( $request, $response ) {
$q = new FoodQuery();
$id = $request->getAttribute('id');
$food = $q::create()
->useRankQuery()
->endUse()
->useCategoryQuery()
->endUse()
->findPK(intval($id));
if ( $food ) {
$rank = $food->getRank();
$category = $food->getCategory();
$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => [
"id" => $food->getId(),
"name" => $food->getName(),
"barcode" => $food->getBarcode(),
"rank" => [
"id" => $food->getRankId(),
"name" => $rank->getName()
],
"category" => [
"id" => $food->getCategoryId(),
"name" => $category->getName()
]
]
];
} else {
$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => []
];
}
$response = $response->withJSON($response_json);
return $response;
});

/**
* GET a Food By ID
* @param int limit
* @param int offset
*/
$app->get('/food/barcode/{barcode}', function( $request, $response ) {
$q = new FoodQuery();
$barcode = $request->getAttribute('barcode');
$food = $q::create()
->useRankQuery()
->endUse()
->useCategoryQuery()
->endUse()
->filterByBarcode($barcode)
->findOne();
if ( $food ) {
$rank = $food->getRank();
$category = $food->getCategory();
$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => [
"id" => $food->getId(),
"name" => $food->getName(),
"barcode" => $food->getBarcode(),
"rank" => [
"id" => $food->getRankId(),
"name" => $rank->getName()
],
"category" => [
"id" => $food->getCategoryId(),
"name" => $category->getName()
]
]
];
} else {
$response_json = [
"status" => [
"code" => $response->getStatusCode(),
"message" => "OK"
],
"data" => []
];
}
$response = $response->withJSON($response_json);
return $response;
});

0 comments on commit f42cf1d

Please sign in to comment.