diff --git a/app/Enums/CommentStatus.php b/app/Enums/CommentStatus.php index 02d63ca..5c16c98 100644 --- a/app/Enums/CommentStatus.php +++ b/app/Enums/CommentStatus.php @@ -9,4 +9,5 @@ enum CommentStatus: string case PASSIVE = 'passive'; case PENDING = 'pending'; case TRASH = 'trash'; + case ANALYZING = 'analyzing'; } diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 0f4f733..a8cd8ae 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -5,35 +5,54 @@ use App\Http\Requests\CommentRequest; use App\Http\Resources\CommentResource; use App\Models\Comment; +use App\Repositories\CommentRepositoryInterface; class CommentController extends Controller { + public function __construct(protected CommentRepositoryInterface $repository) + { + } + public function index() { - return CommentResource::collection(Comment::all()); + return CommentResource::collection( + $this->repository->all() + ); } public function store(CommentRequest $request) { - return new CommentResource(Comment::create($request->validated())); + return new CommentResource( + $this->repository->create( + $request->validated() + ) + ); } public function show(Comment $comment) { - return new CommentResource($comment); + return new CommentResource( + $this->repository->find( + $comment->id + ) + ); } public function update(CommentRequest $request, Comment $comment) { - $comment->update($request->validated()); - return new CommentResource($comment); + return new CommentResource( + $this->repository->update( + $comment->id, + $request->validated() + ) + ); } public function delete(Comment $comment) { - $comment->delete(); - - return response()->json(); + return $this->repository->delete( + $comment->id + ); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..8013365 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Repositories\CommentRepository; +use App\Repositories\CommentRepositoryInterface; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -11,7 +13,7 @@ class AppServiceProvider extends ServiceProvider */ public function register(): void { - // + $this->app->bind(CommentRepositoryInterface::class, CommentRepository::class); } /** diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php new file mode 100644 index 0000000..7a6e2da --- /dev/null +++ b/app/Repositories/BaseRepository.php @@ -0,0 +1,40 @@ +model->all(); + } + + public function find(int|string $id): ?stdClass + { + return (object) $this->model->findOrFail($id)->toArray(); + } + + public function create(array $data): stdClass + { + return $this->model->create([$data])->toArray(); + } + + public function update(string|int $id, array $data): ?stdClass + { + return (object) tap($this->model->findOrFail($id))->update($data)->toArray(); + } + + public function delete(int|string $id): bool + { + return $this->model->findOrFail($id)->delete(); + } +} diff --git a/app/Repositories/BaseRepositoryInterface.php b/app/Repositories/BaseRepositoryInterface.php new file mode 100644 index 0000000..aa7bb83 --- /dev/null +++ b/app/Repositories/BaseRepositoryInterface.php @@ -0,0 +1,19 @@ +model->create([ + 'email' => $data['email'], + 'body' => $data['body'], + 'rate' => $data['rate'], + 'status' => $data['status'] ?? CommentStatus::ANALYZING->value, + ])->toArray(); + } +} diff --git a/app/Repositories/CommentRepositoryInterface.php b/app/Repositories/CommentRepositoryInterface.php new file mode 100644 index 0000000..641e2d6 --- /dev/null +++ b/app/Repositories/CommentRepositoryInterface.php @@ -0,0 +1,11 @@ +get('/api/comments')->assertSuccessful(); + } + public function test_stores_a_comment() { $this->post('/api/comments', [ @@ -16,9 +21,15 @@ public function test_stores_a_comment() ])->assertSuccessful(); } + public function test_view_a_comment() + { + $comment = Comment::latest()->first(); + $this->get('/api/comments/'.$comment->id)->assertSuccessful(); + } + public function test_updates_a_comment() { - $comment = Comment::first(); + $comment = Comment::latest()->first(); $this->put('/api/comments/'.$comment->id, [ 'body' => 'update comment | testing repository pattern', ])->assertSuccessful(); @@ -26,7 +37,7 @@ public function test_updates_a_comment() public function test_delete_a_comment() { - $comment = Comment::first(); + $comment = Comment::latest()->first(); $this->delete('/api/comments/'.$comment->id)->assertSuccessful(); } }