Skip to content

Commit

Permalink
Updated the cart controllers to use the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mattias-persson committed Aug 22, 2019
1 parent ddda4f9 commit 162b93d
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 430 deletions.
32 changes: 20 additions & 12 deletions src/Controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@

namespace Happypixels\Shopr\Controllers;

use Happypixels\Shopr\Cart\Cart;
use Illuminate\Routing\Controller;
use Happypixels\Shopr\Facades\Cart;

class CartController extends Controller
{
protected $cart;

public function __construct(Cart $cart)
{
$this->cart = $cart;
}

/**
* Returns the full cart summary.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
return $this->cart->summary();
return Cart::get();
}

/**
* Returns the count of the cart.
*
* @return \Illuminate\Http\JsonResponse
*/
public function count()
{
return ['count' => $this->cart->count()];
return ['count' => Cart::count()];
}

/**
* Clears the cart and returns the full cart summary.
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroy()
{
$this->cart->clear();
Cart::clear();

return $this->cart->summary();
return Cart::get();
}
}
60 changes: 39 additions & 21 deletions src/Controllers/CartItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,70 @@
namespace Happypixels\Shopr\Controllers;

use Illuminate\Http\Request;
use Happypixels\Shopr\Cart\Cart;
use Illuminate\Routing\Controller;
use Happypixels\Shopr\Facades\Cart;
use Happypixels\Shopr\Rules\Discounts\NotADiscount;
use Illuminate\Foundation\Validation\ValidatesRequests;

class CartItemController extends Controller
{
use ValidatesRequests;

protected $cart;

public function __construct(Cart $cart)
{
$this->cart = $cart;
}

/**
* Adds an item to the cart. Returns the full cart summary.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$this->validate($request, [
'shoppable_type' => ['required', new NotADiscount],
'shoppable_id' => 'required',
]);

$item = $this->cart->addItem(
$request->shoppable_type,
$request->shoppable_id,
$request->get('quantity', 1),
$request->get('options', []),
$request->get('sub_items', []),
$request->get('price', null)
);
$shoppable = $request->shoppable_type::findOrFail($request->shoppable_id);

$subItems = collect($request->get('sub_items', []))->map(function ($subItem) {
$subItem['shoppable'] = $subItem['shoppable_type']::findOrFail($subItem['shoppable_id']);

return $subItem;
})->toArray();

Cart::add($shoppable, [
'quantity' => $request->get('quantity', 1),
'options' => $request->get('options', null),
'sub_items' => $subItems,
'price' => $request->get('price', null),
]);

return $this->cart->summary();
return Cart::get();
}

/**
* Updates a cart item. Returns the full cart summary.
*
* @param Request $request
* @param string $id
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, $id)
{
$this->cart->updateItem($id, $request->all());
Cart::update($id, $request->all());

return $this->cart->summary();
return Cart::get();
}

/**
* Removes an item from the cart and returns the full cart summary.
*
* @param string $id
* @return \Illuminate\Http\JsonResponse
*/
public function destroy($id)
{
$this->cart->removeItem($id);
Cart::delete($id);

return $this->cart->summary();
return Cart::get();
}
}
146 changes: 0 additions & 146 deletions tests/Feature/Cart/AddCartItemTest.php

This file was deleted.

51 changes: 0 additions & 51 deletions tests/Feature/Cart/CartControllerTest.php

This file was deleted.

Loading

0 comments on commit 162b93d

Please sign in to comment.