Skip to content

Commit

Permalink
remove status and add error key for response json
Browse files Browse the repository at this point in the history
  • Loading branch information
yaza-putu committed Apr 13, 2024
1 parent 6fa6aec commit 0a0e6c2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 90 deletions.
28 changes: 14 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
"homepage": "https://github.com/yaza-putu/laravel-repository-with-service.git",
"license": "MIT",
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"php": "^8.2",
"spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^10.0||^11.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-arch": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"spatie/laravel-ray": "^1.26"
"laravel/pint": "^1.14",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^9.0.0||^8.22.0",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-arch": "^2.7",
"pestphp/pest-plugin-laravel": "^2.3",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"spatie/laravel-ray": "^1.35"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function all()
*/
public function create($data)
{
$this->mainRepository->create($data);
return $this->mainRepository->create($data);
}

/**
Expand Down
61 changes: 31 additions & 30 deletions src/ServiceApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,80 @@ class ServiceApi implements BaseService
use ResultService;

protected $title = "";
protected $create_message = "";
protected $update_message = "";
protected $delete_message = "";
protected $create_message = "created successfully";
protected $update_message = "updated successfully";
protected $delete_message = "deleted successfully";


/**
* Find an item by id
* @param mixed $id
* @return Model|null
* find by id
* @param $id
* @return \Illuminate\Database\Eloquent\Model|ServiceApi|ResultService|null
*/
public function find($id)
{
try {
$result = $this->mainRepository->find($id);
return $this->setResult($result)
->setCode(200)
->setStatus(true);
return $this->setData($result)
->setCode(200);
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
}


/**
* Find an item by id or fail
* @param mixed $id
* @return Model|null
* find or fail by id
* @param $id
* @return ServiceApi|ResultService|mixed
*/
public function findOrFail($id)
{
try {
$result = $this->mainRepository->findOrFail($id);
return $this->setResult($result)
->setCode(200)
->setStatus(true);
return $this->setData($result)
->setCode(200);
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
}


/**
* Return all items
* @return Collection|null
* all data
* @return \Illuminate\Database\Eloquent\Collection|ServiceApi|ResultService|null
*/
public function all()
{
try {
$result = $this->mainRepository->all();;
return $this->setResult($result)
->setCode(200)
->setStatus(true);
return $this->setData($result)
->setCode(200);
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
}


/**
* Create an item
* @param array|mixed $data
* @return Model|null
* create data
* @param $data
* @return \Illuminate\Database\Eloquent\Model|ServiceApi|ResultService|null
*/
public function create($data)
{
try {
$this->mainRepository->create($data);
$data = $this->mainRepository->create($data);
return $this->setMessage($this->title." ".$this->create_message)
->setCode(200)
->setStatus(true);
->setData($data);
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
}

/**
* Update a model
* Update data
* @param int|mixed $id
* @param array|mixed $data
* @return bool|mixed
Expand All @@ -93,14 +94,14 @@ public function update($id, array $data)
$this->mainRepository->update($id, $data);
return $this->setMessage($this->title." ".$this->update_message)
->setCode(200)
->setStatus(true);
;
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
}

/**
* Delete a model
* Delete data by id
* @param int|Model $id
*/
public function delete($id)
Expand All @@ -109,7 +110,7 @@ public function delete($id)
$this->mainRepository->delete($id);
return $this->setMessage($this->title." ".$this->delete_message)
->setCode(200)
->setStatus(true);
;
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
Expand All @@ -126,7 +127,7 @@ public function destroy(array $id)
$this->mainRepository->destroy($id);
return $this->setMessage($this->title." ".$this->delete_message)
->setCode(200)
->setStatus(true);
;
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/JsonValidateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function failedValidation(Validator $validator)
$errors = (new ValidationException($validator))->errors();

throw new HttpResponseException(
response()->json(['errors' => $errors, 'success' => false, 'code' => 422, 'message' => 'validation error messages'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
response()->json(['errors' => $errors, 'code' => 422, 'message' => 'Unprocessable Entity'], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ trait Response
* @param null $code
* @return \Illuminate\Http\JsonResponse
*/
public function responseJson($status = true, $msg = '', $data = [], $code = null)
public function responseJson($msg = '', $data = [], $code = null, $errors = [])
{
if(is_null($code)){
$http_code = $status ? 200 : 400;
$http_code = 200;
}else{
$http_code = $code;
}

return response()->json([
'success' => $status,
'code' => $http_code,
'message' => $msg,
'data' => $data,
'errors' => $errors
], $http_code);
}
}
79 changes: 38 additions & 41 deletions src/Traits/ResultService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,30 @@

trait ResultService
{
private $result = null;
private $status = false;
private $data = null;
private $message = null;
private $code = null;
private $errors = [];

/**
* set result output
* @param $result
* set data output
* @param $data
* @return $this
*/
public function setResult($result)
public function setData($data)
{
$this->result = $result;
$this->data = $data;

return $this;
}

/**
* get result
* get data
* @return null
*/
public function getResult()
public function getData()
{
return $this->result;
}

/**
* set status
* @param $status
* @return $this
*/
public function setStatus($status)
{
$this->status = $status;

return $this;
}

/**
* get status
* @return bool
*/
public function getStatus()
{
return $this->status;
return $this->data;
}

/**
Expand Down Expand Up @@ -99,6 +78,27 @@ public function getCode()
return $this->code;
}


/**
* set errors
* @param $error
* @return $this
*/
public function setError($error)
{
$this->errors = $error;
return $this;
}

/**
* get errors
* @return array
*/
public function getError()
{
return $this->errors;
}

/**
* Exception Response
*
Expand All @@ -109,17 +109,15 @@ public function exceptionResponse(Exception $exception)
{
if ($exception instanceof QueryException) {
if ($exception->errorInfo[1] == 1451) {
return $this->setStatus(false)
->setMessage('Data masih terpakai di Data Lain!')
return $this->setMessage('This data cannot be removed because it is still in use.')
->setCode(400);
}
}
if ($exception instanceof ModelNotFoundException) {
if (!request()->expectsJson()) {
return abort(404);
}
return $this->setStatus(false)
->setMessage('Data tidak ditemukan!')
return $this->setMessage('Data not found')
->setCode(404);
}
if (config('app.debug')) {
Expand All @@ -130,13 +128,12 @@ public function exceptionResponse(Exception $exception)
'line' => $exception->getLine(),
'trace' => $exception->getTrace()
];
return $this->setStatus(false)
->setMessage($message)
return $this->setError($message)
->setMessage("internal server error")
->setCode(500);
}

return $this->setStatus(false)
->setMessage('Terjadi suatu kesalahan!')
return $this->setMessage('internal server error')
->setCode(500);
}

Expand All @@ -147,16 +144,16 @@ public function exceptionResponse(Exception $exception)
public function toJson()
{
if(is_null($this->getCode())){
$http_code = $this->getStatus() ? 200 : 400;
$http_code = 200;
}else{
$http_code = $this->getCode();
}

return response()->json([
'success' => $this->getStatus(),
'code' => $http_code,
'message' => $this->getMessage(),
'data' => $this->getResult(),
'data' => $this->getData(),
'errors' => $this->getError(),
], $http_code);
}
}

0 comments on commit 0a0e6c2

Please sign in to comment.