-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ali Özen
committed
Apr 26, 2023
1 parent
e360b85
commit cd0827c
Showing
12 changed files
with
205 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
enum CommentStatus: string | ||
{ | ||
case DRAFT = 'draft'; | ||
case ACTIVE = 'active'; | ||
case PASSIVE = 'passive'; | ||
case PENDING = 'pending'; | ||
case TRASH = 'trash'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\CommentRequest; | ||
use App\Http\Resources\CommentResource; | ||
use App\Models\Comment; | ||
|
||
class CommentController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
return CommentResource::collection(Comment::all()); | ||
} | ||
|
||
public function store(CommentRequest $request) | ||
{ | ||
return new CommentResource(Comment::create($request->validated())); | ||
} | ||
|
||
public function show(Comment $comment) | ||
{ | ||
return new CommentResource($comment); | ||
} | ||
|
||
public function update(CommentRequest $request, Comment $comment) | ||
{ | ||
$comment->update($request->validated()); | ||
|
||
return new CommentResource($comment); | ||
} | ||
|
||
public function delete(Comment $comment) | ||
{ | ||
$comment->delete(); | ||
|
||
return response()->json(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Enums\CommentStatus; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rules\Enum; | ||
|
||
class CommentRequest extends FormRequest | ||
{ | ||
public function rules() | ||
{ | ||
return [ | ||
'email' => ['sometimes', 'email', 'max:254'], | ||
'body' => ['sometimes'], | ||
'rate' => ['sometimes', 'int', 'max:5', 'min:1'], | ||
'status' => ['nullable', new Enum(type: CommentStatus::class)], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
/** @mixin \App\Models\Comment */ | ||
class CommentResource extends JsonResource | ||
{ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'email' => $this->email, | ||
'body' => $this->body, | ||
'rate' => $this->rate, | ||
'status' => $this->status, | ||
'created_at' => $this->created_at, | ||
'updated_at' => $this->updated_at, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Enums\CommentStatus; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Comment extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'comments'; | ||
|
||
protected $fillable = [ | ||
'email', | ||
'body', | ||
'rate', | ||
'status', | ||
]; | ||
|
||
protected $casts = [ | ||
'status' => CommentStatus::class, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Enums\CommentStatus; | ||
use App\Models\Comment; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CommentFactory extends Factory | ||
{ | ||
protected $model = Comment::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'email' => $this->faker->unique()->safeEmail(), | ||
'body' => $this->faker->paragraph(2), | ||
'rate' => $this->faker->numberBetween(1,5), | ||
'status' => CommentStatus::DRAFT->value, | ||
]; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
database/migrations/2023_04_25_231018_create_comments_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use App\Enums\CommentStatus; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('comments', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('email'); | ||
$table->string('body'); | ||
$table->tinyInteger('rate'); | ||
$table->string('status')->nullable()->default(CommentStatus::DRAFT->value); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('comments'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
namespace Database\Seeders; | ||
|
||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents; | ||
use App\Models\Comment; | ||
use Illuminate\Database\Seeder; | ||
|
||
class DatabaseSeeder extends Seeder | ||
|
@@ -12,11 +12,6 @@ class DatabaseSeeder extends Seeder | |
*/ | ||
public function run(): void | ||
{ | ||
// \App\Models\User::factory(10)->create(); | ||
|
||
// \App\Models\User::factory()->create([ | ||
// 'name' => 'Test User', | ||
// 'email' => '[email protected]', | ||
// ]); | ||
Comment::factory(10)->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,9 @@ | ||
<?php | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Route; | ||
use App\Http\Controllers\CommentController; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| API Routes | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here is where you can register API routes for your application. These | ||
| routes are loaded by the RouteServiceProvider and all of them will | ||
| be assigned to the "api" middleware group. Make something great! | ||
| | ||
*/ | ||
|
||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { | ||
return $request->user(); | ||
}); | ||
Route::get('/comments', [CommentController::class, 'index']); | ||
Route::get('/comments/{comment}', [CommentController::class, 'show']); | ||
Route::post('/comments', [CommentController::class, 'store']); | ||
Route::put('/comments/{comment}', [CommentController::class, 'update']); | ||
Route::delete('/comments/{comment}', [CommentController::class, 'delete']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Api; | ||
|
||
use App\Models\Comment; | ||
use Tests\TestCase; | ||
|
||
class CommentTest extends TestCase | ||
{ | ||
public function test_stores_a_comment() | ||
{ | ||
$this->post('/api/comments', [ | ||
'email' => '[email protected]', | ||
'body' => 'create comment | testing repository pattern', | ||
'rate' => 5, | ||
])->assertSuccessful(); | ||
} | ||
|
||
public function test_updates_a_comment() { | ||
$comment = Comment::first(); | ||
$this->put('/api/comments/' . $comment->id, [ | ||
'body' => 'update comment | testing repository pattern' | ||
])->assertSuccessful(); | ||
} | ||
|
||
public function test_delete_a_comment() { | ||
$comment = Comment::first(); | ||
$this->delete('/api/comments/' . $comment->id)->assertSuccessful(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.