diff --git a/api/config.php b/api/config.php index c07ab83..04a42f5 100644 --- a/api/config.php +++ b/api/config.php @@ -1,4 +1,9 @@ [ + 'displayErrorDetails' => true, + 'addContentLengthHeader' => false + ] +]; + diff --git a/api/index.php b/api/index.php index ee8530e..3bae386 100644 --- a/api/index.php +++ b/api/index.php @@ -12,6 +12,7 @@ * Routes */ require './routes/category.php'; +require './routes/rules.php'; // Run App $app->run(); \ No newline at end of file diff --git a/api/routes/category.php b/api/routes/category.php index 6a344a0..cb5856e 100644 --- a/api/routes/category.php +++ b/api/routes/category.php @@ -20,6 +20,32 @@ "name" => $cat->getName() ]; } + $response = $response->withJSON($response_json); + return $response; +}); + +/** +* GET Category by ID +* @param id int PK of known category +*/ + +$app->get('/category/{id}', function( $request, $response ) { + $q = new CategoryQuery; + $cat = $q->findPK($request->getAttribute('id')); + + $response_json = [ + "status" => [ + "code" => $response->getStatusCode(), + "message" => "OK" + ], + "data" => [] + ]; + + $response_json["data"][] = [ + "id" => $cat->getId(), + "name" => $cat->getName() + ]; + $response = $response->withJSON($response_json); return $response; }); \ No newline at end of file diff --git a/api/routes/rules.php b/api/routes/rules.php new file mode 100644 index 0000000..5a24562 --- /dev/null +++ b/api/routes/rules.php @@ -0,0 +1,53 @@ +get('/rules/byCategory/{id}', function($request, $response) { + $q = new CategoryRankNutrientQuery; + $requestedCatRules = $q->filterByCategoryId($request->getAttribute('id'))->find(); + + $response_json = [ + "status" => [ + "code" => $response->getStatusCode(), + "message" => "OK" + ], + "data" => [] + ]; + + foreach($requestedCatRules as $rule) { + $response_json["data"][] = [ + "nutrientName" => $rule->getNutrient()->getName(), + "nutrientId" => $rule->getNutrient()->getId(), + "operator" => $rule->getOperator(), + "threshold" => $rule->getThreshold(), + "units" => $rule->getUnits() + ]; + } + + $response = $response->withJSON($response_json); + return $response; +}); + + + +$app->post('/rules/createForCategory/{catId}', function($request, $response) { + + $newRule = new CategoryRankNutrient; + + $cat = $request->getAttribute('catId'); + + + $nutrient = $request->getParam('nutrientId'); + + $rank = $request->getParam('rankId'); + + + $newRule->setThreshold($request->getParam('threshold')); + $newRule->setUnits($request->getParam('units')); + $newRule->setOperator($request->getParam('operator')); + $newRule->setCategoryId($cat); + $newRule->setRankId($rank); + $newRule->setNutrientId($nutrient); + + $newRule->save(); + + +}); \ No newline at end of file