File tree Expand file tree Collapse file tree
Events/Interfaces/Queries
Projects/Interfaces/Http/Resources
Interfaces/Http/Controller Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77use App \Application \Commands \CountEvents ;
88use Modules \Events \Domain \EventRepositoryInterface ;
99use Spiral \Cqrs \Attribute \QueryHandler ;
10+ use Spiral \Cqrs \QueryBusInterface ;
1011
1112final class CountEventsHandler extends EventsHandler
1213{
1314 public function __construct (
1415 private readonly EventRepositoryInterface $ events ,
15- ) {}
16+ QueryBusInterface $ bus ,
17+ ) {
18+ parent ::__construct ($ bus );
19+ }
1620
1721 #[QueryHandler]
1822 public function __invoke (CountEvents $ query ): int
1923 {
20- return $ this ->events ->countAll (self :: getScopeFromFindEvents ($ query ));
24+ return $ this ->events ->countAll ($ this -> getScopeFromFindEvents ($ query ));
2125 }
2226}
Original file line number Diff line number Diff line change 33namespace Modules \Events \Interfaces \Queries ;
44
55use 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 ;
610
711abstract class EventsHandler
812{
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
1018 {
1119 $ scope = [];
1220 if ($ query ->type !== null ) {
1321 $ scope ['type ' ] = $ query ->type ;
1422 }
1523
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+ }
1736
1837 return $ scope ;
1938 }
Original file line number Diff line number Diff line change 77use App \Application \Commands \FindEvents ;
88use Modules \Events \Domain \EventRepositoryInterface ;
99use Spiral \Cqrs \Attribute \QueryHandler ;
10+ use Spiral \Cqrs \QueryBusInterface ;
1011
1112final class FindEventsHandler extends EventsHandler
1213{
1314 public function __construct (
1415 private readonly EventRepositoryInterface $ events ,
15- ) {}
16+ QueryBusInterface $ bus ,
17+ ) {
18+ parent ::__construct ($ bus );
19+ }
1620
1721 #[QueryHandler]
1822 public function __invoke (FindEvents $ query ): iterable
1923 {
2024 return $ this ->events ->findAll (
21- scope: self :: getScopeFromFindEvents ($ query ),
25+ scope: $ this -> getScopeFromFindEvents ($ query ),
2226 orderBy: ['timestamp ' => 'desc ' ],
2327 limit: $ query ->limit ,
2428 );
Original file line number Diff line number Diff line change 55namespace Modules \Projects \Interfaces \Http \Resources ;
66
77use App \Application \HTTP \Response \JsonResource ;
8+ use Modules \Projects \Domain \Project ;
89use Modules \Projects \Domain \ProjectInterface ;
910use OpenApi \Attributes as OA ;
1011
1617 properties: [
1718 new OA \Property (property: 'key ' , type: 'string ' ),
1819 new OA \Property (property: 'name ' , type: 'string ' ),
20+ new OA \Property (property: 'is_default ' , type: 'boolean ' ),
1921 ],
2022)]
2123final class ProjectResource extends JsonResource
@@ -30,6 +32,7 @@ protected function mapData(): array|\JsonSerializable
3032 return [
3133 'key ' => $ this ->data ->getKey (),
3234 'name ' => $ this ->data ->getName (),
35+ 'is_default ' => (string ) $ this ->data ->getKey () === Project::DEFAULT_KEY ,
3336 ];
3437 }
3538}
Original file line number Diff line number Diff line change 44
55namespace App \Application \Commands ;
66
7- use Modules \Projects \Domain \Project ;
7+ use Modules \Projects \Domain \ProjectInterface ;
88use Spiral \Cqrs \QueryInterface ;
99
1010/**
11- * @implements QueryInterface<Project []>
11+ * @implements QueryInterface<ProjectInterface []>
1212 */
1313final class FindAllProjects implements QueryInterface {}
Original file line number Diff line number Diff line change 99use App \Application \HTTP \Response \JsonResource ;
1010use App \Application \HTTP \Response \ResourceInterface ;
1111use App \Application \Ide \UrlTemplate ;
12- use Modules \Projects \Domain \Project ;
1312use Spiral \Boot \EnvironmentInterface ;
1413use Spiral \Router \Annotation \Route ;
1514
@@ -31,10 +30,6 @@ public function __invoke(
3130 'url_template ' => $ ideUrl ->template ,
3231 ],
3332 'version ' => $ appVersion ->version ,
34- 'project ' => [
35- // todo: use better option for default project
36- 'default ' => Project::DEFAULT_KEY ,
37- ],
3833 ]);
3934 }
4035}
You can’t perform that action at this time.
0 commit comments