|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modules\Posts\Http\Controllers\Dashboard; |
| 4 | + |
| 5 | +use Modules\Posts\Entities\Post; |
| 6 | +use Illuminate\Contracts\View\View; |
| 7 | +use App\Http\Controllers\Controller; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Contracts\View\Factory; |
| 10 | +use Modules\Posts\Http\Requests\PostRequest; |
| 11 | +use Modules\Posts\Repositories\PostRepository; |
| 12 | +use Illuminate\Auth\Access\AuthorizationException; |
| 13 | +use Illuminate\Foundation\Validation\ValidatesRequests; |
| 14 | +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
| 15 | + |
| 16 | +class PostController extends Controller |
| 17 | +{ |
| 18 | + use AuthorizesRequests, ValidatesRequests; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var PostRepository |
| 22 | + */ |
| 23 | + protected PostRepository $repository; |
| 24 | + |
| 25 | + /** |
| 26 | + * PostController constructor. |
| 27 | + * @param PostRepository $repository |
| 28 | + */ |
| 29 | + public function __construct(PostRepository $repository) |
| 30 | + { |
| 31 | + $this->middleware('permission:read_posts')->only(['index']); |
| 32 | + $this->middleware('permission:create_posts')->only(['create', 'store']); |
| 33 | + $this->middleware('permission:update_posts')->only(['edit', 'update']); |
| 34 | + $this->middleware('permission:delete_posts')->only(['destroy']); |
| 35 | + $this->middleware('permission:show_posts')->only(['show']); |
| 36 | + $this->repository = $repository; |
| 37 | + $this->repository = $repository; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Display a listing of the resource. |
| 42 | + * @return Factory|View |
| 43 | + */ |
| 44 | + public function index(): Factory|View |
| 45 | + { |
| 46 | + $posts = $this->repository->all(); |
| 47 | + |
| 48 | + return view('posts::posts.index', compact('posts')); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Show the form for creating a new resource. |
| 53 | + * @return Factory|View |
| 54 | + */ |
| 55 | + public function create(): Factory|View |
| 56 | + { |
| 57 | + return view('posts::posts.create'); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Store a newly created resource in storage. |
| 62 | + * @param PostRequest $request |
| 63 | + * @return RedirectResponse |
| 64 | + */ |
| 65 | + public function store(PostRequest $request) |
| 66 | + { |
| 67 | + $post = $this->repository->create($request->all()); |
| 68 | + |
| 69 | + flash(trans('posts::posts.messages.created'))->success(); |
| 70 | + |
| 71 | + return redirect()->route('dashboard.posts.show', $post); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Show the specified resource. |
| 76 | + * @param Post $post |
| 77 | + * @return Factory|View |
| 78 | + * @throws AuthorizationException |
| 79 | + */ |
| 80 | + public function show(Post $post) |
| 81 | + { |
| 82 | + $post = $this->repository->find($post); |
| 83 | + |
| 84 | + return view('posts::posts.show', compact('post')); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Show the form for editing the specified resource. |
| 89 | + * @param Post $post |
| 90 | + * @return Factory|View |
| 91 | + */ |
| 92 | + public function edit(Post $post) |
| 93 | + { |
| 94 | + return view('posts::posts.edit', compact('post')); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Update the specified resource in storage. |
| 99 | + * @param PostRequest $request |
| 100 | + * @param Post $post |
| 101 | + * @return RedirectResponse |
| 102 | + */ |
| 103 | + public function update(PostRequest $request, Post $post) |
| 104 | + { |
| 105 | + $post = $this->repository->update($post, $request->all()); |
| 106 | + |
| 107 | + flash(trans('posts::posts.messages.updated'))->success(); |
| 108 | + |
| 109 | + return redirect()->route('dashboard.posts.show', $post); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Remove the specified resource from storage. |
| 114 | + * @param Post $post |
| 115 | + * @return RedirectResponse |
| 116 | + */ |
| 117 | + public function destroy(Post $post) |
| 118 | + { |
| 119 | + $this->repository->delete($post); |
| 120 | + |
| 121 | + flash(trans('posts::posts.messages.deleted'))->error(); |
| 122 | + |
| 123 | + return redirect()->route('dashboard.posts.index'); |
| 124 | + } |
| 125 | +} |
0 commit comments