Skip to content

Commit

Permalink
Some more work toward a mongodb-based cart
Browse files Browse the repository at this point in the history
  • Loading branch information
masom committed Mar 25, 2011
1 parent fcaa534 commit 3711717
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 29 deletions.
1 change: 1 addition & 0 deletions controllers/CheckoutsController.php
Expand Up @@ -42,6 +42,7 @@ public function cancel(){
return $this->redirect(array('Offers::index'));
}
public function confirm(){

if(Cart::isEmpty()){
FlashMessage::set("Your cart is currently empty.");
return $this->redirect("Offers::index");
Expand Down
20 changes: 10 additions & 10 deletions controllers/OffersController.php
Expand Up @@ -14,9 +14,7 @@ class OffersController extends \chowly\extensions\action\Controller{

public function index(){
$offers = Offers::current();



$venues = array();
$venues_id = array();
foreach($offers as $offer){
Expand All @@ -26,7 +24,7 @@ public function index(){
if(!empty($venues_id)){
$conditions = array('_id' => array_keys($venues_id));
Venues::meta('title', 'logo');
$venues = Venue::find('list', compact('conditions','fields'));
$venues = Venues::find('list', compact('conditions','fields'));
}
return compact('offers', 'venues');
}
Expand Down Expand Up @@ -54,26 +52,28 @@ public function view(){
public function buy(){
if(!$this->request->id){
FlashMessage::set("Missing data.");
return $this->redirect(array("Offers::index"));
return $this->redirect( array("Offers::index") );
}
if(Cart::contain($this->request->id)){
return $this->redirect(array('Checkouts::confirm'));

if($this->Cart->containItem($this->request->id)){
return $this->redirect( array('Checkouts::confirm') );
}
$readonly = Cart::isReadOnly();
if($readonly){

if($this->Cart->isReadOnly()){
FlashMessage::set("There is currently a transaction in progress on your cart.");
return $this->redirect(array('Checkouts::confirm'));
}

try{
$reserved = Offers::reserve($this->request->id, Cart::id());
$reserved = Offers::reserve($this->request->id, $this->Cart->_id);
}catch(InventoryException $e){
FlashMessage::set("Sorry, The item could not be added to your cart");
return $this->redirect($this->request->referer());
}catch(OfferException $e){
FlashMessage::set("Sorry, The item could not be added to your cart for the following reason: {$e->getMessage()}");
return $this->redirect($this->request->referer());
}
Cart::add($this->request->id, $reserved);
$this->Cart->addItem($this->request->id, $reserved);
return $this->redirect(array('Checkouts::confirm'));
}

Expand Down
10 changes: 9 additions & 1 deletion extensions/action/Controller.php
Expand Up @@ -4,8 +4,12 @@
use \lithium\net\http\Router;
use \lithium\template\View;

use chowly\models\Carts;

class Controller extends \lithium\action\Controller{

protected $Cart;

protected function _init(){
parent::_init();
if($this->request->is('ssl') && !in_array($this->request->controller, array('checkouts','users'))){
Expand All @@ -16,7 +20,11 @@ protected function _init(){
)
);
}

$this->Cart = Carts::first(array('_id' => session_id()));
if(!$this->Cart){
$this->Cart = Carts::create();
$this->Cart->_id = new \MongoId();
}
}
protected function _getEmail(array $data, $template = null, $controller = null){

Expand Down
24 changes: 20 additions & 4 deletions models/Cart.php → models/Carts.php
Expand Up @@ -3,6 +3,10 @@

class Carts extends \lithium\data\Model{

protected $_schema = array(
'_id' => array('type'=>'id'),
'items' => array('type'=>'id', 'array' => true)
);
public function endTransaction(){
$command = $entity->_transaction('transaction', 'default');
$result = static::connection()->connection->command($command);
Expand All @@ -14,6 +18,13 @@ public function startTransaction($entity){
return !isset($result['errmsg']);
}

/**
* Wraps findAndModify mongodb command for transaction status handling.
* @param Model $entity
* @param var $from
* @param var $to
* @return var The mongodb command structure.
*/
private function _transaction($entity, $from = 'default', $to = 'transaction'){
debug($entity);die;
return array(
Expand All @@ -32,10 +43,9 @@ private function _transaction($entity, $from = 'default', $to = 'transaction'){

/**
* Add a cart item
* @param var $offer_id
* @return bool
*/
public function add($entity, $offer_id, $inventory_id){
public function addItem($entity, $offer_id, $inventory_id){
$conditions = array('_id' => $entity->_id, 'state' => 'default');
$data = array('$addToSet' => array('items' => $offer_id));
$options = array('multiple' => false, 'safe' => true);
Expand All @@ -46,17 +56,23 @@ public static function isEmpty($entity){
return empty($entity->items);
}

public function clear($entity){
public function clearItems($entity){
$conditions = array('_id' => $entity->_id, 'state' => 'default');
$data = array('$set' => array('items' => array()));
$options = array('multiple' => false, 'safe' => true);
return $entity->update($data, $conditions, $options);
}
public function remove($entity, $offer_id){
public function removeItem($entity, $offer_id){
$conditions = array('_id' => $entity->_id, 'state' => 'default');
$data = array('$pull' => array('items' => $offer_id));
$options = array('safe'=>true);
return $entity->update($data, $conditions, $options);
}
public function containItem($entity, $offer_id){
return $entity->items->first(function($i) use ($offer_id) { return (string) $i->_id == $offer_id; });
}
public function isReadOnly($entity){
return ($entity->state == "default");
}
}
?>
15 changes: 5 additions & 10 deletions models/Inventories.php
Expand Up @@ -30,9 +30,8 @@ public static function releaseExpired(){
return static::update( $data , $conditions);
}
public static function release($customer_id, $offer_id){
$self = static::_object();
$command = array(
'findAndModify' => $self->_meta['source'],
'findAndModify' => static::meta('source'),
'query' => array(
'offer_id' => new \MongoId($offer_id),
'state' => 'reserved'
Expand Down Expand Up @@ -64,10 +63,9 @@ public static function release($customer_id, $offer_id){
* @param var $offer_id
* @todo Add indexes to inventory
*/
public static function reserve($customer_id, $offer_id){
$self = static::_object();
public static function reserve($offer_id, $customer_id){
$command = array(
'findAndModify' => $self->_meta['source'],
'findAndModify' => static::meta('source'),
'query' => array(
'offer_id' => new \MongoId($offer_id),
'state' => 'available'
Expand All @@ -80,7 +78,6 @@ public static function reserve($customer_id, $offer_id){
)
)
);

$result = static::connection()->connection->command($command);

if(isset($result['errmsg'])){
Expand All @@ -93,9 +90,8 @@ public static function reserve($customer_id, $offer_id){
return $inventory;
}
public static function secure($inventory_id){
$self = static::_object();
$command = array(
'findAndModify' => $self->_meta['source'],
'findAndModify' => static::meta('source'),
'query' => array(
'_id' => $inventory_id,
),
Expand All @@ -113,9 +109,8 @@ public static function secure($inventory_id){
return true;
}
public static function purchase($purchase_id, $inventory_id){
$self = static::_object();
$command = array(
'findAndModify' => $self->_meta['source'],
'findAndModify' => static::meta('source'),
'query' => array(
'_id' => new \MongoId($inventory_id),
),
Expand Down
5 changes: 3 additions & 2 deletions models/Offers.php
Expand Up @@ -100,7 +100,7 @@ public static function releaseInventory($offer_id){
* @param var $customer_id
* @param var $offer_id
*/
public static function reserve($offer_id,$customer_id){
public static function reserve($offer_id, $cart_id){
$date = new \MongoDate();
$conditions = array(
'starts' => array('$lt' => $date),
Expand All @@ -114,8 +114,9 @@ public static function reserve($offer_id,$customer_id){
throw new OfferException("Offer not found.");
}


try{
$inventory = Inventories::reserve($customer_id,$offer_id);
$inventory = Inventories::reserve($offer_id, $cart_id);
$offer->availability--;
if($offer->availability <= 0){
$offer->availability = 0;
Expand Down
4 changes: 2 additions & 2 deletions views/offers/admin_edit.html.php
@@ -1,6 +1,6 @@
<?php
$starts = $offer->starts->sec ?: time();
$ends = $offer->starts->sec ?: time() + 60 * 60 * 24 * 30;
$starts = ($offer->_id)? $offer->starts->sec : time();
$ends = ($offer->_id)? $offer->ends->sec : time() + 60 * 60 * 24 * 30;
?>
<div id="content-header">
<?php if($offer->_id):?>
Expand Down

0 comments on commit 3711717

Please sign in to comment.