66use util \{Date , Secret };
77
88class Repository {
9+ private const WITH_PARENT = [
10+ ['$lookup ' => [
11+ 'from ' => 'entries ' ,
12+ 'localField ' => 'parent ' ,
13+ 'foreignField ' => 'slug ' ,
14+ 'as ' => 'parent ' ,
15+ ]],
16+ ['$addFields ' => ['parent ' => ['$first ' => '$parent ' ]]],
17+ ];
918 private $ passwords = Hashing::sha256 ();
1019
1120 public function __construct (private Database $ database ) { }
@@ -19,13 +28,14 @@ public function authenticate(string $user, Secret $secret): ?Document {
1928 return $ cursor ->first ();
2029 }
2130
22- /** Returns newest (top-level) entries */
31+ /** Returns newest entries */
2332 public function newest (int $ limit ): array<Document > {
2433 $ cursor = $ this ->database ->collection ('entries ' )->aggregate ([
25- ['$match ' => ['parent ' => ['$eq ' => null ], 'published ' => ['$lt ' => Date::now ()]]],
34+ ['$match ' => ['is.journey ' => ['$ne ' => true ], 'published ' => ['$lt ' => Date::now ()]]],
2635 ['$unset ' => '_searchable ' ],
2736 ['$sort ' => ['date ' => -1 ]],
2837 ['$limit ' => $ limit ],
38+ ...self ::WITH_PARENT ,
2939 ]);
3040 return $ cursor ->all ();
3141 }
@@ -40,36 +50,16 @@ public function journeys(): array<Document> {
4050 return $ cursor ->all ();
4151 }
4252
43- /** Returns paginated (top-level) entries */
44- public function entries (Pagination $ pagination , int $ page , int $ children = 6 ): array<Document > {
45- $ entries = $ this ->database ->collection ('entries ' );
46- $ cursor = $ entries ->aggregate ([
47- ['$match ' => ['parent ' => ['$eq ' => null ], 'published ' => ['$lt ' => Date::now ()]]],
53+ /** Returns paginated entries */
54+ public function entries (Pagination $ pagination , int $ page ): array<Document > {
55+ $ cursor = $ this ->database ->collection ('entries ' )->aggregate ([
56+ ['$match ' => ['is.journey ' => ['$ne ' => true ], 'published ' => ['$lt ' => Date::now ()]]],
4857 ['$unset ' => '_searchable ' ],
4958 ['$sort ' => ['date ' => -1 ]],
5059 ['$skip ' => $ pagination ->skip ($ page )],
5160 ['$limit ' => $ pagination ->limit ()],
52-
53- // If no preview images are set, aggregate children
54- ['$lookup ' => [
55- 'from ' => 'entries ' ,
56- 'let ' => ['parent ' => '$slug ' , 'images ' => ['$size ' => ['$ifNull ' => ['$images ' , []]]]],
57- 'pipeline ' => [
58- ['$match ' => ['$expr ' => ['$cond ' => [
59- ['$eq ' => ['$$images ' , 0 ]],
60- ['$eq ' => ['$parent ' , '$$parent ' ]],
61- ['$eq ' => ['$_id ' , null ]],
62- ]]]]
63- ],
64- 'as ' => 'children ' ,
65- ]],
66- ['$addFields ' => ['children ' => ['$map ' => [
67- 'input ' => ['$sortArray ' => ['input ' => '$children ' , 'sortBy ' => ['date ' => -1 ]]],
68- 'as ' => 'it ' ,
69- 'in ' => ['$unsetField ' => ['input ' => '$$it ' , 'field ' => '_searchable ' ]],
70- ]]]],
61+ ...self ::WITH_PARENT ,
7162 ]);
72-
7363 return $ pagination ->paginate ($ page , $ cursor );
7464 }
7565
@@ -145,11 +135,11 @@ public function entry(string $slug, bool $published= true): ?Document {
145135 }
146136
147137 /** Returns an entry's children, latest first */
148- public function children (string $ slug ): Cursor {
138+ public function children (string $ slug, array<string, mixed > $ sort = [ ' date ' => - 1 ] ): Cursor {
149139 return $ this ->database ->collection ('entries ' )->aggregate ([
150- ['$match ' => ['parent ' => [ ' $eq ' => $ slug] , 'published ' => ['$lt ' => Date::now ()]]],
140+ ['$match ' => ['parent ' => $ slug , 'published ' => ['$lt ' => Date::now ()]]],
151141 ['$unset ' => '_searchable ' ],
152- ['$sort ' => [ ' date ' => - 1 ] ],
142+ ['$sort ' => $ sort ],
153143 ]);
154144 }
155145
0 commit comments