Skip to content

Commit 3ab0d96

Browse files
committed
added basic sorting structure
1 parent 97da547 commit 3ab0d96

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed

src/Endpoints/Database.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44

55

66
use FiveamCode\LaravelNotionApi\Notion;
7+
use \FiveamCode\LaravelNotionApi\Entities\Database as DatabaseEntity;
8+
use FiveamCode\LaravelNotionApi\Query\Sorting;
9+
use Illuminate\Support\Collection;
710

811
class Database extends Endpoint
912
{
1013
private string $databaseId;
14+
private Collection $sortings;
1115

1216
public function __construct(string $databaseId, Notion $notion)
1317
{
1418
$this->databaseId = $databaseId;
1519
parent::__construct($notion);
1620
}
1721

18-
public function query(string $databaseId): array
22+
public function query(): array
1923
{
2024

2125
$filterJson = '
@@ -26,32 +30,51 @@ public function query(string $databaseId): array
2630
}
2731
}';
2832

33+
34+
$sortingJson = '{
35+
"property": "Ordered",
36+
"timestamp": "created_time",
37+
"direction": "descending"
38+
}';
39+
40+
2941
$filter = json_decode($filterJson);
30-
$postData = ["filter" => $filter];
42+
43+
if($this->sortings->isNotEmpty())
44+
$postData["sorts"] = Sorting::sortQuery($this->sortings);
3145

3246
$response = $this->post(
33-
$this->url(Endpoint::DATABASES . "/{$databaseId}/query"),
47+
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
3448
$postData
3549
)
3650
->json();
3751

38-
dump($response);
39-
return [];
52+
// toDo return Database Entity
53+
dd($response);
4054
}
4155

4256
public function filterBy()
4357
{
58+
59+
return $this;
4460
}
4561

46-
public function sortBy()
62+
public function sortBy(Collection $sortings)
4763
{
64+
$this->sortings = $sortings;
65+
66+
return $this;
4867
}
4968

5069
public function limit()
5170
{
71+
72+
return $this;
5273
}
5374

5475
public function offset()
5576
{
77+
78+
return $this;
5679
}
5780
}

src/Query/QueryHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use Illuminate\Http\Resources\Json\JsonResource;
66
use Illuminate\Support\Collection;
77

8-
class QueryHelper extends JsonResource
8+
class QueryHelper
99
{
1010
/**
1111
* Contains the property name the query helper works with.
1212
* @var
1313
*/
14-
protected string $property;
14+
protected ?string $property = null;
1515

1616
/**
1717
* Contains all valid timestamps to sort against.
@@ -20,6 +20,7 @@ class QueryHelper extends JsonResource
2020
* @var Collection
2121
*/
2222
protected Collection $validTimestamps;
23+
2324
/**
2425
* Contains all valid directions to sort by.
2526
*

src/Query/Sorting.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,76 @@
33
namespace FiveamCode\LaravelNotionApi\Query;
44

55
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
use Illuminate\Support\Collection;
67

78
class Sorting extends QueryHelper
89
{
910

10-
private string $timestamp;
11+
private ?string $timestamp = null;
1112
private string $direction;
1213

13-
public function __construct(string $timestamp, string $direction)
14+
public function __construct(string $direction, string $property = null, string $timestamp = null)
1415
{
1516
parent::__construct();
1617

17-
if ($this->validTimestamps->contains($timestamp))
18+
if ($timestamp !== null && !$this->validTimestamps->contains($timestamp))
1819
throw WrapperException::instance(
1920
"Invalid sorting timestamp provided.", ["invalidTimestamp" => $timestamp]
2021
);
2122

22-
if ($this->validDirections->contains($direction))
23+
24+
if (!$this->validDirections->contains($direction))
2325
throw WrapperException::instance(
2426
"Invalid sorting direction provided.", ["invalidDirection" => $direction]
2527
);
2628

29+
$this->property = $property;
2730
$this->timestamp = $timestamp;
2831
$this->direction = $direction;
2932
}
3033

34+
public static function timestampSort(string $timestampToSort, string $direction)
35+
{
36+
37+
$propertySort = new Sorting($direction, null, $timestampToSort);
38+
39+
return $propertySort;
40+
}
41+
42+
public static function propertySort(string $property, string $direction)
43+
{
44+
$propertySort = new Sorting($direction, $property);
3145

46+
return $propertySort;
47+
}
48+
49+
public function toArray(): array
50+
{
51+
if ($this->timestamp !== null) {
52+
return [
53+
"timestamp" => $this->timestamp,
54+
"direction" => $this->direction
55+
];
56+
}
57+
58+
return [
59+
"property" => $this->property,
60+
"direction" => $this->direction
61+
];
62+
}
63+
64+
public static function sortQuery(Collection $sortings): array
65+
{
66+
67+
$querySortings = new Collection();
68+
69+
$sortings->each(function (Sorting $sorting) use ($querySortings) {
70+
$querySortings->add($sorting->toArray());
71+
});
72+
73+
return $querySortings->toArray();
74+
75+
}
3276

3377

3478
}

0 commit comments

Comments
 (0)