Skip to content

Commit

Permalink
1. add user_id and guard to databasestorage table.
Browse files Browse the repository at this point in the history
2. update database test
  • Loading branch information
chenbidepro committed Jan 28, 2018
1 parent be0e6a8 commit 2ee29ac
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 15 deletions.
5 changes: 3 additions & 2 deletions migrations/2016_12_16_154403_create_shopping_cart_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function up()
Schema::create('shopping_cart', function (Blueprint $table) {
$table->string('key');
$table->string('__raw_id');
$table->string('guard')->nullable();
$table->integer('user_id')->nullable();
$table->integer('id');
$table->string('name');
$table->integer('qty');
Expand All @@ -32,8 +34,7 @@ public function up()
$table->string('type')->nullable();
$table->string('status')->nullable();
$table->text('attributes')->nullable();
$table->index('key');
$table->index('__raw_id');
$table->primary(['key', '__raw_id']);
$table->nullableTimestamps();
});
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@

require __DIR__.'/tests/CartTestTrait.php';
require __DIR__.'/tests/Product.php';
require __DIR__.'/tests/User.php';
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<file>./src/Exception.php</file>
<file>./src/Facade.php</file>
<file>./src/ServiceProvider.php</file>
<file>./src/Storage/DatabaseStorage.php</file>
<file>./src/Storage/SessionStorage.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
23 changes: 15 additions & 8 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace iBrand\Shoppingcart;

use iBrand\Shoppingcart\Storage\DatabaseStorage;
use iBrand\Shoppingcart\Storage\SessionStorage;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;

/**
Expand Down Expand Up @@ -52,6 +52,15 @@ public function register()
);

$this->app->singleton(Cart::class, function ($app) {
$storage = config('ibrand.cart.storage');

$cart = new Cart(new $storage(), $app['events']);

if (SessionStorage::class == $storage) {
return $cart;
}

//The below code is used of database storage
$currentGuard = null;
$user = null;

Expand All @@ -63,13 +72,11 @@ public function register()
}
}

$storage = config('ibrand.cart.storage');

$cart = new Cart(new $storage(), $app['events']);

if (DatabaseStorage::class == $storage && $user) {
$cart->name($currentGuard.$user->id);
$cart->saveFromSession();
if ($user) {
//The cart name like `cart.{guard}.{user_id}`: cart.api.1
$cart->name($currentGuard.'.'.$user->id);
}else{
throw new Exception('Invalid auth.');
}

return $cart;
Expand Down
8 changes: 7 additions & 1 deletion src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ public function set($key, $values)

$rawIds = $values->pluck('__raw_id')->toArray();

//Delete the data that has been removed from cart.
DB::table($this->table)->whereNotIn('__raw_id', $rawIds)->where('key', $key)->delete();

$keys = explode('.', $key);

$userId = end($keys);
$guard = prev($keys);

$values = $values->toArray();
foreach ($values as $value) {
$item = array_only($value, $this->filed);
$attr = json_encode(array_except($value, $this->filed));
$insert = array_merge($item, ['attributes' => $attr, 'key' => $key]);
$insert = array_merge($item, ['attributes' => $attr, 'key' => $key, 'guard' => $guard, 'user_id' => $userId]);
if (DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']])->first()) {
DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']])
->update(array_except($insert, ['key', '__raw_id']));
Expand Down
22 changes: 21 additions & 1 deletion tests/DatabaseCartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/

use App\Models\User;
use iBrand\ShoppingCart\Cart;
use Illuminate\Foundation\Testing\DatabaseMigrations;

Expand All @@ -32,6 +33,7 @@ protected function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);

}

protected function loadMigrationsFrom($app)
Expand All @@ -48,9 +50,27 @@ protected function setUp()
{
parent::setUp(); // TODO: Change the autogenerated stub


$user = new User([
'id' => 1,
'name' => 'ibrand'
]);

$this->be($user,'api');

$this->loadMigrationsFrom(__DIR__.'/database');

//dd($this->app['config']->get('database'));
$this->cart = $this->app['cart'];

}

/**
* test name().
*/
public function testName()
{
$this->assertEquals('cart.api.1', $this->cart->getName());
$this->cart->name('ibrand');
$this->assertEquals('cart.ibrand', $this->cart->getName());
}
}
18 changes: 18 additions & 0 deletions tests/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/1/28
* Time: 22:25
*/

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
protected $fillable = [
'id', 'name', 'email', 'password',
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ public function up()
Schema::create('shopping_cart', function (Blueprint $table) {
$table->string('key');
$table->string('__raw_id');
$table->string('guard')->nullable();
$table->integer('user_id')->nullable();
$table->integer('id');
$table->string('name');
$table->integer('qty');
$table->decimal('price');
$table->decimal('total');
$table->string('__model')->nullable();
$table->string('type');
$table->string('type')->nullable();
$table->string('status')->nullable();
$table->text('attributes')->nullable();
$table->index('key');
$table->index('__raw_id');
$table->primary(['key', '__raw_id']);
$table->nullableTimestamps();
});
}
Expand Down

0 comments on commit 2ee29ac

Please sign in to comment.