Commit 0a196fa2 authored by cygnet's avatar cygnet

following changes done

1) created an blog tag store api with the custom request class
2) changes done on the throw error on api to make the api return json error
3) changes done on the Handeler to check for jwt token not found error to return json
4) changes done on the Handeler to check for jwt token expired exception error to return json
5) changes done on the Handeler to check for jwt token invalid exception error to return json
parent 8114de50
......@@ -62,14 +62,14 @@ class Handler extends ExceptionHandler
case \Tymon\JWTAuth\Exceptions\TokenExpiredException::class:
return response()->json([
'status' => 'error',
'message' => 'Token has expired',
'error' => 'Token has expired',
'data' => json_decode("{}"),
], $exception->getStatusCode());
case \Tymon\JWTAuth\Exceptions\TokenInvalidException::class:
case \Tymon\JWTAuth\Exceptions\TokenBlacklistedException::class:
return response()->json([
'status' => 'error',
'message' => 'Token is invalid',
'error' => 'Token is invalid',
'data' => json_decode("{}"),
], $exception->getStatusCode());
default:
......@@ -88,6 +88,17 @@ class Handler extends ExceptionHandler
* All instances of GeneralException redirect back with a flash message to show a bootstrap alert-error
*/
if ($exception instanceof GeneralException) {
//Note:Below code is required when we use an extra class as api request then we need to pass accept:application/json in the header also
//if the header has accept application/json then $request->wantsJson() returns true
// if ($request->ajax() || $request->wantsJson()){
// $json = [
// 'success' => false,
// 'error' => [
// 'message' => $exception->getMessage(),
// ],
// ];
// return response()->json($json, 400);
// }
return redirect()->back()->withInput()->withFlashDanger($exception->getMessage());
}
......
......@@ -178,6 +178,20 @@ class APIController extends Controller
return $this->setStatusCode(204)->respond(null);
}
/**Note this function is same as the below function but instead of responding with error below function returns error json
* Throw Validation.
*
* @param string $message
*
* @return mix
*/
// public function throwValidation($message)
// {
// return $this->setStatusCode(422)
// ->respondWithError($message);
// }
/**
* Throw Validation.
*
......@@ -185,9 +199,8 @@ class APIController extends Controller
*
* @return mix
*/
public function throwValidation($message)
public function throwValidation($validation)
{
return $this->setStatusCode(422)
->respondWithError($message);
return ["error"=>$validation->errors()];
}
}
......@@ -6,7 +6,9 @@ use App\Http\Resources\BlogTagsResource;
use App\Models\BlogTags\BlogTag;
use App\Repositories\Backend\BlogTags\BlogTagsRepository;
use Illuminate\Http\Request;
use App\Http\Requests\Backend\BlogTags\StoreApiBlogTagsRequest;
use Validator;
use Exception;
class BlogTagsController extends APIController
{
......@@ -67,6 +69,22 @@ class BlogTagsController extends APIController
return new BlogTagsResource(BlogTag::orderBy('created_at', 'desc')->first());
}
/** NOTE This function is same as about but uses StoreApiBlogTagsRequest for validation of the api
* Creates the Resource for BlogTag.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*
*
*/
// public function store(StoreApiBlogTagsRequest $request)
// {
// $this->repository->create($request->all());
// return new BlogTagsResource(BlogTag::orderBy('created_at', 'desc')->first());
// }
/**
* @param BlogTag $blog_tag
......@@ -79,7 +97,7 @@ class BlogTagsController extends APIController
$validation = $this->validatingRequest($request, $blog_tag->id);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
return $this->throwValidation($validation);
}
$this->repository->update($blog_tag, $request->all());
......
<?php
namespace App\Http\Requests\Backend\BlogTags;
use App\Http\Requests\Request;
/**
* Class StoreBlogTagsRequest.
*/
class StoreApiBlogTagsRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:191',
];
}
/**
* Get the custom validation messages.
*
* @return array
*/
public function messages()
{
return [
'name.required' => 'Blog Tag name is a required field.111111',
'name.max' => 'Blog Tag may not be greater than 191 characters.',
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment