Skip to content

Commit

Permalink
Merge pull request #231 from buggregator/feature/default-project
Browse files Browse the repository at this point in the history
Improves information about default project
  • Loading branch information
butschster authored Jul 28, 2024
2 parents ab82861 + 7640bb1 commit ae3e0e6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
8 changes: 6 additions & 2 deletions app/modules/Events/Interfaces/Queries/CountEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
use App\Application\Commands\CountEvents;
use Modules\Events\Domain\EventRepositoryInterface;
use Spiral\Cqrs\Attribute\QueryHandler;
use Spiral\Cqrs\QueryBusInterface;

final class CountEventsHandler extends EventsHandler
{
public function __construct(
private readonly EventRepositoryInterface $events,
) {}
QueryBusInterface $bus,
) {
parent::__construct($bus);
}

#[QueryHandler]
public function __invoke(CountEvents $query): int
{
return $this->events->countAll(self::getScopeFromFindEvents($query));
return $this->events->countAll($this->getScopeFromFindEvents($query));
}
}
23 changes: 21 additions & 2 deletions app/modules/Events/Interfaces/Queries/EventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@
namespace Modules\Events\Interfaces\Queries;

use App\Application\Commands\AskEvents;
use App\Application\Commands\FindAllProjects;
use Modules\Projects\Domain\ProjectInterface;
use Modules\Projects\Domain\ValueObject\Key;
use Spiral\Cqrs\QueryBusInterface;

abstract class EventsHandler
{
protected static function getScopeFromFindEvents(AskEvents $query): array
public function __construct(
private readonly QueryBusInterface $bus,
) {}

protected function getScopeFromFindEvents(AskEvents $query): array
{
$scope = [];
if ($query->type !== null) {
$scope['type'] = $query->type;
}

$scope['project'] = $query->project;
if ($query->project !== null) {
$scope['project'] = $query->project;
} elseif ($query->project !== []) {
// TODO: refactor this
$projects = $this->bus->ask(new FindAllProjects());
$keys = \array_map(
static fn(ProjectInterface $project): Key => $project->getKey(),
\iterator_to_array($projects),
);

$scope['project'] = $keys;
}

return $scope;
}
Expand Down
8 changes: 6 additions & 2 deletions app/modules/Events/Interfaces/Queries/FindEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
use App\Application\Commands\FindEvents;
use Modules\Events\Domain\EventRepositoryInterface;
use Spiral\Cqrs\Attribute\QueryHandler;
use Spiral\Cqrs\QueryBusInterface;

final class FindEventsHandler extends EventsHandler
{
public function __construct(
private readonly EventRepositoryInterface $events,
) {}
QueryBusInterface $bus,
) {
parent::__construct($bus);
}

#[QueryHandler]
public function __invoke(FindEvents $query): iterable
{
return $this->events->findAll(
scope: self::getScopeFromFindEvents($query),
scope: $this->getScopeFromFindEvents($query),
orderBy: ['timestamp' => 'desc'],
limit: $query->limit,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Modules\Projects\Interfaces\Http\Resources;

use App\Application\HTTP\Response\JsonResource;
use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ProjectInterface;
use OpenApi\Attributes as OA;

Expand All @@ -16,6 +17,7 @@
properties: [
new OA\Property(property: 'key', type: 'string'),
new OA\Property(property: 'name', type: 'string'),
new OA\Property(property: 'is_default', type: 'boolean'),
],
)]
final class ProjectResource extends JsonResource
Expand All @@ -30,6 +32,7 @@ protected function mapData(): array|\JsonSerializable
return [
'key' => $this->data->getKey(),
'name' => $this->data->getName(),
'is_default' => (string) $this->data->getKey() === Project::DEFAULT_KEY,
];
}
}
4 changes: 2 additions & 2 deletions app/src/Application/Commands/FindAllProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Application\Commands;

use Modules\Projects\Domain\Project;
use Modules\Projects\Domain\ProjectInterface;
use Spiral\Cqrs\QueryInterface;

/**
* @implements QueryInterface<Project[]>
* @implements QueryInterface<ProjectInterface[]>
*/
final class FindAllProjects implements QueryInterface {}
5 changes: 0 additions & 5 deletions app/src/Interfaces/Http/Controller/SettingsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use App\Application\HTTP\Response\JsonResource;
use App\Application\HTTP\Response\ResourceInterface;
use App\Application\Ide\UrlTemplate;
use Modules\Projects\Domain\Project;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Router\Annotation\Route;

Expand All @@ -31,10 +30,6 @@ public function __invoke(
'url_template' => $ideUrl->template,
],
'version' => $appVersion->version,
'project' => [
// todo: use better option for default project
'default' => Project::DEFAULT_KEY,
],
]);
}
}

0 comments on commit ae3e0e6

Please sign in to comment.