From 944b2d83fe6a40a143fd3aac2210f8b5f4703bad Mon Sep 17 00:00:00 2001 From: Alex Rabinovich <4alexr@gmail.com> Date: Sun, 28 Oct 2018 12:00:20 +0200 Subject: [PATCH 1/4] Update 2013_11_26_161501_create_currency_table.php allow soft deletes --- database/migrations/2013_11_26_161501_create_currency_table.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/2013_11_26_161501_create_currency_table.php b/database/migrations/2013_11_26_161501_create_currency_table.php index 7161565..3081bb1 100644 --- a/database/migrations/2013_11_26_161501_create_currency_table.php +++ b/database/migrations/2013_11_26_161501_create_currency_table.php @@ -35,6 +35,7 @@ public function up() $table->string('exchange_rate'); $table->boolean('active')->default(false); $table->timestamps(); + $table->softDeletes(); }); } From 4b018c9f4e9e5cbcb244c7a5a9456b8855f13030 Mon Sep 17 00:00:00 2001 From: Alex Rabinovich <4alexr@gmail.com> Date: Sun, 28 Oct 2018 12:24:12 +0200 Subject: [PATCH 2/4] Update Database.php optional find currencies that are not soft deleted (if column exists) --- src/Drivers/Database.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Database.php b/src/Drivers/Database.php index e326e60..b8dd793 100644 --- a/src/Drivers/Database.php +++ b/src/Drivers/Database.php @@ -5,6 +5,7 @@ use DateTime; use Illuminate\Support\Collection; use Illuminate\Database\DatabaseManager; +use Illuminate\Support\Facades\Schema; class Database extends AbstractDriver { @@ -48,9 +49,15 @@ public function create(array $params) 'exchange_rate' => 1, 'active' => 0, 'created_at' => $created, - 'updated_at' => $created, + 'updated_at' => $created ], $params); + if (Schema::hasColumn($this->config('table'), 'deleted_at')) { + $params = array_merge([ + 'deleted_at' => null + ], $params); + } + return $this->database->table($this->config('table'))->insert($params); } @@ -59,7 +66,13 @@ public function create(array $params) */ public function all() { - $collection = new Collection($this->database->table($this->config('table'))->get()); + $table = $this->database->table($this->config('table')); + + if (Schema::hasColumn($this->config('table'), 'deleted_at')) { + $collection = new Collection($table->whereNull('deleted_at')->get()); + } else { + $collection = new Collection($this->database->table($this->config('table'))->get()); + } return $collection->keyBy('code') ->map(function ($item) { @@ -72,7 +85,7 @@ public function all() 'exchange_rate' => $item->exchange_rate, 'active' => $item->active, 'created_at' => $item->updated_at, - 'updated_at' => $item->updated_at, + 'updated_at' => $item->updated_at ]; }) ->all(); @@ -86,6 +99,10 @@ public function find($code, $active = 1) $query = $this->database->table($this->config('table')) ->where('code', strtoupper($code)); + if (Schema::hasColumn($this->config('table'), 'deleted_at')) { + $query->whereNull('deleted_at'); + } + // Make active optional if (is_null($active) === false) { $query->where('active', $active); From ea7df3c2db8f0a8154e6bf58b1978cd8b2c59b23 Mon Sep 17 00:00:00 2001 From: Alex Rabinovich <4alexr@gmail.com> Date: Sun, 28 Oct 2018 13:14:26 +0200 Subject: [PATCH 3/4] Update Database.php --- src/Drivers/Database.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Drivers/Database.php b/src/Drivers/Database.php index b8dd793..1c6f702 100644 --- a/src/Drivers/Database.php +++ b/src/Drivers/Database.php @@ -34,7 +34,7 @@ public function __construct(array $config) public function create(array $params) { // Ensure the currency doesn't already exist - if ($this->find($params['code'], null) !== null) { + if ($this->find($params['code'], null, true) !== null) { return 'exists'; } @@ -94,12 +94,12 @@ public function all() /** * {@inheritdoc} */ - public function find($code, $active = 1) + public function find($code, $active = 1, $strict = false) { $query = $this->database->table($this->config('table')) ->where('code', strtoupper($code)); - if (Schema::hasColumn($this->config('table'), 'deleted_at')) { + if (!$strict && Schema::hasColumn($this->config('table'), 'deleted_at')) { $query->whereNull('deleted_at'); } From ae07986243849e597e4ab673fa8c2072fe33fb69 Mon Sep 17 00:00:00 2001 From: Alex Rabinovich <4alexr@gmail.com> Date: Mon, 29 Oct 2018 10:31:04 +0200 Subject: [PATCH 4/4] Update Database.php --- src/Drivers/Database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Drivers/Database.php b/src/Drivers/Database.php index 1c6f702..07a43b6 100644 --- a/src/Drivers/Database.php +++ b/src/Drivers/Database.php @@ -94,12 +94,12 @@ public function all() /** * {@inheritdoc} */ - public function find($code, $active = 1, $strict = false) + public function find($code, $active = 1, $ignoreDeleted = false) { $query = $this->database->table($this->config('table')) ->where('code', strtoupper($code)); - if (!$strict && Schema::hasColumn($this->config('table'), 'deleted_at')) { + if (!$ignoreDeleted && Schema::hasColumn($this->config('table'), 'deleted_at')) { $query->whereNull('deleted_at'); }