diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index b19408d..ef207f2 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -2,8 +2,7 @@ namespace App\Http\Controllers; -use App\Models\Post; -use Illuminate\Http\Request; +use App\Http\Requests\PostRequest; class PostController extends Controller { @@ -12,8 +11,8 @@ public function create() return view('posts.create'); } - public function storeDataWithoutValidation() + public function store(PostRequest $request) { - Post::create(\request()->all()); + auth()->user()->posts()->create($request->validated()); } } diff --git a/app/Http/Requests/PostRequest.php b/app/Http/Requests/PostRequest.php new file mode 100644 index 0000000..e9f4ba1 --- /dev/null +++ b/app/Http/Requests/PostRequest.php @@ -0,0 +1,31 @@ + + */ + public function rules() + { + return [ + 'title' => '', + 'description' => '' + ]; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 23b4063..a369592 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,4 +41,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function posts() + { + return $this->hasMany(Post::class); + } } diff --git a/database/migrations/2022_12_08_152740_create_posts_table.php b/database/migrations/2022_12_08_152740_create_posts_table.php index 5b74819..a125cd9 100644 --- a/database/migrations/2022_12_08_152740_create_posts_table.php +++ b/database/migrations/2022_12_08_152740_create_posts_table.php @@ -15,9 +15,15 @@ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); + $table->unsignedBigInteger('user_id'); $table->string('title'); $table->text('description'); $table->timestamps(); + + $table->foreign('user_id') + ->references('id') + ->on('users') + ->cascadeOnDelete(); }); } diff --git a/routes/web.php b/routes/web.php index 148c506..9626572 100644 --- a/routes/web.php +++ b/routes/web.php @@ -27,6 +27,6 @@ Route::prefix('posts')->controller(PostController::class)->group(function() { Route::get('/create', 'create')->name('posts.create'); - Route::post('/store-simple-form-data', 'storeDataWithoutValidation')->name('posts.store.data.without.validation'); + Route::post('/store', 'store')->name('posts.store'); }); }); diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 1eafba6..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,21 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/FormTest.php b/tests/Feature/FormTest.php deleted file mode 100644 index 7ee5180..0000000 --- a/tests/Feature/FormTest.php +++ /dev/null @@ -1,44 +0,0 @@ -get(route('posts.create'))->assertRedirect(route('login')); - } - - /** - * Simple form test - * - * Test creating a post without validation - * - * - Log in as a user - * - Make a post request with data created using factory - * - Assert database has those data - */ - public function test_a_user_can_create_a_post() - { - $this->actingAs(User::factory()->create()); - - $this->post( - route('posts.store.data.without.validation'), - $attributes = Post::factory()->raw() - ); - - $this->assertDatabaseHas('posts', $attributes); - } -} diff --git a/tests/Feature/PostModuleTest.php b/tests/Feature/PostModuleTest.php new file mode 100644 index 0000000..cc99335 --- /dev/null +++ b/tests/Feature/PostModuleTest.php @@ -0,0 +1,25 @@ +withoutExceptionHandling(); + + $this->actingAs(User::factory()->create()); + + $this->post(route('posts.store'), $attributes = Post::factory()->raw()); + + $this->assertDatabaseHas('posts', $attributes); + } +} diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index e5c5fef..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,18 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php new file mode 100644 index 0000000..437b318 --- /dev/null +++ b/tests/Unit/UserTest.php @@ -0,0 +1,21 @@ +create(); + + $this->assertInstanceOf(Collection::class, $user->posts); + } +}