repository pattern

This commit is contained in:
Sameer 2024-02-13 13:11:11 +05:45
parent c391c71bc7
commit 574bf336df
15 changed files with 368 additions and 0 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace App\Http\Controllers;
use App\Interfaces\OrderRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class OrderController extends Controller
{
private OrderRepositoryInterface $orderRepository;
public function __construct(OrderRepositoryInterface $orderRepository)
{
$this->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);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace App\Interfaces;
interface OrderRepositoryInterface
{
public function getAllOrders();
public function getOrderById($orderId);
public function deleteOrder($orderId);
public function createOrder(array $orderDetails);
public function updateOrder($orderId, array $newDetails);
public function getFulfilledOrders();
}

11
app/Models/Order.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
}

View File

@ -0,0 +1,66 @@
<?php
namespace App\Policies;
use App\Models\Order;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class OrderPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Order $order): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Order $order): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Order $order): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Order $order): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Order $order): bool
{
//
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Providers;
use App\Interfaces\OrderRepositoryInterface;
use App\Repositories\OrderRepository;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register()
{
$this->app->bind(OrderRepositoryInterface::class, OrderRepository::class);
}
/**
* Bootstrap services.
*/
public function boot(): void
{
//
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Repositories;
use App\Interfaces\OrderRepositoryInterface;
use App\Models\Order;
class OrderRepository implements OrderRepositoryInterface
{
public function getAllOrders()
{
return Order::all();
}
public function getOrderById($orderId)
{
return Order::findOrFail($orderId);
}
public function deleteOrder($orderId)
{
Order::destroy($orderId);
}
public function createOrder(array $orderDetails)
{
return Order::create($orderDetails);
}
public function updateOrder($orderId, array $newDetails)
{
return Order::whereId($orderId)->update($newDetails);
}
public function getFulfilledOrders()
{
return Order::where('is_fulfilled', true);
}
}

View File

@ -159,6 +159,7 @@ return [
/*
* Package Service Providers...
*/
App\Providers\RepositoryServiceProvider::class,
/*
* Application Service Providers...

View File

@ -0,0 +1,25 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Order>
*/
class OrderFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'details' => $this->faker->sentences(4, true),
'client' => $this->faker->name(),
'is_fulfilled' => $this->faker->boolean(),
];
}
}

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('orders', function (Blueprint $table) {
$table->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');
}
};

View File

@ -18,5 +18,7 @@ class DatabaseSeeder extends Seeder
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call(OrderSeeder::class);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Database\Seeders;
use App\Models\Order;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class OrderSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
Order::factory()->times(50)->create();
}
}

View File

@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\OrderController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
@ -17,3 +18,10 @@ use Illuminate\Support\Facades\Route;
Route::middleware('auth:sanctum')->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']);

View File

@ -16,3 +16,4 @@ use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});