11import { Brackets , getRepository , In , IsNull , Repository } from 'typeorm' ;
2+ import Paginated from '../business/pagination/Paginated' ;
3+ import PaginationInput from '../business/pagination/PaginationInput' ;
24import Story from '../entities/Story' ;
35import StoryState from '../enum/StoryState' ;
46import getLngLatForIdentifier from './getLngLatForIdentifier' ;
7+ import { getPaginated } from './paginationUtils' ;
58
69// eslint-disable-next-line @typescript-eslint/explicit-function-return-type -- better type is inferred
710const StoryRepository = ( ) =>
811 getRepository ( Story ) . extend ( {
912 async findPublishedForPhotoIdentifier (
1013 this : Repository < Story > ,
11- identifier : string
14+ identifier : string ,
15+ pagination : PaginationInput
1216 ) {
1317 // Get the lng,lat for this photo, so we can return stories
1418 // for this photo and stories for other photos in the same location
1519 const maybeLngLat = await getLngLatForIdentifier ( identifier ) ;
1620
17- return this . createQueryBuilder ( 'story' )
21+ const qb = this . createQueryBuilder ( 'story' )
1822 . where ( { state : StoryState . PUBLISHED } )
1923 . andWhere (
2024 new Brackets ( ( qb ) => {
@@ -31,19 +35,39 @@ const StoryRepository = () =>
3135 )
3236 . leftJoinAndSelect ( 'story.photo' , 'photo' )
3337 . leftJoinAndSelect ( 'photo.effectiveAddress' , 'effectiveAddress' )
34- . leftJoinAndSelect ( 'photo.effectiveGeocode' , 'effectiveGeocode' )
35- . orderBy ( 'story.created_at' , 'DESC' )
36- . getMany ( ) ;
38+ . leftJoinAndSelect ( 'photo.effectiveGeocode' , 'effectiveGeocode' ) ;
39+ return getPaginated (
40+ qb ,
41+ {
42+ key : 'createdAt' ,
43+ sortDirection : 'DESC' ,
44+ getSerializedToken : ( story ) => story . createdAt . toISOString ( ) ,
45+ deserializeToken : ( token ) => Date . parse ( token ) ,
46+ } ,
47+ pagination
48+ ) ;
3749 } ,
3850
39- async findPublished ( this : Repository < Story > ) {
40- return this . createQueryBuilder ( 'story' )
51+ async findPublished (
52+ this : Repository < Story > ,
53+ pagination : PaginationInput
54+ ) : Promise < Paginated < Story > > {
55+ const qb = this . createQueryBuilder ( 'story' )
4156 . where ( { state : StoryState . PUBLISHED } )
42- . orderBy ( 'story.created_at' , 'DESC' )
4357 . leftJoinAndSelect ( 'story.photo' , 'photo' )
4458 . leftJoinAndSelect ( 'photo.effectiveAddress' , 'effectiveAddress' )
45- . leftJoinAndSelect ( 'photo.effectiveGeocode' , 'effectiveGeocode' )
46- . getMany ( ) ;
59+ . leftJoinAndSelect ( 'photo.effectiveGeocode' , 'effectiveGeocode' ) ;
60+
61+ return getPaginated (
62+ qb ,
63+ {
64+ key : 'createdAt' ,
65+ sortDirection : 'DESC' ,
66+ getSerializedToken : ( story ) => story . createdAt . toISOString ( ) ,
67+ deserializeToken : ( token ) => new Date ( token ) ,
68+ } ,
69+ pagination
70+ ) ;
4771 } ,
4872
4973 /**
0 commit comments