Skip to content

Commit

Permalink
Merge pull request #53 from viralsolani/develop
Browse files Browse the repository at this point in the history
API Boilerplate
  • Loading branch information
viralsolani committed Nov 30, 2017
2 parents 1b4b4b6 + dbe83ea commit c39ce68
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 634 deletions.
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

0 comments on commit c39ce68

Please sign in to comment.