File tree 6 files changed +38
-13
lines changed
Events/Interfaces/Queries
Projects/Interfaces/Http/Resources
Interfaces/Http/Controller
6 files changed +38
-13
lines changed Original file line number Diff line number Diff line change 7
7
use App \Application \Commands \CountEvents ;
8
8
use Modules \Events \Domain \EventRepositoryInterface ;
9
9
use Spiral \Cqrs \Attribute \QueryHandler ;
10
+ use Spiral \Cqrs \QueryBusInterface ;
10
11
11
12
final class CountEventsHandler extends EventsHandler
12
13
{
13
14
public function __construct (
14
15
private readonly EventRepositoryInterface $ events ,
15
- ) {}
16
+ QueryBusInterface $ bus ,
17
+ ) {
18
+ parent ::__construct ($ bus );
19
+ }
16
20
17
21
#[QueryHandler]
18
22
public function __invoke (CountEvents $ query ): int
19
23
{
20
- return $ this ->events ->countAll (self :: getScopeFromFindEvents ($ query ));
24
+ return $ this ->events ->countAll ($ this -> getScopeFromFindEvents ($ query ));
21
25
}
22
26
}
Original file line number Diff line number Diff line change 3
3
namespace Modules \Events \Interfaces \Queries ;
4
4
5
5
use App \Application \Commands \AskEvents ;
6
+ use App \Application \Commands \FindAllProjects ;
7
+ use Modules \Projects \Domain \ProjectInterface ;
8
+ use Modules \Projects \Domain \ValueObject \Key ;
9
+ use Spiral \Cqrs \QueryBusInterface ;
6
10
7
11
abstract class EventsHandler
8
12
{
9
- protected static function getScopeFromFindEvents (AskEvents $ query ): array
13
+ public function __construct (
14
+ private readonly QueryBusInterface $ bus ,
15
+ ) {}
16
+
17
+ protected function getScopeFromFindEvents (AskEvents $ query ): array
10
18
{
11
19
$ scope = [];
12
20
if ($ query ->type !== null ) {
13
21
$ scope ['type ' ] = $ query ->type ;
14
22
}
15
23
16
- $ scope ['project ' ] = $ query ->project ;
24
+ if ($ query ->project !== null ) {
25
+ $ scope ['project ' ] = $ query ->project ;
26
+ } elseif ($ query ->project !== []) {
27
+ // TODO: refactor this
28
+ $ projects = $ this ->bus ->ask (new FindAllProjects ());
29
+ $ keys = \array_map (
30
+ static fn (ProjectInterface $ project ): Key => $ project ->getKey (),
31
+ \iterator_to_array ($ projects ),
32
+ );
33
+
34
+ $ scope ['project ' ] = $ keys ;
35
+ }
17
36
18
37
return $ scope ;
19
38
}
Original file line number Diff line number Diff line change 7
7
use App \Application \Commands \FindEvents ;
8
8
use Modules \Events \Domain \EventRepositoryInterface ;
9
9
use Spiral \Cqrs \Attribute \QueryHandler ;
10
+ use Spiral \Cqrs \QueryBusInterface ;
10
11
11
12
final class FindEventsHandler extends EventsHandler
12
13
{
13
14
public function __construct (
14
15
private readonly EventRepositoryInterface $ events ,
15
- ) {}
16
+ QueryBusInterface $ bus ,
17
+ ) {
18
+ parent ::__construct ($ bus );
19
+ }
16
20
17
21
#[QueryHandler]
18
22
public function __invoke (FindEvents $ query ): iterable
19
23
{
20
24
return $ this ->events ->findAll (
21
- scope: self :: getScopeFromFindEvents ($ query ),
25
+ scope: $ this -> getScopeFromFindEvents ($ query ),
22
26
orderBy: ['timestamp ' => 'desc ' ],
23
27
limit: $ query ->limit ,
24
28
);
Original file line number Diff line number Diff line change 5
5
namespace Modules \Projects \Interfaces \Http \Resources ;
6
6
7
7
use App \Application \HTTP \Response \JsonResource ;
8
+ use Modules \Projects \Domain \Project ;
8
9
use Modules \Projects \Domain \ProjectInterface ;
9
10
use OpenApi \Attributes as OA ;
10
11
16
17
properties: [
17
18
new OA \Property (property: 'key ' , type: 'string ' ),
18
19
new OA \Property (property: 'name ' , type: 'string ' ),
20
+ new OA \Property (property: 'is_default ' , type: 'boolean ' ),
19
21
],
20
22
)]
21
23
final class ProjectResource extends JsonResource
@@ -30,6 +32,7 @@ protected function mapData(): array|\JsonSerializable
30
32
return [
31
33
'key ' => $ this ->data ->getKey (),
32
34
'name ' => $ this ->data ->getName (),
35
+ 'is_default ' => (string ) $ this ->data ->getKey () === Project::DEFAULT_KEY ,
33
36
];
34
37
}
35
38
}
Original file line number Diff line number Diff line change 4
4
5
5
namespace App \Application \Commands ;
6
6
7
- use Modules \Projects \Domain \Project ;
7
+ use Modules \Projects \Domain \ProjectInterface ;
8
8
use Spiral \Cqrs \QueryInterface ;
9
9
10
10
/**
11
- * @implements QueryInterface<Project []>
11
+ * @implements QueryInterface<ProjectInterface []>
12
12
*/
13
13
final class FindAllProjects implements QueryInterface {}
Original file line number Diff line number Diff line change 9
9
use App \Application \HTTP \Response \JsonResource ;
10
10
use App \Application \HTTP \Response \ResourceInterface ;
11
11
use App \Application \Ide \UrlTemplate ;
12
- use Modules \Projects \Domain \Project ;
13
12
use Spiral \Boot \EnvironmentInterface ;
14
13
use Spiral \Router \Annotation \Route ;
15
14
@@ -31,10 +30,6 @@ public function __invoke(
31
30
'url_template ' => $ ideUrl ->template ,
32
31
],
33
32
'version ' => $ appVersion ->version ,
34
- 'project ' => [
35
- // todo: use better option for default project
36
- 'default ' => Project::DEFAULT_KEY ,
37
- ],
38
33
]);
39
34
}
40
35
}
You can’t perform that action at this time.
0 commit comments