Skip to content

Database Creation #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 58 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
c3d4b48
add the ability to resolve users and parents
johguentner Feb 6, 2023
31bd869
Apply fixes from StyleCI (#115)
mechelon Feb 6, 2023
27f4fe9
Merge branch 'dev' into feature/resolve
johguentner Feb 6, 2023
9a3dbb3
fix: modify changed method within `NotionParent`
johguentner Feb 6, 2023
3c9c845
add prototypical relation resolving
johguentner Feb 16, 2023
29c991a
Apply fixes from StyleCI (#124)
mechelon Feb 16, 2023
c7a5bf2
create trait for title attribute
johguentner Apr 28, 2023
f19db04
add property type names within `Property::class`
johguentner Apr 28, 2023
8963193
add comment for clarification
johguentner Apr 28, 2023
29d56f5
add `HasTitle` traidMapping of `Entity::class`
johguentner Apr 28, 2023
8356792
implementation: start with database-building
johguentner Apr 28, 2023
0deaee7
Apply fixes from StyleCI (#134)
mechelon Apr 28, 2023
44aee37
implement further and polish database creation
johguentner Apr 29, 2023
766c7ab
Merge branch 'feature/database-creation' of https://github.com/5am-co…
johguentner Apr 29, 2023
589455a
Apply fixes from StyleCI (#136)
mechelon Apr 29, 2023
478f224
add further database attributes
johguentner Apr 29, 2023
b9f3eda
Merge branch 'feature/database-creation' of https://github.com/5am-co…
johguentner Apr 29, 2023
667928e
Apply fixes from StyleCI (#137)
mechelon Apr 29, 2023
85190f9
Merge branch 'dev' into feature/database-creation
johguentner Apr 30, 2023
2e70438
fix: in `HttpRecorder`, allow query to be empty
johguentner Apr 30, 2023
f6c870d
fix: missing default name for status properties
johguentner Apr 30, 2023
97be981
add tests for db creation in databases endpoint
johguentner Apr 30, 2023
53c15fb
polish `PestHttpRecorder::class`
johguentner Apr 30, 2023
2edd4ea
add query names to `RecordedEndpoint` tests
johguentner Apr 30, 2023
039f8f9
update existing snapshots
johguentner Apr 30, 2023
ad5d85d
Apply fixes from StyleCI (#141)
mechelon Apr 30, 2023
39d981d
add no-title-property test
johguentner Apr 30, 2023
71416dc
update current snapshots (database creation)
johguentner Apr 30, 2023
5ef1f93
Apply fixes from StyleCI (#142)
mechelon Apr 30, 2023
04f9be6
Merge branch 'dev' into feature/database-creation
johguentner Apr 30, 2023
68cb62f
Merge branch 'dev' into feature/resolve
johguentner Apr 30, 2023
fe961c8
add and polish phpdocs
johguentner May 2, 2023
fe0707c
Apply fixes from StyleCI (#144)
mechelon May 2, 2023
1e4f0ff
Merge branch 'dev' into feature/resolve
johguentner May 2, 2023
246396f
polish phpdocs of comment endpoint
johguentner May 2, 2023
d27f4ff
Apply fixes from StyleCI (#145)
mechelon May 2, 2023
9a3730b
polish: add newline to improve readability
johguentner May 2, 2023
69e15bd
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner May 2, 2023
55fe958
Apply fixes from StyleCI (#146)
mechelon May 2, 2023
2b11b70
add `parentOf` to access parents easily
johguentner May 2, 2023
a82ebeb
Apply fixes from StyleCI (#147)
mechelon May 2, 2023
eb7b39b
polish `PestHttpRecorder::class`
johguentner Apr 30, 2023
c83c9b1
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner May 3, 2023
cd8116d
Apply fixes from StyleCI (#148)
mechelon May 3, 2023
e73207a
add tests for `Notion::resolve()`; polish record
johguentner Jun 9, 2023
13a560d
build snapshots for resolve and
johguentner Jun 9, 2023
1c27736
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner Jun 9, 2023
44626bb
Apply fixes from StyleCI (#152)
mechelon Jun 9, 2023
b01fc99
add simple phpdbg code coverage (cmd)
johguentner Jun 10, 2023
0cbd9de
add additional tests for resolve endpoint
johguentner Jun 10, 2023
1f6aa11
Apply fixes from StyleCI (#153)
mechelon Jun 10, 2023
54f10a3
Merge branch 'feature/resolve' into feature/database-creation
johguentner Jun 10, 2023
a389879
add missing properties for database creation test
johguentner Jun 10, 2023
9d2e153
updated snapshots (database creation)
johguentner Jun 10, 2023
fe76f73
Apply fixes from StyleCI (#154)
mechelon Jun 10, 2023
9334b9b
minor polishes and cleanup
johguentner Jun 16, 2023
6e54138
Apply fixes from StyleCI (#158)
mechelon Jun 16, 2023
7129653
minor fix for resolve tests
johguentner Jun 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ vendor
.phpunit.result.cache
coverage/
.phpunit.cache/
.env*
.env*
coverage-report
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage-html coverage"
"test-coverage": "phpdbg -qrr ./vendor/bin/pest --coverage-html ./coverage-report"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand Down
245 changes: 245 additions & 0 deletions src/Builder/DatabaseBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<?php

namespace FiveamCode\LaravelNotionApi\Builder;

use FiveamCode\LaravelNotionApi\Endpoints\Databases;
use FiveamCode\LaravelNotionApi\Entities\Database;
use Illuminate\Support\Collection;

/**
* Class DatabaseBuilder.
*/
class DatabaseBuilder
{
/**
* @var array
*/
private array $payload;

/**
* @var Databases
*/
private Databases $databasesEndpoint;

/**
* DatabaseBuilder constructor.
*
* @param Databases $databasesEndpoint
*/
public function __construct(Databases $databasesEndpoint)
{
$this->databasesEndpoint = $databasesEndpoint;
$this->payload = [
'is_inline' => false,
'parent' => [],
'title' => [
[
'text' => [
'content' => '',
],
],
],
'properties' => [],
];
}

/**
* Creates database within given page.
*
* @param string $pageId
* @return Database
*/
public function createInPage(string $pageId): Database
{
$this->payload['parent'] = [
'type' => 'page_id',
'page_id' => $pageId,
];

if ($this->payload['properties'] === []) {
$this->addTitle();
}

return $this->databasesEndpoint->create($this->payload());
}

/**
* Sets the title for the database creation.
*
* @param string $title
* @return DatabaseBuilder
*/
public function title(string $title): DatabaseBuilder
{
$this->payload['title'] = [
[
'text' => [
'content' => $title,
],
],
];

return $this;
}

/**
* Sets the description for the database creation.
*
* @param string $description
* @return DatabaseBuilder
*/
public function description(string $description): DatabaseBuilder
{
$this->payload['description'] = [
[
'text' => [
'content' => $description,
],
],
];

return $this;
}

/**
* Sets the created database as inline (currently not supported).
*
* @todo increase Notion API Version, to make this work
*
* @return DatabaseBuilder
*/
public function inline(): DatabaseBuilder
{
$this->payload['is_inline'] = true;

return $this;
}

/**
* Sets the icon for the database creation.
*
* @param string $icon
* @return DatabaseBuilder
*/
public function iconEmoji(string $icon): DatabaseBuilder
{
$this->payload['icon'] = [
'type' => 'emoji',
'emoji' => $icon,
];

return $this;
}

/**
* Sets the icon for the database creation.
*
* @param string $url
* @return DatabaseBuilder
*/
public function iconExternal(string $url): DatabaseBuilder
{
$this->payload['icon'] = [
'type' => 'external',
'external' => [
'url' => $url,
],
];

return $this;
}

/**
* Sets the cover for the database creation.
*
* @param string $url
* @return DatabaseBuilder
*/
public function coverExternal(string $url): DatabaseBuilder
{
$this->payload['cover'] = [
'type' => 'external',
'external' => [
'url' => $url,
],
];

return $this;
}

/**
* Adds the property `title` database creation.
*
* @param string $name
* @return DatabaseBuilder
*/
public function addTitle(string $name = 'Name')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing return type? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's right there, isn't it ?

{
$this->add(PropertyBuilder::title($name));

return $this;
}

/**
* Adds one or multiple properties to the database creation.
*
* @param PropertyBuilder|Collection|DatabaseSchemeBuilder $properties
* @return DatabaseBuilder
*/
public function add(PropertyBuilder|Collection|DatabaseSchemeBuilder $properties): DatabaseBuilder
{
if ($properties instanceof PropertyBuilder) {
$properties = collect([$properties]);
}

if ($properties instanceof DatabaseSchemeBuilder) {
$properties = $properties->getProperties();
}

$properties->each(function (PropertyBuilder $property) {
$this->payload['properties'][$property->getName()] = $property->payload();
});

return $this;
}

/**
* Adds multiple properties to the database creation, similar to a Laravel migration.
*
* @param callable $callback
* @return DatabaseBuilder
*/
public function scheme(callable $callback): DatabaseBuilder
{
$builder = new DatabaseSchemeBuilder();
$callback($builder);

return $this->add($builder);
}

/**
* Adds a raw property to the database creation.
*
* @param string $title
* @param string $propertyType
* @param array|null $content
* @return DatabaseBuilder
*/
public function addRaw(string $title, string $propertyType, ?array $content = null): DatabaseBuilder
{
$this->payload['properties'][$title] = [];
$this->payload['properties'][$title][$propertyType] = $content ?? new \stdClass();

return $this;
}

/**
* Returns the payload for the database creation.
*
* @return array
*/
public function payload(): array
{
return $this->payload;
}
}
Loading