Update source into github
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
<?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('admin.post.index', compact('title', 'posts'));
|
||||
}
|
||||
public function create()
|
||||
{
|
||||
$title = 'Add new Post';
|
||||
|
||||
return view('admin.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('admin.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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user