File: /var/www/console.fixgini.com/app/Http/Controllers/Admin/TestimonyController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Testimony;
use Illuminate\Http\Request;
class TestimonyController extends Controller
{
public function list()
{
$testimony = Testimony::InRandomOrder()->limit(3)->get();
return response()->json(['status' => 'success', 'message' => 'Fetch successfully', 'data' => $testimony]);
}
public function store(Request $request)
{
try {
$validatedData = $request->validate([
'company_name' => 'nullable|string|unique:testimonies,company_name',
'company_role' => 'required|string',
'name' => 'required|string',
'message' => 'required|max:255',
'photo_url' => 'required|image|mimes:jpeg,png,jpg,gif|max:1025',
]);
if ($request->hasFile('photo_url')) {
$uploadedFile = $request->file('photo_url')->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'testimonyPhoto']);
$uploadedFileUrl = $uploadResult->getSecurePath();
$publicId = $uploadResult->getPublicId();
}
$testimony = Testimony::create([
'company_name' => ucwords($validatedData['company_name']),
'company_role' => ucfirst($validatedData['company_role']),
'name' => ucfirst($validatedData['name']),
'message' => ucfirst($validatedData['message']),
'photo_url' => $uploadedFileUrl ?? NULL,
'photo_public_id' => $publicId ?? NULL,
]);
return response()->json(['status' => 'success', 'message' => 'Testimony created successfully', 'testimony' => $testimony], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
}
}
public function update(Request $request, $id)
{
try {
$validatedData = $request->validate([
'company_name' => 'nullable|unique:testimonies,title',
'company_role' => 'required|unique:testimonies,subtitle',
'name' => 'required|unique:testimonies,subtitle',
'message' => 'required|unique:testimonies,subtitle',
'photo_url' => 'sometimes|image|mimes:jpeg,png,jpg,gif|max:1025',
]);
$testimony = Testimony::findOrFail($id);
// If icon is uploaded, store it
if ($request->hasFile('photo_url')) {
$uploadedFile = $request->file('photo_url')->getRealPath();
$uploadResult = cloudinary()->upload($uploadedFile, ['folder' => 'testimonyPhoto']);
$uploadedFileUrl = $uploadResult->getSecurePath();
$publicId = $uploadResult->getPublicId();
// Delete the old icon file and public ID if they exist
if ($testimony->photo_public_id) {
cloudinary()->destroy($testimony->photo_public_id);
}
} else {
$uploadedFileUrl = $testimony->photo_url;
$publicId = $testimony->photo_public_id;
}
$testimony->update([
'company_name' => ucwords($validatedData['company_name']),
'company_role' => ucfirst($validatedData['company_role']),
'name' => ucfirst($validatedData['name']),
'message' => ucfirst($validatedData['message']),
'photo_url' => $uploadedFileUrl,
'photo_public_id' => $publicId,
]);
return response()->json(['status' => 'success', 'message' => 'Hero Banner updated successfully', 'testimony' => $testimony], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 'error', 'message' => $th->getMessage()], 500);
}
}
public function saveReview(Request $request)
{
try {
$validatedData = $request->validate([
'user_id' => 'required|string',
'gig_id' => 'required|string',
'comment' => 'required|string',
'star_rating' => 'required|string',
'photo_url' => 'nullable|string',
'photo_public_id' => 'nullable|string',
]);
} catch (\Throwable $th) {
return response()->json(['status' => 'error', 'message' => $th->getMessage()], 400);
}
}
}