132 lines
3.3 KiB
PHP
132 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Post\app\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Modules\Page\app\Models\Page;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Modules\Post\app\Http\Requests\PostRequest;
|
|
use Modules\Post\app\Repositories\PostRepository;
|
|
|
|
class PostController extends Controller
|
|
{
|
|
protected $postRepo;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->postRepo = new PostRepository();
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$perPage = $request->has('per-page') ? $request->input('per-page') : null;
|
|
$filter = $request->has('filter') ? $request->input('filter') : [];
|
|
$data['posts'] = $this->postRepo->findAll($perPage, $filter);
|
|
|
|
return view('post::index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['pageList'] = Page::pluck('title', 'id');
|
|
|
|
return view('post::create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(PostRequest $request): RedirectResponse
|
|
{
|
|
try {
|
|
$validated = $request->validated();
|
|
|
|
$this->postRepo->create($validated);
|
|
toastr()->success('Data created successfully.');
|
|
|
|
return redirect()->route('post.index');
|
|
} catch (\Throwable $th) {
|
|
throw $th;
|
|
report($th);
|
|
toastr()->error('Something went wrong.');
|
|
|
|
return back();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return view('post::show');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$data['pageList'] = Page::pluck('title', 'id');
|
|
$data['post'] = $this->postRepo->findById($id);
|
|
if (!$data['post']) {
|
|
toastr()->success('post not found');
|
|
return back();
|
|
}
|
|
|
|
return view('post::edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update($id, PostRequest $request): RedirectResponse
|
|
{
|
|
try {
|
|
$validated = $request->validated();
|
|
$post = $this->postRepo->update($id, $validated);
|
|
if (! $post) {
|
|
toastr()->success('post not found');
|
|
return back();
|
|
}
|
|
toastr()->success('Data updated successfully.');
|
|
|
|
return redirect()->route('post.index');
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Oops! Something went wrong.');
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$post = $this->postRepo->delete($id);
|
|
if (! $post) {
|
|
toastr()->error('post not found');
|
|
|
|
return back();
|
|
}
|
|
toastr()->success('Data deleted successfully.');
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Oops! Something went wrong.');
|
|
}
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|