#4 conflict resolve
This commit is contained in:
17
app/Enums/PostStatus.php
Normal file
17
app/Enums/PostStatus.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum PostStatus: string
|
||||
{
|
||||
case Published = 'published';
|
||||
case Draft = 'draft';
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
PostStatus::Published => 'Published',
|
||||
PostStatus::Draft => 'Draft',
|
||||
};
|
||||
}
|
||||
}
|
||||
69
app/Http/Controllers/PostController.php
Normal file
69
app/Http/Controllers/PostController.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StorePostRequest;
|
||||
use App\Http\Requests\UpdatePostRequest;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class PostController extends Controller
|
||||
{
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$posts = Post::query()
|
||||
->where('user_id', $request->user()->id)
|
||||
->orderByDesc('created_at')
|
||||
->paginate(10, ['id', 'title', 'slug', 'status', 'created_at']);
|
||||
|
||||
return view('post.index', [
|
||||
'posts' => $posts,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
return view('post.create');
|
||||
}
|
||||
|
||||
public function store(StorePostRequest $request): RedirectResponse
|
||||
{
|
||||
$request->user()->posts()->create($request->validated());
|
||||
|
||||
return redirect()
|
||||
->route('post.index')
|
||||
->with('status', 'post-created');
|
||||
}
|
||||
|
||||
public function edit(Post $post): View
|
||||
{
|
||||
Gate::authorize('update', $post);
|
||||
|
||||
return view('post.edit', [
|
||||
'post' => $post,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(UpdatePostRequest $request, Post $post): RedirectResponse
|
||||
{
|
||||
$post->update($request->validated());
|
||||
|
||||
return redirect()
|
||||
->route('post.edit', $post)
|
||||
->with('status', 'post-updated');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Post $post): RedirectResponse
|
||||
{
|
||||
Gate::authorize('delete', $post);
|
||||
|
||||
$post->delete();
|
||||
|
||||
return redirect()
|
||||
->route('post.index')
|
||||
->with('status', 'post-deleted');
|
||||
}
|
||||
}
|
||||
29
app/Http/Requests/StorePostRequest.php
Normal file
29
app/Http/Requests/StorePostRequest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\PostStatus;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StorePostRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'content' => ['required', 'string'],
|
||||
'slug' => ['required', 'string', 'max:255', 'regex:/^[a-z0-9-]+$/', Rule::unique('posts', 'slug')],
|
||||
'status' => ['required', Rule::enum(PostStatus::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
29
app/Http/Requests/UpdatePostRequest.php
Normal file
29
app/Http/Requests/UpdatePostRequest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\PostStatus;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdatePostRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('update', $this->route('post'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'content' => ['required', 'string'],
|
||||
'slug' => ['required', 'string', 'max:255', 'regex:/^[a-z0-9-]+$/', Rule::unique('posts', 'slug')->ignore($this->route('post'))],
|
||||
'status' => ['required', Rule::enum(PostStatus::class)],
|
||||
];
|
||||
}
|
||||
}
|
||||
35
app/Models/Post.php
Normal file
35
app/Models/Post.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\PostStatus;
|
||||
use Database\Factories\PostFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
/** @use HasFactory<PostFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'title',
|
||||
'content',
|
||||
'slug',
|
||||
'status',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'status' => PostStatus::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -38,4 +39,9 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->belongsToMany(Role::class);
|
||||
}
|
||||
|
||||
public function posts(): HasMany
|
||||
{
|
||||
return $this->hasMany(Post::class);
|
||||
}
|
||||
}
|
||||
|
||||
34
app/Policies/PostPolicy.php
Normal file
34
app/Policies/PostPolicy.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\User;
|
||||
|
||||
class PostPolicy
|
||||
{
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function view(User $user, Post $post): bool
|
||||
{
|
||||
return $user->id === $post->user_id;
|
||||
}
|
||||
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function update(User $user, Post $post): bool
|
||||
{
|
||||
return $user->id === $post->user_id;
|
||||
}
|
||||
|
||||
public function delete(User $user, Post $post): bool
|
||||
{
|
||||
return $user->id === $post->user_id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user