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 all 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
228 changes: 58 additions & 170 deletions app/Http/Controllers/Api/V1/Testimonial/TestimonialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,206 +6,94 @@
use App\Http\Requests\StoreTestimonialRequest;
use App\Http\Requests\UpdateTestimonialRequest;
use App\Models\Testimonial;
use App\Helpers\ResponseHelper;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Response;

class TestimonialController extends Controller
{

/**
* Display a listing of the resource.
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
{
$this->authorize('viewAny', Testimonial::class);

try {
$testimonials = Testimonial::all();
return ResponseHelper::response('Testimonials fetched successfully', Response::HTTP_OK, collect($testimonials));
} catch (\Exception $e) {
return ResponseHelper::response('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,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ResponseHelper::response('Testimonial created successfully', Response::HTTP_CREATED, $testimonial->toArray());
} catch (\Exception $e) {
return response()->json([
'status' => 'Internal Server Error',
'message' => 'Internal Server Error. Please try again later.',
'status_code' => 500,
], 500);
return ResponseHelper::response('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)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateTestimonialRequest $request, Testimonial $testimonial)
public function show($id)
{
//
try {
$testimonial = Testimonial::findOrFail($id);
$this->authorize('view', $testimonial);

return ResponseHelper::response('Testimonial fetched successfully', Response::HTTP_OK, $testimonial->toArray());
} catch (ModelNotFoundException $e) {
return ResponseHelper::response('Testimonial not found.', Response::HTTP_NOT_FOUND);
} catch (\Exception $e) {
return ResponseHelper::response('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();

try {
$testimonial = Testimonial::findOrFail($id);
$this->authorize('update', $testimonial);

if (!$user) {
return response()->json([
'status' => 'Unauthorized',
'message' => 'Unauthorized. Please log in.',
'status_code' => 401,
], 401);
}
$testimonial->update([
'content' => $request->get('content')
]);

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 ResponseHelper::response('Testimonial updated successfully', Response::HTTP_OK, $testimonial->toArray());
} catch (ModelNotFoundException $e) {
return ResponseHelper::response('Testimonial not found.', Response::HTTP_NOT_FOUND);
} catch (\Exception $e) {
return ResponseHelper::response('Internal Server Error. Please try again later.', Response::HTTP_INTERNAL_SERVER_ERROR, ['error' => $e->getMessage()]);
}
}

public function destroy($id)
{
try {
$testimonial = Testimonial::findOrFail($id);
$this->authorize('delete', $testimonial);

$testimonial->delete();
return ResponseHelper::response('Testimonial deleted successfully', Response::HTTP_OK);
} catch (ModelNotFoundException $e) {
return response()->json([
'status' => 'Not Found',
'message' => 'Testimonial not found.',
'status_code' => 404,
], 404);
return ResponseHelper::response('Testimonial not found.', Response::HTTP_NOT_FOUND);
} catch (\Illuminate\Auth\Access\AuthorizationException $e) {
return ResponseHelper::response('You do not have the required permissions to perform this action.', Response::HTTP_FORBIDDEN);
} catch (\Exception $e) {
return ResponseHelper::response('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