Skip to content

Commit

Permalink
Rename API end-points, Add Recipe resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaplet committed Mar 16, 2024
1 parent c0e3f13 commit 36925b0
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Repository info
VERSION='0.7.9' #can be '0.0.1', 'master', 'develop', 'whatever'
VERSION='0.8.0' #can be '0.0.1', 'master', 'develop', 'whatever'
TAGS_OR_HEADS='tags' # 'heads' for branches, 'tags' for tag-releases
REPOSITORY_OWNER='strategio-digital'
REPOSITORY_NAME='megio-starter'
Expand Down
25 changes: 18 additions & 7 deletions src/Collection/RecipeEntityMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ public function __construct(
{
}

/**
* @return string
*/
public function getTableName(): string
public function getRecipe(): ICollectionRecipe
{
return $this->tableName;
return $this->recipe;
}

/**
Expand All @@ -38,6 +35,20 @@ public function getReflection(): \ReflectionClass
return $this->entityRef;
}

public function getType(): CollectionPropType
{
return $this->type;
}

/**
* @return string
*/
public function getTableName(): string
{
return $this->tableName;
}


public function getQbSelect(string $qbAlias): string
{
$schema = $this->getFullSchemaReflectedByDoctrine();
Expand All @@ -47,7 +58,7 @@ public function getQbSelect(string $qbAlias): string

/**
* @return array{
* meta: array{table: string, invisible: string[]},
* meta: array{recipe: string, invisible: string[]},
* props: array{maxLength: int|null, name: string, nullable: bool, type: string}[]
* }
*/
Expand All @@ -58,7 +69,7 @@ public function getSchema(): array

return [
'meta' => [
'table' => $this->tableName,
'recipe' => $this->recipe->name(),
'invisible' => $this->recipe->invisibleColumns()
],
'props' => $this->sortColumns($props)
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/RecipeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class RecipeFinder

public function load(): self
{
$this->recipes = [];

$appFiles = Finder::findFiles()->from(Path::appDir() . '/Recipe');
foreach ($appFiles as $file) {
$class = 'App\\Recipe\\' . $file->getBasename('.php');
Expand Down
31 changes: 16 additions & 15 deletions src/Database/Manager/AuthResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace Megio\Database\Manager;

use Megio\Collection\RecipeFinder;
use Nette\Utils\Strings;
use Megio\Database\EntityFinder;
use Megio\Database\Entity\Admin;
use Megio\Database\Entity\Auth\Resource;
use Megio\Database\EntityManager;
Expand All @@ -22,7 +22,7 @@
public function __construct(
private EntityManager $em,
private RouteCollection $routes,
private EntityFinder $entityFinder,
private RecipeFinder $recipeFinder,
)
{
}
Expand Down Expand Up @@ -128,19 +128,19 @@ public function routerViewResources(array $viewResources): array
public function collectionDataResources(): array
{
$excluded = [Admin::class];
$entities = $this->entityFinder->findAll();
$entities = array_filter($entities, fn($entity) => !in_array($entity['className'], $excluded));
$recipes = $this->recipeFinder->load()->getAll();

$tables = array_map(fn($entity) => $entity['table'], $entities);
$resourceNames = array_keys($this->routes->all());
/** @var \Megio\Collection\ICollectionRecipe[] $recipes */
$recipes = array_filter($recipes, fn($recipe) => !in_array($recipe->source(), $excluded));
$recipeNames = array_map(fn($recipe) => $recipe->name(), $recipes);

$resourceNames = array_keys($this->routes->all());
$collectionRouteNames = array_filter($resourceNames, fn($name) => Strings::startsWith($name, Router::ROUTE_COLLECTION_PREFIX));

$names = [];

foreach ($tables as $tableName) {
foreach ($recipeNames as $recipeName) {
foreach ($collectionRouteNames as $routeName) {
$names[] = $routeName . '.' . $tableName;
$names[] = $routeName . '.' . $recipeName;
}
}

Expand All @@ -153,14 +153,15 @@ public function collectionDataResources(): array
public function collectionNavResources(): array
{
$excluded = [Admin::class];
$entities = $this->entityFinder->findAll();
$entities = array_filter($entities, fn($entity) => !in_array($entity['className'], $excluded));
$recipes = $this->recipeFinder->load()->getAll();

$tables = array_map(fn($entity) => $entity['table'], $entities);
$names = [];
/** @var \Megio\Collection\ICollectionRecipe[] $recipes */
$recipes = array_filter($recipes, fn($recipe) => !in_array($recipe->source(), $excluded));
$recipeNames = array_map(fn($recipe) => $recipe->name(), $recipes);

foreach ($tables as $tableName) {
$names[] = Router::ROUTE_META_NAVBAR . '.' . $tableName;
$names = [];
foreach ($recipeNames as $recipeName) {
$names[] = Router::ROUTE_META_NAVBAR . '.' . $recipeName;
}

return $names;
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/CreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ public function schema(): array
// TODO: get rows types trough reflection and validate them

return [
'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName
'recipe' => Expect::anyOf(...$names)->required(),
'rows' => Expect::array()->min(1)->max(1000)->required()
];
}

public function process(array $data): Response
{
$recipe = $this->recipeFinder->findByName($data['table']);
$recipe = $this->recipeFinder->findByName($data['recipe']);

if ($recipe === null) {
return $this->error(["Collection {$data['table']} not found"]);
return $this->error(["Collection {$data['recipe']} not found"]);
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/DeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public function schema(): array
$names = array_map(fn($r) => $r->name(), $this->recipeFinder->load()->getAll());

return [
'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName
'recipe' => Expect::anyOf(...$names)->required(),
'ids' => Expect::arrayOf('string')->min(1)->required(),
];
}

public function process(array $data): Response
{
$recipe = $this->recipeFinder->findByName($data['table']);
$recipe = $this->recipeFinder->findByName($data['recipe']);

if ($recipe === null) {
return $this->error(["Collection {$data['table']} not found"]);
return $this->error(["Collection {$data['recipe']} not found"]);
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/ShowOneRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public function schema(): array
$names = array_map(fn($r) => $r->name(), $this->recipeFinder->load()->getAll());

return [
'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName
'recipe' => Expect::anyOf(...$names)->required(),
'schema' => Expect::bool(false),
'id' => Expect::string()->required(),
];
}

public function process(array $data): Response
{
$recipe = $this->recipeFinder->findByName($data['table']);
$recipe = $this->recipeFinder->findByName($data['recipe']);

if ($recipe === null) {
return $this->error(["Collection {$data['table']} not found"]);
return $this->error(["Collection {$data['recipe']} not found"]);
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/ShowRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function schema(): array
$names = array_map(fn($r) => $r->name(), $this->recipeFinder->load()->getAll());

return [
'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName
'recipe' => Expect::anyOf(...$names)->required(),
'schema' => Expect::bool(false),
'currentPage' => Expect::int(1)->min(1)->required(),
'itemsPerPage' => Expect::int(10)->max(1000)->required(),
Expand All @@ -45,10 +45,10 @@ public function schema(): array

public function process(array $data): Response
{
$recipe = $this->recipeFinder->findByName($data['table']);
$recipe = $this->recipeFinder->findByName($data['recipe']);

if ($recipe === null) {
return $this->error(["Collection {$data['table']} not found"]);
return $this->error(["Collection {$data['recipe']} not found"]);
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Request/Collection/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function schema(): array
// TODO: get rows types trough reflection and validate them

return [
'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName
'recipe' => Expect::anyOf(...$names)->required(),
'rows' => Expect::arrayOf(
Expect::structure([
'id' => Expect::string()->required(),
Expand All @@ -50,10 +50,10 @@ public function schema(): array

public function process(array $data): Response
{
$recipe = $this->recipeFinder->findByName($data['table']);
$recipe = $this->recipeFinder->findByName($data['recipe']);

if ($recipe === null) {
return $this->error(["Collection {$data['table']} not found"]);
return $this->error(["Collection {$data['recipe']} not found"]);
}

try {
Expand Down
4 changes: 2 additions & 2 deletions src/Subscriber/AuthCollectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function onProcess(OnProcessingStartEvent $event): void
return;
}

$tableName = $event->getMetadata()->getTableName();
$resourceName = $routeName . '.' . $tableName;
$recipeName = $event->getMetadata()->getRecipe()->name();
$resourceName = $routeName . '.' . $recipeName;

if (!in_array($resourceName, $this->authUser->getResources())) {
$message = "This collection-resource '{$resourceName}' is not allowed for current user";
Expand Down

0 comments on commit 36925b0

Please sign in to comment.