Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Boilerplate #53

Merged
merged 6 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 80 additions & 34 deletions app/Http/Controllers/Api/V1/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getStatusCode()
*
* @param [type] $statusCode [description]
*
* @return mix
* @return statuscode
*/
public function setStatusCode($statusCode)
{
Expand All @@ -42,37 +42,13 @@ public function setStatusCode($statusCode)
return $this;
}

/**
* responsd not found.
*
* @param string $message
*
* @return mix
*/
public function respondNotFound($message = 'Not Found')
{
return $this->setStatusCode(IlluminateResponse::HTTP_NOT_FOUND)->respondWithError($message);
}

/**
* Respond with error.
*
* @param string $message
*
* @return mix
*/
public function respondInternalError($message = 'Internal Error')
{
return $this->setStatusCode('500')->respondWithError($message);
}

/**
* Respond.
*
* @param array $data
* @param array $headers
*
* @return mix
* @return \Illuminate\Http\JsonResponse
*/
public function respond($data, $headers = [])
{
Expand All @@ -85,7 +61,7 @@ public function respond($data, $headers = [])
* @param Paginator $items
* @param array $data
*
* @return mix
* @return \Illuminate\Http\JsonResponse
*/
public function respondWithPagination($items, $data)
{
Expand All @@ -101,12 +77,38 @@ public function respondWithPagination($items, $data)
return $this->respond($data);
}

/**
* Respond Created.
*
* @param string $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondCreated($data)
{
return $this->setStatusCode(201)->respond([
'data' => $data,
]);
}

/**
* Respond Created with data.
*
* @param string $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondCreatedWithData($data)
{
return $this->setStatusCode(201)->respond($data);
}

/**
* respond with error.
*
* @param $message
*
* @return mix
* @return \Illuminate\Http\JsonResponse
*/
public function respondWithError($message)
{
Expand All @@ -119,17 +121,61 @@ public function respondWithError($message)
}

/**
* Respond Created.
* responsd not found.
*
* @param string $message
*
* @return mix
* @return \Illuminate\Http\JsonResponse
*/
public function respondCreated($message)
public function respondNotFound($message = 'Not Found')
{
return $this->setStatusCode(201)->respond([
'message' => $message,
]);
return $this->setStatusCode(IlluminateResponse::HTTP_NOT_FOUND)->respondWithError($message);
}

/**
* Respond with error.
*
* @param string $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function respondInternalError($message = 'Internal Error')
{
return $this->setStatusCode(500)->respondWithError($message);
}

/**
* Respond with unauthorized.
*
* @param string $message
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondUnauthorized($message = 'Unauthorized')
{
return $this->setStatusCode(401)->respondWithError($message);
}

/**
* Respond with forbidden.
*
* @param string $message
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondForbidden($message = 'Forbidden')
{
return $this->setStatusCode(403)->respondWithError($message);
}

/**
* Respond with no content.
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithNoContent()
{
return $this->setStatusCode(204)->respond(null);
}

/**
Expand Down