Permalink
Browse files

Add Resource Controller/Routes for Products,Orders

- Add resource routes for all operations except showing create/edit
  form.
- Add Controller for Orders and Products for API endpoints to view
  collections of each (with pagination), update and delete.
- Utilize custom request and response objects to keep slim controllers.
  • Loading branch information...
MitchellMcKenna committed Aug 18, 2017
1 parent eb96a68 commit 7de09d828429b95311bfd8bba15d86fe7aed870e
Showing with 165 additions and 5 deletions.
  1. +86 −0 app/Http/Controllers/OrderController.php
  2. +77 −0 app/Http/Controllers/ProductController.php
  3. +2 −5 routes/api.php
@@ -0,0 +1,86 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\OrderCreateRequest;
use App\Http\Requests\OrderUpdateRequest;
use App\Http\Responses\OrderCollectionResponse;
use App\Http\Responses\OrderResponse;
use App\Order;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
class OrderController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
* @return Response
*/
public function index(Request $request)
{
/** @var LengthAwarePaginator $paginator */
$paginator = (Order::paginate($request->input('limit')))->appends($request->query());
$orders = $paginator->getCollection();
return new OrderCollectionResponse($orders, $paginator);
}
/**
* Store a newly created resource in storage.
*
* @param OrderCreateRequest $request
* @return \Illuminate\Http\Response
*/
public function store(OrderCreateRequest $request)
{
$order = Order::create([
'order_id' => $request->getOrderId(),
'quantity' => $request->getQuantity(),
'product_id' => $request->getProductId()
]);
return new OrderResponse($order, Response::HTTP_CREATED);
}
/**
* Display the specified resource.
*
* @param \App\Order $order
* @return \Illuminate\Http\Response
*/
public function show(Order $order)
{
return new OrderResponse($order);
}
/**
* Update the specified resource in storage.
*
* @param OrderUpdateRequest $request
* @param \App\Order $order
* @return Response
*/
public function update(OrderUpdateRequest $request, Order $order)
{
$order->update(array_filter([
'order_id' => $request->getOrderId(),
'quantity' => $request->getQuantity(),
'product_id' => $request->getProductId()
]));
return new OrderResponse($order);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Order $order
* @return \Illuminate\Http\Response
*/
public function destroy(Order $order)
{
$order->delete();
return new Response('', Response::HTTP_NO_CONTENT);
}
}
@@ -0,0 +1,77 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProductCreateRequest;
use App\Http\Requests\ProductUpdateRequest;
use App\Http\Responses\ProductCollectionResponse;
use App\Http\Responses\ProductResponse;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
* @return Response
*/
public function index(Request $request)
{
/** @var LengthAwarePaginator $paginator */
$paginator = (Product::paginate($request->input('limit')))->appends($request->query());
$products = $paginator->getCollection();
return new ProductCollectionResponse($products, $paginator);
}
/**
* Store a newly created resource in storage.
*
* @param ProductCreateRequest $request
* @return \Illuminate\Http\Response
*/
public function store(ProductCreateRequest $request)
{
return new ProductResponse(Product::create(['name' => $request->getName()]), Response::HTTP_CREATED);
}
/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return new ProductResponse($product);
}
/**
* Update the specified resource in storage.
*
* @param ProductUpdateRequest|Request $request
* @param \App\Product $product
* @return Response
*/
public function update(ProductUpdateRequest $request, Product $product)
{
$product->name = $request->getName();
$product->save();
return new ProductResponse($product);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return new Response('', Response::HTTP_NO_CONTENT);
}
}
View
@@ -1,7 +1,5 @@
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
@@ -13,6 +11,5 @@
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::resource('products', 'ProductController', ['except' => ['create', 'edit']]);
Route::resource('orders', 'OrderController', ['except' => ['create', 'edit']]);

0 comments on commit 7de09d8

Please sign in to comment.