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 6 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
234 changes: 65 additions & 169 deletions app/Http/Controllers/Api/V1/Testimonial/TestimonialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor

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

Copy link
Author

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

}

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

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