[Ivan Diaz] - Add response object and errors class

This commit is contained in:
Ivan Diaz 2015-11-02 23:20:38 -03:00
parent 9b6ad74e6a
commit 4331a14004
3 changed files with 35 additions and 3 deletions

View File

@ -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);
}
});

4
server/models/ERRORS.php Normal file
View File

@ -0,0 +1,4 @@
<?php
class ERRORS {
const UNDEFINED_CREDENTIALS = 'User or password is not defined';
}

View File

@ -0,0 +1,25 @@
<?php
class Response{
private static $errored;
public static function respondError($errorMsg, $data = null) {
$response = array(
'status' => '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));
}
}