Skip to content

Commit 5f3367f

Browse files
josip-miloticjmiloticNorgul
authored
Soft delete search #minor (asseco-voice#28)
* Implemented searching by soft deleted models * Readme update * Apply fixes from StyleCI Co-authored-by: jmilotic <josip.milotic@asseco-see.hr> Co-authored-by: Marko Dupor <Norgul@users.noreply.github.com>
1 parent e5a1a77 commit 5f3367f

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ JSON parameters (keys):
5757
- `offset` - will return a subset of results starting from a point given. This parameter **MUST**
5858
be used together with ``limit`` parameter.
5959
- `count` - will return record count.
60+
- `soft_deleted` - will include soft deleted models in search results
6061

6162
### Search
6263

@@ -329,6 +330,17 @@ You can fetch count of records instead of concrete records by adding the count k
329330

330331
This will do a ``SELECT count(*) FROM table``.
331332

333+
### Soft deleted
334+
335+
By default, soft deleted records are excluded from the search. This can be overridden
336+
with ``soft_deleted``:
337+
338+
```
339+
{
340+
"soft_deleted": true
341+
}
342+
```
343+
332344
## Top level logical operators
333345

334346
Additionally, it is possible to group search clauses by top-level logical operator.

config/asseco-json-query-builder.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Asseco\JsonQueryBuilder\RequestParameters\RelationsParameter;
99
use Asseco\JsonQueryBuilder\RequestParameters\ReturnsParameter;
1010
use Asseco\JsonQueryBuilder\RequestParameters\SearchParameter;
11+
use Asseco\JsonQueryBuilder\RequestParameters\SoftDeletedParameter;
1112
use Asseco\JsonQueryBuilder\SearchCallbacks\Between;
1213
use Asseco\JsonQueryBuilder\SearchCallbacks\Equals;
1314
use Asseco\JsonQueryBuilder\SearchCallbacks\GreaterThan;
@@ -32,6 +33,7 @@
3233
OffsetParameter::class,
3334
CountParameter::class,
3435
GroupByParameter::class,
36+
SoftDeletedParameter::class,
3537
],
3638

3739
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Asseco\JsonQueryBuilder\RequestParameters;
6+
7+
use Asseco\JsonQueryBuilder\Exceptions\JsonQueryBuilderException;
8+
use Illuminate\Database\Eloquent\SoftDeletingScope;
9+
10+
class SoftDeletedParameter extends AbstractParameter
11+
{
12+
public static function getParameterName(): string
13+
{
14+
return 'soft_deleted';
15+
}
16+
17+
protected function areArgumentsValid(): void
18+
{
19+
if (count($this->arguments) != 1) {
20+
throw new JsonQueryBuilderException("Parameter '{$this->getParameterName()}' expects only one argument.");
21+
}
22+
23+
if (!in_array($this->arguments[0], [1, '1', true, 'true'], true)) {
24+
throw new JsonQueryBuilderException("Parameter '{$this->getParameterName()}' expects to be 'true' if it is to be used.");
25+
}
26+
}
27+
28+
protected function appendQuery(): void
29+
{
30+
$this->builder->withoutGlobalScope(SoftDeletingScope::class);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Asseco\JsonQueryBuilder\Tests\Unit\RequestParameters;
6+
7+
use Asseco\JsonQueryBuilder\Config\ModelConfig;
8+
use Asseco\JsonQueryBuilder\RequestParameters\SoftDeletedParameter;
9+
use Asseco\JsonQueryBuilder\Tests\TestCase;
10+
use Exception;
11+
use Illuminate\Database\Eloquent\Builder;
12+
use Mockery;
13+
14+
class SoftDeletedParameterTest extends TestCase
15+
{
16+
protected Builder $builder;
17+
protected ModelConfig $modelConfig;
18+
19+
public function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->builder = app(Builder::class);
24+
25+
$this->modelConfig = Mockery::mock(ModelConfig::class);
26+
}
27+
28+
/** @test */
29+
public function has_a_name()
30+
{
31+
$countParameter = new SoftDeletedParameter([], $this->builder, $this->modelConfig);
32+
33+
$this->assertEquals('soft_deleted', $countParameter::getParameterName());
34+
}
35+
36+
/** @test */
37+
public function accepts_valid_arguments()
38+
{
39+
foreach ([1, '1', true, 'true'] as $validArgument) {
40+
$countParameter = new SoftDeletedParameter([$validArgument], $this->builder, $this->modelConfig);
41+
$countParameter->run();
42+
}
43+
44+
$this->assertTrue(true);
45+
}
46+
47+
/** @test */
48+
public function rejects_non_bool_argument()
49+
{
50+
$this->expectException(Exception::class);
51+
52+
$countParameter = new SoftDeletedParameter(['invalid'], $this->builder, $this->modelConfig);
53+
$countParameter->run();
54+
}
55+
56+
/** @test */
57+
public function rejects_multiple_arguments()
58+
{
59+
$this->expectException(Exception::class);
60+
61+
$countParameter = new SoftDeletedParameter([1, 1], $this->builder, $this->modelConfig);
62+
$countParameter->run();
63+
}
64+
}

0 commit comments

Comments
 (0)