-
Notifications
You must be signed in to change notification settings - Fork 64
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
Feature/testimonial UUID crud #631
base: dev
Are you sure you want to change the base?
Changes from 6 commits
268a8e2
84c4839
9140473
79abc8c
84b0618
6358a26
339ff46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,206 +6,102 @@ | |
use App\Http\Requests\StoreTestimonialRequest; | ||
use App\Http\Requests\UpdateTestimonialRequest; | ||
use App\Models\Testimonial; | ||
use App\Traits\ApiResponse; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Http\Response; | ||
|
||
class TestimonialController extends Controller | ||
{ | ||
use ApiResponse; | ||
|
||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index() | ||
public function __construct() | ||
{ | ||
// | ||
$this->middleware('auth:api'); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
*/ | ||
public function create() | ||
public function index() | ||
{ | ||
// | ||
$this->authorize('viewAny', Testimonial::class); | ||
|
||
try { | ||
$testimonials = Testimonial::all(); | ||
return response()->json($this->successResponse('Testimonials fetched successfully', $testimonials->toArray())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is testimonial being converted to array? collection works just fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array and collect are same depends on person There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but i have used collect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its a bad code |
||
} catch (\Exception $e) { | ||
return response()->json($this->errorResponse('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()])); | ||
} | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(StoreTestimonialRequest $request) | ||
{ | ||
$user = Auth::user(); | ||
|
||
if (!$user) { | ||
return response()->json([ | ||
'status' => 'Unauthorized', | ||
'message' => 'Unauthorized. Please log in.', | ||
'status_code' => 401, | ||
], 401); | ||
} | ||
|
||
$this->authorize('create', Testimonial::class); | ||
|
||
try { | ||
$user = Auth::user(); | ||
$name = $request->get('name') ?? $user->name; | ||
if (empty($name)) { | ||
$name = 'Anonymous User'; | ||
} | ||
|
||
$testimonial = Testimonial::create([ | ||
'user_id' => $user->id, | ||
'name' => $user->name, | ||
'name' => $name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name not given for testimonial should be considered anonymous by default. i believe the logic is fine |
||
'content' => $request->get('content'), | ||
]); | ||
|
||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Testimonial created successfully', | ||
'data' => $testimonial, | ||
], 201); | ||
|
||
return response()->json($this->successResponse('Testimonial created successfully', $testimonial->toArray()), Response::HTTP_CREATED); | ||
} catch (\Exception $e) { | ||
return response()->json([ | ||
'status' => 'Internal Server Error', | ||
'message' => 'Internal Server Error. Please try again later.', | ||
'status_code' => 500, | ||
], 500); | ||
return response()->json($this->errorResponse('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()])); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
|
||
|
||
// public function show(Testimonial $testimonial_id) | ||
// { | ||
// $user = Auth::user(); | ||
|
||
// if (!$user) { | ||
// return response()->json([ | ||
// 'status' => 'Unauthorized', | ||
// 'message' => 'Unauthorized. Please log in.', | ||
// 'status_code' => 401, | ||
// ], 401); | ||
// } | ||
|
||
// $testimonial = Testimonial::find($testimonial_id); | ||
|
||
// if (!$testimonial) { | ||
// return response()->json([ | ||
// 'status' => 'Not Found', | ||
// 'message' => 'Testimonial not found.', | ||
// 'status_code' => 404, | ||
// ], 404); | ||
// } | ||
|
||
// return response()->json([ | ||
// 'status' => 'success', | ||
// 'message' => 'Testimonial fetched successfully', | ||
// 'data' => $testimonial, | ||
// ], 200); | ||
// } | ||
|
||
// public function show(Testimonial $testimonial) | ||
// { | ||
// $user = Auth::user(); | ||
|
||
// if (!$user) { | ||
// return response()->json([ | ||
// 'status' => 'Unauthorized', | ||
// 'message' => 'Unauthorized. Please log in.', | ||
// 'status_code' => 401, | ||
// ], 401); | ||
// } | ||
|
||
// return response()->json([ | ||
// 'status' => 'success', | ||
// 'message' => 'Testimonial fetched successfully', | ||
// 'data' => $testimonial, | ||
// ], 200); | ||
// } | ||
|
||
|
||
public function show($id) | ||
{ | ||
$user = Auth::user(); | ||
|
||
if (!$user) { | ||
return response()->json([ | ||
'status' => 'Unauthorized', | ||
'message' => 'Unauthorized. Please log in.', | ||
'status_code' => 401, | ||
], 401); | ||
} | ||
|
||
try { | ||
$testimonial = Testimonial::findOrFail($id); | ||
} catch (ModelNotFoundException $e) { | ||
return response()->json([ | ||
'status' => 'Not Found', | ||
'message' => 'Testimonial not found.', | ||
'status_code' => 404, | ||
], 404); | ||
} | ||
|
||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Testimonial fetched successfully', | ||
'data' => $testimonial, | ||
], 200); | ||
} | ||
|
||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(Testimonial $testimonial) | ||
public function show($id) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(UpdateTestimonialRequest $request, Testimonial $testimonial) | ||
{ | ||
// | ||
try { | ||
$testimonial = Testimonial::findOrFail($id); | ||
$this->authorize('view', $testimonial); | ||
|
||
return response()->json($this->successResponse('Testimonial fetched successfully', $testimonial->toArray())); | ||
} catch (ModelNotFoundException $e) { | ||
return response()->json($this->errorResponse('Testimonial not found.', Response::HTTP_NOT_FOUND)); | ||
} catch (\Exception $e) { | ||
return response()->json($this->errorResponse('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()])); | ||
} | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy($id) | ||
public function update(UpdateTestimonialRequest $request, $id) | ||
{ | ||
$user = Auth::user(); | ||
|
||
|
||
if (!$user) { | ||
return response()->json([ | ||
'status' => 'Unauthorized', | ||
'message' => 'Unauthorized. Please log in.', | ||
'status_code' => 401, | ||
], 401); | ||
} | ||
|
||
if ($user->role !== 'admin') { | ||
return response()->json([ | ||
'status' => 'Forbidden', | ||
'message' => 'You do not have the required permissions to perform this action.', | ||
'status_code' => 403, | ||
], 403); | ||
} | ||
|
||
try { | ||
$testimonial = Testimonial::findOrFail($id); | ||
$testimonial->delete(); | ||
$this->authorize('update', $testimonial); | ||
|
||
$testimonial->update([ | ||
'content' => $request->get('content') | ||
]); | ||
|
||
return response()->json($this->successResponse('Testimonial updated successfully', $testimonial->toArray())); | ||
} catch (ModelNotFoundException $e) { | ||
return response()->json([ | ||
'status' => 'Not Found', | ||
'message' => 'Testimonial not found.', | ||
'status_code' => 404, | ||
], 404); | ||
return response()->json($this->errorResponse('Testimonial not found.', Response::HTTP_NOT_FOUND)); | ||
} catch (\Exception $e) { | ||
return response()->json($this->errorResponse('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()])); | ||
} | ||
|
||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Testimonial deleted successfully', | ||
'status_code' => 200, | ||
], 200); | ||
} | ||
|
||
public function destroy($id) | ||
{ | ||
try { | ||
$testimonial = Testimonial::findOrFail($id); | ||
$this->authorize('delete', $testimonial); | ||
|
||
$testimonial->delete(); | ||
return response()->json($this->successResponse('Testimonial deleted successfully')); | ||
} catch (ModelNotFoundException $e) { | ||
return response()->json($this->errorResponse('Testimonial not found.', Response::HTTP_NOT_FOUND)); | ||
} catch (\Illuminate\Auth\Access\AuthorizationException $e) { | ||
|
||
return response()->json($this->errorResponse('You do not have the required permissions to perform this action.', Response::HTTP_FORBIDDEN)); | ||
} catch (\Exception $e) { | ||
return response()->json($this->errorResponse('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()])); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UpdateTestimonialRequest extends FormRequest | ||
{ | ||
/** | ||
* 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<string, mixed> | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'content' => 'required|string|min:3|max:1000', | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be done on the route
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh bad it a mistake