Skip to content

Commit

Permalink
Comment Api boiler template
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Özen committed Apr 26, 2023
1 parent e360b85 commit cd0827c
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 58 deletions.
12 changes: 12 additions & 0 deletions app/Enums/CommentStatus.php
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';
}
39 changes: 39 additions & 0 deletions app/Http/Controllers/CommentController.php
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();
}
}
20 changes: 20 additions & 0 deletions app/Http/Requests/CommentRequest.php
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)],
];
}
}
23 changes: 23 additions & 0 deletions app/Http/Resources/CommentResource.php
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,
];
}
}
25 changes: 25 additions & 0 deletions app/Models/Comment.php
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,
];
}
22 changes: 22 additions & 0 deletions database/factories/CommentFactory.php
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 database/migrations/2023_04_25_231018_create_comments_table.php
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');
}
};
9 changes: 2 additions & 7 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
22 changes: 6 additions & 16 deletions routes/api.php
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']);
30 changes: 30 additions & 0 deletions tests/Feature/Api/CommentTest.php
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();
}
}
19 changes: 0 additions & 19 deletions tests/Feature/ExampleTest.php

This file was deleted.

16 changes: 0 additions & 16 deletions tests/Unit/ExampleTest.php

This file was deleted.

0 comments on commit cd0827c

Please sign in to comment.