File: /var/www/console.fixgini.com/app/Http/Controllers/Seller/CertificationController.php
<?php
namespace App\Http\Controllers\Seller;
use Illuminate\Http\Request;
use App\Models\SellerCertificate;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use phpseclib3\File\ASN1\Maps\Certificate;
class CertificationController extends Controller
{
public function store(Request $request)
{
try {
$validatedData = $request->validate([
'user_id' => 'required|exists:shops,user_id',
'category_id' => 'required|exists:categories,id',
'title' => 'required|string|max:255',
'year_obtained' => 'nullable|digits:4',
'document' => 'required|url',
'document_public_url' => 'required|string',
]);
$certificate = SellerCertificate::create([
'user_id' => $validatedData['user_id'],
'category_id' => $validatedData['category_id'],
'title' => ucwords($validatedData['title']),
'year_obtained' => $validatedData['year_obtained'],
'document' => $validatedData['document'],
'document_public_url' => $validatedData['document_public_url'],
]);
return response()->json(['status' => 'success', 'message' => 'Seller certificate created successfully.', 'data' => $certificate], 200);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['status' => 'error', 'message' => 'Seller certificate not created successfully', 'data' => $e->getMessage()], 400);
}
}
public function list(Request $request)
{
try {
$user = Auth::user();
$certificate = SellerCertificate::with('category')->where('user_id', $user->id)->latest()->get();
info($certificate);
if ($certificate) {
return response()->json(['status' => 'success', 'message' => 'Certificate found successfully', 'data' => $certificate], 200);
}
} catch (\Throwable $th) {
return response()->json(['status' => 'success', 'message' => 'Certificate not found successfully', 'data' => $th->getMessage()], 400);
}
}
public function query(Request $request)
{
try {
$validatedData = $request->validate([
'uuid' => 'required|exists:seller_certificates,uuid',
]);
$certificate = SellerCertificate::with('category')->where('uuid', $validatedData['uuid'])->first();
if ($certificate) {
return response()->json(['status' => 'success', 'message' => 'Certificate found successfully', 'data' => $certificate], 200);
}
} catch (\Throwable $th) {
return response()->json(['status' => 'success', 'message' => 'Certificate not found successfully', 'data' => $th->getMessage()], 400);
}
}
public function update(Request $request)
{
try {
$validatedData = $request->validate([
'uuid' => 'required|exists:seller_certificates,uuid',
'category_id' => 'nullable|exists:categories,id',
'title' => 'nullable|string|max:255',
'year_obtained' => 'nullable|digits:4',
'document' => 'nullable',
'document_public_url' => 'nullable',
]);
$certificate = SellerCertificate::where('uuid', $validatedData['uuid'])->first();
if ($certificate) {
$certificate->update([
'category_id' => $validatedData['category_id'],
'title' => ucwords($validatedData['title']),
'year_obtained' => $validatedData['year_obtained'],
'document' => $validatedData['document'],
'document_public_url' => $validatedData['document_public_url'],
]);
return response()->json(['status' => 'success', 'message' => 'Seller certificate updated successfully.', 'data' => $certificate], 200);
}
return response()->json(['status' => 'error', 'message' => 'Seller certificate not found.'], 404);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['status' => 'error', 'message' => 'Seller certificate not updated successfully', 'data' => $e->getMessage()], 400);
}
}
public function destroy(Request $request)
{
try {
// Validate request
$validatedData = $request->validate([
'id' => 'required|exists:seller_certificates,id',
]);
// Find the gig by ID
$cert = SellerCertificate::find($validatedData['id']);
// Handle images
$imagePublicId = $cert->document_public_url;
// Delete images from Cloudinary
cloudinary()->destroy($imagePublicId);
// Delete the gig record
$cert->delete();
// Return success response
return response()->json(['status' => 'success', 'message' => 'Certificate deleted successfully.']);
} catch (\Throwable $th) {
// Return error response
return response()->json([
'status' => 'error',
'message' => 'Something went wrong. Please try again.',
'data' => $th->getMessage()
], 400);
}
}
}