skl/app/Http/Controllers/PostController.php

199 lines
6.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\PostRequest;
use App\Models\Kategori;
use App\Models\Post;
use App\Models\SubKategori;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
public function index()
{
$posts = Post::with(['kategori', 'subkategori'])->latest()->get();
// Transform posts to include proper image URLs
$posts = $posts->map(function ($post) {
if ($post->ImagePost) {
$post->ImagePost = Storage::url($post->ImagePost);
}
return $post;
});
$kategori = Kategori::all();
$subkategori = SubKategori::all();
return Inertia::render('admin/post/index_post', [
'posts' => $posts,
'kategori' => $kategori,
'subkategori' => $subkategori
]);
}
public function create()
{
$kategori = Kategori::all();
$subkategori = SubKategori::all();
return Inertia::render('admin/post/add_post', [
'kategori' => Kategori::all(),
'subkategori' => SubKategori::all(),
'existingPosts' => Post::select('JudulPost', 'KategoriId')->get()
]);
}
public function store(PostRequest $request)
{
try {
$data = $request->validated();
DB::beginTransaction();
try {
$data['IsPublish'] = $request->boolean('IsPublish');
if ($request->hasFile('ImagePost')) {
$file = $request->file('ImagePost');
if (!$file->isValid()) {
throw new \Exception('Invalid image file');
}
Log::info('Uploading file to Minio', [
'filename' => $file->getClientOriginalName(),
'mimetype' => $file->getMimeType(),
'size' => $file->getSize()
]);
$data['ImagePost'] = $file->store('images/posts', 's3');
Log::info('File uploaded to Minio', [
'path' => $data['ImagePost'],
'url' => Storage::url($data['ImagePost'])
]);
if ($data['ImagePost'] === false) {
throw new \Exception('Failed to store image');
}
}
$post = Post::create($data);
DB::commit();
return redirect()
->route('admin.post.index')
->with('success', 'Post berhasil ditambahkan');
} catch (\Exception $e) {
DB::rollBack();
if (isset($data['ImagePost']) && Storage::disk('s3')->exists($data['ImagePost'])) {
Storage::disk('s3')->delete($data['ImagePost']);
}
Log::error('Error creating post: ' . $e->getMessage());
return redirect()
->route('admin.post.index')
->with('error', 'Post gagal ditambahkan: ' . $e->getMessage());
}
} catch (\Exception $e) {
Log::error('Validation error: ' . $e->getMessage());
return redirect()
->route('admin.post.index')
->with('error', 'Validasi gagal: ' . $e->getMessage());
}
}
public function edit(Post $post)
{
// Debug the image path
Log::info('Image path:', ['path' => $post->ImagePost]);
return Inertia::render('admin/post/edit_post', [
'posting' => [
...$post->toArray(),
'ImagePost' => $post->ImagePost ? Storage::url($post->ImagePost) : null,
],
'kategori' => Kategori::all(),
'subkategori' => SubKategori::all(),
'existingPosts' => Post::where('PostId', '!=', $post->PostId)
->select('JudulPost', 'KategoriId')
->get()
]);
}
public function update(PostRequest $request, Post $post)
{
try {
DB::beginTransaction();
$data = $request->validated();
// Update gambar hanya jika ada file baru yang diupload
if ($request->hasFile('ImagePost')) {
// Hapus gambar lama jika ada
if ($post->ImagePost && Storage::disk('s3')->exists($post->ImagePost)) {
Log::info('Deleting old image from Minio', ['path' => $post->ImagePost]);
Storage::disk('s3')->delete($post->ImagePost);
}
$file = $request->file('ImagePost');
Log::info('Uploading new image to Minio', [
'filename' => $file->getClientOriginalName(),
'mimetype' => $file->getMimeType(),
'size' => $file->getSize()
]);
$data['ImagePost'] = $file->store('images/posts', 's3');
Log::info('New image uploaded to Minio', [
'path' => $data['ImagePost'],
'url' => Storage::url($data['ImagePost'])
]);
} else {
// Jika tidak ada file baru, jangan update field ImagePost
unset($data['ImagePost']);
}
// Pastikan nilai is_publish dikonversi ke boolean
$data['IsPublish'] = filter_var($request->input('IsPublish'), FILTER_VALIDATE_BOOLEAN);
$post->update($data);
DB::commit();
return redirect()->route('admin.post.index')->with('success', 'Post berhasil diperbarui.');
} catch (\Exception $e) {
DB::rollBack();
Log::error('Error updating Post: ' . $e->getMessage());
Log::info('Form data received:', [
'IsPublish' => $request->input('IsPublish'),
'IsPublishType' => gettype($request->input('IsPublish')),
'hasFile' => $request->hasFile('ImagePost')
]);
return back()->with('error', 'Terjadi kesalahan saat memperbarui post.');
}
}
public function destroy(Post $post)
{
try {
Storage::disk('s3')->delete($post->ImagePost);
$post->delete();
return redirect()->route('admin.post.index')->with('success', 'Post berhasil dihapus.');
} catch (\Exception $e) {
Log::error('Error deleting Post: ' . $e->getMessage());
return back()->with('error', 'Something went wrong.');
}
}
}