Files
portal-api/app/Http/Controllers/admin/PostController.php
T
pbao9 17b02631e4 Refactor views to use new layout structure and update routes for administrator pages
- Changed view extends from "admin.main" to "administrator.layout.master" across multiple files.
- Updated route names in admin.php and administrator.php to reflect new structure for user, order, invoice, and other management functionalities.
- Adjusted view includes to point to the new directory structure for blocks and components.
- Ensured all permission checks remain intact while refactoring the views.
2026-02-02 10:31:11 +07:00

112 lines
3.1 KiB
PHP

<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\PostRequest;
use App\Models\Post;
use App\Repositories\Post\PostRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
public function __construct(
public PostRepositoryInterface $postRepository
) {}
public function index(Request $request)
{
$title = 'Posts';
$posts = $this->postRepository->filter($request->merge(['page' => $request->get('page') ?? 1])->all());
return view('administrator.pages.post.index', compact('title', 'posts'));
}
public function create()
{
$title = 'Add new Post';
return view('administrator.pages.post.add', compact('title'));
}
public function store(PostRequest $request)
{
$request->validated();
$isExits = $this->postRepository->findBy('slug', $request->get('slug'));
if ($isExits) {
return back()->withErrors('The Slug already exits');
}
DB::beginTransaction();
try {
$this->postRepository->create($request->all());
DB::commit();
} catch (\Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('post.index')->with('success', 'Added successfully');
}
public function edit(int $id = 0)
{
$post = $this->postRepository->find($id);
if (!$post) {
return back()->withErrors('Invalid request');
}
$title = 'Edit Post';
return view('administrator.pages.post.edit', compact('title', 'post'));
}
public function update(int $id = 0,PostRequest $request)
{
$post = $this->postRepository->find($id);
if (!$post) {
return back()->withErrors('Invalid request');
}
$request->validated();
if($request->slug !== $post->slug)
{
$isExits = $this->postRepository->findBy('slug', $request->get('slug'));
if ($isExits) {
return back()->withErrors('The Slug already exits');
}
}
DB::beginTransaction();
try {
$this->postRepository->update($id,$request->all());
DB::commit();
} catch (\Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('post.index')->with('success', 'Updated successfully');
}
public function destroy(int $id = 0)
{
$post = $this->postRepository->find($id);
if (!$post) {
return back()->withErrors('Invalid request');
}
DB::beginTransaction();
try {
$this->postRepository->delete($id);
DB::commit();
} catch (\Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('post.index')->with('success', 'Deleted successfully');
}
}