0.1: replace Request with FormRequest in all controllers
Deploy Backend / deploy (push) Has been cancelled
Deploy Backend / deploy (push) Has been cancelled
This commit is contained in:
1 parent
b7e85f80f7
commit
a5f76a3657
15 files changed
+277
-107
No files matched your search
@@ -3,10 +3,11 @@
|
||||
namespace App\Http\Controllers\Blog;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Blog\StorePostRequest;
|
||||
use App\Http\Requests\Blog\UpdatePostRequest;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Models\Blog\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
@@ -51,28 +52,9 @@ class PostController extends Controller
|
||||
}
|
||||
|
||||
// ذخیره یک پست جدید
|
||||
public function store(Request $request)
|
||||
public function store(StorePostRequest $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'required|string|max:255',
|
||||
'body' => 'required|string',
|
||||
'excerpt' => 'nullable|string|max:500',
|
||||
'slug' => 'required|string|unique:posts,slug',
|
||||
'author_id' => 'required|exists:users,id',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
'featured_image' => 'nullable|url',
|
||||
'views' => 'nullable|integer',
|
||||
'likes' => 'nullable|integer',
|
||||
'published_at' => 'nullable|date',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'exists:tags,id',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$post = Post::create($validator->validated());
|
||||
$post = Post::create($request->validated());
|
||||
|
||||
if ($request->has('tags')) {
|
||||
$post->tags()->attach($request->tags);
|
||||
@@ -82,30 +64,10 @@ class PostController extends Controller
|
||||
}
|
||||
|
||||
// بهروزرسانی یک پست خاص
|
||||
public function update(Request $request, $id)
|
||||
public function update(UpdatePostRequest $request, $id)
|
||||
{
|
||||
$post = Post::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($request->all(), [
|
||||
'title' => 'sometimes|required|string|max:255',
|
||||
'body' => 'sometimes|required|string',
|
||||
'excerpt' => 'nullable|string|max:500',
|
||||
'slug' => 'sometimes|required|string|unique:posts,slug,' . $post->id,
|
||||
'author_id' => 'sometimes|required|exists:users,id',
|
||||
'category_id' => 'sometimes|required|exists:categories,id',
|
||||
'featured_image' => 'nullable|url',
|
||||
'views' => 'nullable|integer',
|
||||
'likes' => 'nullable|integer',
|
||||
'published_at' => 'nullable|date',
|
||||
'tags' => 'nullable|array',
|
||||
'tags.*' => 'exists:tags,id',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json($validator->errors(), 422);
|
||||
}
|
||||
|
||||
$post->update($validator->validated());
|
||||
$post->update($request->validated());
|
||||
|
||||
if ($request->has('tags')) {
|
||||
$post->tags()->sync($request->tags);
|
||||
|
||||
Reference in new issue
Block a user