From 4331a140045887fad34511f3447b17508c952df8 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Mon, 2 Nov 2015 23:20:38 -0300 Subject: [PATCH] [Ivan Diaz] - Add response object and errors class --- server/controllers/user.php | 9 ++++++--- server/models/ERRORS.php | 4 ++++ server/models/Response.php | 25 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 server/models/ERRORS.php create mode 100644 server/models/Response.php diff --git a/server/controllers/user.php b/server/controllers/user.php index 84cc8fce..9508d582 100644 --- a/server/controllers/user.php +++ b/server/controllers/user.php @@ -13,7 +13,9 @@ $app->group('/user', function () use ($app) { 'admin' => 0 )); $id = $userInstance->store(); - $app->response()->setBody("{ \"id\": $id }"); + Response::respondSuccess(array( + 'id' => $id + )); }); $app->post('/login', function () use ($app) { @@ -23,11 +25,12 @@ $app->group('/user', function () use ($app) { if ($userInstance = User::getUser($user, 'user')) { $pass = $userInstance->password; } + if ($pass === $password) { - $app->response()->setBody("{ \"response\": \"OK\" }"); + Response::respondSuccess(); } else { - $app->response()->setBody("{ \"response\": \"FAIL\" }"); + Response::respondError(ERRORS::UNDEFINED_CREDENTIALS); } }); diff --git a/server/models/ERRORS.php b/server/models/ERRORS.php new file mode 100644 index 00000000..898aada5 --- /dev/null +++ b/server/models/ERRORS.php @@ -0,0 +1,4 @@ + 'fail', + 'message' => $errorMsg, + 'data' => $data + ); + + $app = \Slim\Slim::getInstance(); + $app->response()->setBody(json_encode($response)); + } + + public static function respondSuccess($data = null) { + $response = array( + 'status' => 'success', + 'data' => $data + ); + + $app = \Slim\Slim::getInstance(); + $app->response()->setBody(json_encode($response)); + } +}