simedkom/app/Http/Controllers/Dashboard/ContentRecapController.php
myuqinurrohman 48560b9b3f feat: content recap classification
chore: add classification_id, social_media, and image field
2025-04-15 14:04:32 +07:00

106 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContentRecapRequest;
use App\Models\ContentRecap;
use App\Models\ContentRecapClassification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ContentRecapController extends Controller
{
public function index(){
return view('dashboard.content-recaps.index', [
'title' => 'Rekap Konten'
]);
}
public function create(){
return view('dashboard.content-recaps.create', [
'title' => 'Tambah Rekap Konten',
'classifications' => ContentRecapClassification::all()
]);
}
public function store(ContentRecapRequest $request){
$validatedData = $request->validated();
$validatedData['enhancer_id'] = Auth::id();
if($request->has('image')){
$validatedData['image'] = store_file($validatedData['image'], '/uploads/content-recaps', 'public')['randomFileName'];
}
$createdContentRecap = ContentRecap::create($validatedData);
if($createdContentRecap){
return redirect()->back()->with('success-status', 'Berhasil menambahkan rekap konten.');
}else{
return redirect()->back()->with('failed-status', 'Gagal menambahkan rekap konten.');
}
}
public function edit($id){
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
return view('dashboard.content-recaps.edit', [
'title' => 'Ubah Rekap Konten',
'contentRecap' => $contentRecap,
'classifications' => ContentRecapClassification::all()
]);
}
public function update(ContentRecapRequest $request, $id){
$validatedData = $request->validated();
$validatedData['enhancer_id'] = Auth::id();
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
if($request->has('image')){
delete_file('app/public/uploads/content-recaps/'.$contentRecap->image);
$validatedData['image'] = store_file($validatedData['image'], '/uploads/content-recaps', 'public')['randomFileName'];
}else{
$validatedData['image'] = $contentRecap->image;
}
$updatedContentRecap = $contentRecap->update($validatedData);
if($updatedContentRecap){
return redirect()->back()->with('success-status', 'Berhasil mengubah rekap konten.');
}else{
return redirect()->back()->with('failed-status', 'Gagal mengubah rekap konten.');
}
}
public function show($id){
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
return view('dashboard.content-recaps.show', [
'title' => 'Detail Rekap Konten',
'contentRecap' => $contentRecap
]);
}
public function destroy($id){
try {
$contentRecap = ContentRecap::findOrFail(decrypt_id($id));
$contentRecap->delete();
return response()->json(['message' => 'Berhasil menghapus rekap konten.']);
} catch (\Exception $e) {
return response()->json(['message' => 'Gagal menghapus rekap konten.'], 500);
}
}
public function checkLink(Request $request)
{
$linkExists = ContentRecap::where('link', $request->link)->exists();
return response()->json([
'exists' => $linkExists
]);
}
}