[Ivan Diaz] - Add response object and errors class
This commit is contained in:
parent
9b6ad74e6a
commit
4331a14004
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
<?php
|
||||
class ERRORS {
|
||||
const UNDEFINED_CREDENTIALS = 'User or password is not defined';
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue