repository pattern
This commit is contained in:
70
app/Http/Controllers/OrderController.php
Normal file
70
app/Http/Controllers/OrderController.php
Normal 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);
|
||||
}
|
||||
}
|
28
app/Http/Requests/StoreOrderRequest.php
Normal file
28
app/Http/Requests/StoreOrderRequest.php
Normal 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 [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
28
app/Http/Requests/UpdateOrderRequest.php
Normal file
28
app/Http/Requests/UpdateOrderRequest.php
Normal 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 [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
13
app/Interfaces/OrderRepositoryInterface.php
Normal file
13
app/Interfaces/OrderRepositoryInterface.php
Normal 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
11
app/Models/Order.php
Normal 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;
|
||||
}
|
66
app/Policies/OrderPolicy.php
Normal file
66
app/Policies/OrderPolicy.php
Normal 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
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
27
app/Providers/RepositoryServiceProvider.php
Normal file
27
app/Providers/RepositoryServiceProvider.php
Normal 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
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
39
app/Repositories/OrderRepository.php
Normal file
39
app/Repositories/OrderRepository.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user