From 574bf336df056af6bba15ccc313ac2e687e8a53f Mon Sep 17 00:00:00 2001 From: Sameer Date: Tue, 13 Feb 2024 13:11:11 +0545 Subject: [PATCH] repository pattern --- app/Http/Controllers/OrderController.php | 70 +++++++++++++++++++ app/Http/Requests/StoreOrderRequest.php | 28 ++++++++ app/Http/Requests/UpdateOrderRequest.php | 28 ++++++++ app/Interfaces/OrderRepositoryInterface.php | 13 ++++ app/Models/Order.php | 11 +++ app/Policies/OrderPolicy.php | 66 +++++++++++++++++ app/Providers/RepositoryServiceProvider.php | 27 +++++++ app/Repositories/OrderRepository.php | 39 +++++++++++ config/app.php | 1 + database/factories/OrderFactory.php | 25 +++++++ .../2024_02_13_064344_create_orders_table.php | 30 ++++++++ database/seeders/DatabaseSeeder.php | 2 + database/seeders/OrderSeeder.php | 19 +++++ routes/api.php | 8 +++ routes/web.php | 1 + 15 files changed, 368 insertions(+) create mode 100644 app/Http/Controllers/OrderController.php create mode 100644 app/Http/Requests/StoreOrderRequest.php create mode 100644 app/Http/Requests/UpdateOrderRequest.php create mode 100644 app/Interfaces/OrderRepositoryInterface.php create mode 100644 app/Models/Order.php create mode 100644 app/Policies/OrderPolicy.php create mode 100644 app/Providers/RepositoryServiceProvider.php create mode 100644 app/Repositories/OrderRepository.php create mode 100644 database/factories/OrderFactory.php create mode 100644 database/migrations/2024_02_13_064344_create_orders_table.php create mode 100644 database/seeders/OrderSeeder.php diff --git a/app/Http/Controllers/OrderController.php b/app/Http/Controllers/OrderController.php new file mode 100644 index 0000000..5427659 --- /dev/null +++ b/app/Http/Controllers/OrderController.php @@ -0,0 +1,70 @@ +orderRepository = $orderRepository; + } + + public function index(): JsonResponse + { + return response()->json([ + 'data' => $this->orderRepository->getAllOrders() + ]); + } + + public function store(Request $request): JsonResponse + { + $orderDetails = $request->only([ + 'client', + 'details' + ]); + + return response()->json( + [ + 'data' => $this->orderRepository->createOrder($orderDetails) + ], + Response::HTTP_CREATED + ); + } + + public function show(Request $request): JsonResponse + { + $orderId = $request->route('id'); + + return response()->json([ + 'data' => $this->orderRepository->getOrderById($orderId) + ]); + } + + public function update(Request $request): JsonResponse + { + $orderId = $request->route('id'); + $orderDetails = $request->only([ + 'client', + 'details' + ]); + + return response()->json([ + 'data' => $this->orderRepository->updateOrder($orderId, $orderDetails) + ]); + } + + public function destroy(Request $request): JsonResponse + { + $orderId = $request->route('id'); + $this->orderRepository->deleteOrder($orderId); + + return response()->json(null, Response::HTTP_NO_CONTENT); + } +} diff --git a/app/Http/Requests/StoreOrderRequest.php b/app/Http/Requests/StoreOrderRequest.php new file mode 100644 index 0000000..7aed419 --- /dev/null +++ b/app/Http/Requests/StoreOrderRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Http/Requests/UpdateOrderRequest.php b/app/Http/Requests/UpdateOrderRequest.php new file mode 100644 index 0000000..2f86b44 --- /dev/null +++ b/app/Http/Requests/UpdateOrderRequest.php @@ -0,0 +1,28 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Interfaces/OrderRepositoryInterface.php b/app/Interfaces/OrderRepositoryInterface.php new file mode 100644 index 0000000..5e092b7 --- /dev/null +++ b/app/Interfaces/OrderRepositoryInterface.php @@ -0,0 +1,13 @@ +app->bind(OrderRepositoryInterface::class, OrderRepository::class); + } + + + /** + * Bootstrap services. + */ + public function boot(): void + { + // + } +} diff --git a/app/Repositories/OrderRepository.php b/app/Repositories/OrderRepository.php new file mode 100644 index 0000000..f66d641 --- /dev/null +++ b/app/Repositories/OrderRepository.php @@ -0,0 +1,39 @@ +update($newDetails); + } + + public function getFulfilledOrders() + { + return Order::where('is_fulfilled', true); + } +} diff --git a/config/app.php b/config/app.php index 9207160..53efc7d 100644 --- a/config/app.php +++ b/config/app.php @@ -159,6 +159,7 @@ return [ /* * Package Service Providers... */ + App\Providers\RepositoryServiceProvider::class, /* * Application Service Providers... diff --git a/database/factories/OrderFactory.php b/database/factories/OrderFactory.php new file mode 100644 index 0000000..e55bda9 --- /dev/null +++ b/database/factories/OrderFactory.php @@ -0,0 +1,25 @@ + + */ +class OrderFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'details' => $this->faker->sentences(4, true), + 'client' => $this->faker->name(), + 'is_fulfilled' => $this->faker->boolean(), + ]; + } +} diff --git a/database/migrations/2024_02_13_064344_create_orders_table.php b/database/migrations/2024_02_13_064344_create_orders_table.php new file mode 100644 index 0000000..8eb7ff5 --- /dev/null +++ b/database/migrations/2024_02_13_064344_create_orders_table.php @@ -0,0 +1,30 @@ +id(); + $table->text('details'); + $table->string('client'); + $table->boolean('is_fulfilled')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('orders'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index a9f4519..2c03711 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -18,5 +18,7 @@ class DatabaseSeeder extends Seeder // 'name' => 'Test User', // 'email' => 'test@example.com', // ]); + $this->call(OrderSeeder::class); } + } diff --git a/database/seeders/OrderSeeder.php b/database/seeders/OrderSeeder.php new file mode 100644 index 0000000..a9620b1 --- /dev/null +++ b/database/seeders/OrderSeeder.php @@ -0,0 +1,19 @@ +times(50)->create(); + } +} diff --git a/routes/api.php b/routes/api.php index 889937e..ca1be88 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,5 +1,6 @@ get('/user', function (Request $request) { return $request->user(); }); + +Route::get('orders', [OrderController::class, 'index']); +Route::get('orders/{id}', [OrderController::class, 'show']); +Route::post('orders', [OrderController::class, 'store']); +Route::put('orders/{id}', [OrderController::class, 'update']); +Route::delete('orders/{id}', [OrderController::class, 'delete']); + diff --git a/routes/web.php b/routes/web.php index d259f33..26cee5d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -16,3 +16,4 @@ use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); }); +