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

Feature/testimonial UUID crud #631

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 66 additions & 143 deletions app/Http/Controllers/Api/V1/Testimonial/TestimonialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@
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()
{
//
}
$user = Auth::user();
if (!$user) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking for authentication in each method, consider using middleware (like auth:api) to handle this globally

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok working on it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return response()->json($this->errorResponse('Unauthorized. Please log in.', Response::HTTP_UNAUTHORIZED));
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
try {
$testimonials = Testimonial::all();
return response()->json($this->successResponse('Testimonials fetched successfully', $testimonials->toArray()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is testimonial being converted to array? collection works just fine

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array and collect are same depends on person

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but i have used collect

Copy link
Contributor

Choose a reason for hiding this comment

The 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()]));
}
}

/**
Expand All @@ -35,177 +39,96 @@ public function create()
public function store(StoreTestimonialRequest $request)
{
$user = Auth::user();

if (!$user) {
return response()->json([
'status' => 'Unauthorized',
'message' => 'Unauthorized. Please log in.',
'status_code' => 401,
], 401);
return response()->json($this->errorResponse('Unauthorized. Please log in.', Response::HTTP_UNAUTHORIZED));
}

try {
$testimonial = Testimonial::create([
'user_id' => $user->id,
'name' => $user->name,
'name' => $request->get('name') ?? 'Anonymous User', // Use request name, fallback to 'Anonymous User'
'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(string $id)
{
//
$user = Auth::user();
if (!$user) {
return response()->json($this->errorResponse('Unauthorized. Please log in.', Response::HTTP_UNAUTHORIZED));
}

try {
$testimonial = Testimonial::findOrFail($id);
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()]));
}
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateTestimonialRequest $request, Testimonial $testimonial)
public function update(UpdateTestimonialRequest $request, string $id)
{
//
$user = Auth::user();
if (!$user) {
return response()->json($this->errorResponse('Unauthorized. Please log in.', Response::HTTP_UNAUTHORIZED));
}

try {
$testimonial = Testimonial::findOrFail($id);

// Check if the user owns this testimonial or is an admin
if ($testimonial->user_id !== $user->id && $user->role !== 'admin') {
return response()->json($this->errorResponse('You do not have permission to update this testimonial.', Response::HTTP_FORBIDDEN));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this update method, you check if the user owns the testimonial or is an admin. You can move this to a policy for better separation of concerns

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done created the policy

$testimonial->update([
'content' => $request->get('content')
]);

return response()->json($this->successResponse('Testimonial updated 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 destroy(string $id)
{
$user = Auth::user();


if (!$user) {
return response()->json([
'status' => 'Unauthorized',
'message' => 'Unauthorized. Please log in.',
'status_code' => 401,
], 401);
return response()->json($this->errorResponse('Unauthorized. Please log in.', Response::HTTP_UNAUTHORIZED));
}

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);
return response()->json($this->errorResponse('You do not have the required permissions to perform this action.', Response::HTTP_FORBIDDEN));
}

try {
$testimonial = Testimonial::findOrFail($id);
$testimonial->delete();

return response()->json($this->successResponse('Testimonial deleted successfully'));
} 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);
}

}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/UpdateTestimonialRequest.php
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',
];
}
}
36 changes: 35 additions & 1 deletion app/Models/Testimonial.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,49 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasUuids;

class Testimonial extends Model
{
use HasFactory;
use HasFactory, HasUuids;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'name',
'content',
];

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;

/**
* Indicates if the model's ID is not auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The "type" of the primary key ID.
*
* @var string
*/
protected $keyType = 'string';

/**
* Get the user that owns the testimonial.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Loading
Loading