Skip to content

Commit

Permalink
fix(modules/controllers): pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Apr 18, 2024
1 parent 4c5b7a2 commit cca36e4
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 107 deletions.
52 changes: 34 additions & 18 deletions src/modules/albums/albums.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ import { NotFoundException } from '@nestjs/common'
import { paginate } from 'nestjs-typeorm-paginate'

import { AlbumsController } from './albums.controller'
import { AlbumsRepository } from './albums.repository'
import { Album } from './album.entity'
import {
AlbumsRepository,
albumsSimplifiedRelations,
} from './albums.repository'

import {
albumEntityMock,
createQueryBuilderFactoryMock,
generatePaginatedResponseFactoryMock,
paginatedResponseMockImplementation,
} from '@common/mocks'

vi.mock('nestjs-typeorm-paginate')

describe('AlbumsController', () => {
const queryBuilderMock = createQueryBuilderFactoryMock(Album)

let albumsController: AlbumsController
let albumsRepository: AlbumsRepository

Expand All @@ -28,7 +27,6 @@ describe('AlbumsController', () => {
{
provide: AlbumsRepository,
useValue: {
createQueryBuilder: queryBuilderMock,
findAlbumById: vi.fn(),
},
},
Expand Down Expand Up @@ -56,10 +54,16 @@ describe('AlbumsController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(10)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
limit: 10,
page: 1,
})
expect(paginateSpy).toHaveBeenCalledWith(
albumsRepository,
{
limit: 10,
page: 1,
},
{
relations: albumsSimplifiedRelations,
}
)
})

test('should get all albums with limit', async () => {
Expand All @@ -74,10 +78,16 @@ describe('AlbumsController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(limit)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
limit,
page: 1,
})
expect(paginateSpy).toHaveBeenCalledWith(
albumsRepository,
{
limit,
page: 1,
},
{
relations: albumsSimplifiedRelations,
}
)
})

test('should get all albums with page', async () => {
Expand All @@ -93,10 +103,16 @@ describe('AlbumsController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(10)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
limit: 10,
page,
})
expect(paginateSpy).toHaveBeenCalledWith(
albumsRepository,
{
limit: 10,
page,
},
{
relations: albumsSimplifiedRelations,
}
)
})
})

Expand Down
19 changes: 11 additions & 8 deletions src/modules/albums/albums.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
} from '@nestjs/swagger'
import { Pagination, paginate } from 'nestjs-typeorm-paginate'

import { AlbumsRepository } from './albums.repository'
import {
AlbumsRepository,
albumsSimplifiedRelations,
} from './albums.repository'
import { Album } from './album.entity'

import { NOT_BEEN_FOUND } from '@common/constants'
Expand All @@ -36,13 +39,13 @@ export class AlbumsController {
type: [Pagination<Album>],
})
async getAlbums(@Query() { limit = 10, page = 1 }: PaginatedQuery) {
const queryBuilder = this.albumsRepository.createQueryBuilder('a')

queryBuilder
.leftJoinAndSelect('a.images', 'images')
.orderBy('a.name', 'ASC')

return paginate(queryBuilder, { limit, page })
return paginate(
this.albumsRepository,
{ limit, page },
{
relations: albumsSimplifiedRelations,
}
)
}

@Get(':id')
Expand Down
11 changes: 3 additions & 8 deletions src/modules/artists/artists.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import { paginate } from 'nestjs-typeorm-paginate'

import { ArtistsController } from './artists.controller'
import { ArtistsRepository } from './artists.repository'
import { Artist } from './artist.entity'

import {
artistEntityMock,
createQueryBuilderFactoryMock,
generatePaginatedResponseFactoryMock,
paginatedResponseMockImplementation,
} from '@common/mocks'

vi.mock('nestjs-typeorm-paginate')

describe('ArtistsController', () => {
const queryBuilderMock = createQueryBuilderFactoryMock(Artist)

let artistsController: ArtistsController
let artistsRepository: ArtistsRepository

Expand All @@ -28,7 +24,6 @@ describe('ArtistsController', () => {
{
provide: ArtistsRepository,
useValue: {
createQueryBuilder: queryBuilderMock,
findArtistById: vi.fn(),
},
},
Expand Down Expand Up @@ -56,7 +51,7 @@ describe('ArtistsController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(10)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
expect(paginateSpy).toHaveBeenCalledWith(artistsRepository, {
limit: 10,
page: 1,
})
Expand All @@ -76,7 +71,7 @@ describe('ArtistsController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(50)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
expect(paginateSpy).toHaveBeenCalledWith(artistsRepository, {
limit: 50,
page: 1,
})
Expand All @@ -97,7 +92,7 @@ describe('ArtistsController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(10)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
expect(paginateSpy).toHaveBeenCalledWith(artistsRepository, {
limit: 10,
page: 2,
})
Expand Down
10 changes: 2 additions & 8 deletions src/modules/artists/artists.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ export class ArtistsController {
description: 'Artists successfully found.',
type: [Pagination<Artist>],
})
async getArtists(@Query() { limit = 10, page = 1 }: PaginatedQuery) {
const queryBuilder = this.artistsRepository.createQueryBuilder('a')

queryBuilder
.leftJoinAndSelect('a.images', 'images')
.orderBy('a.name', 'ASC')

return paginate(queryBuilder, { limit, page })
getArtists(@Query() { limit = 10, page = 1 }: PaginatedQuery) {
return paginate(this.artistsRepository, { limit, page })
}

@Get(':id')
Expand Down
6 changes: 3 additions & 3 deletions src/modules/history/tracks/history-tracks.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MockProxy, mock } from 'vitest-mock-extended'

import {
HistoryTracksRepository,
order,
historyTracksOrder,
relations,
} from './history-tracks.repository'
import { HistoryTrack } from './history-track.entity'
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('HistoryTracksRepository', () => {
},
},
relations,
order,
order: historyTracksOrder,
})
})

Expand All @@ -78,7 +78,7 @@ describe('HistoryTracksRepository', () => {
},
},
relations,
order,
order: historyTracksOrder,
})
})

Expand Down
6 changes: 3 additions & 3 deletions src/modules/history/tracks/history-tracks.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CreateHistoryTrack } from './dtos'
export const relations: FindOptionsRelations<HistoryTrack> = {
user: true,
}
export const order: FindOptionsOrder<HistoryTrack> = {
export const historyTracksOrder: FindOptionsOrder<HistoryTrack> = {
playedAt: 'DESC',
}

Expand All @@ -30,7 +30,7 @@ export class HistoryTracksRepository extends Repository<HistoryTrack> {
},
},
relations,
order,
order: historyTracksOrder,
})
}

Expand All @@ -42,7 +42,7 @@ export class HistoryTracksRepository extends Repository<HistoryTrack> {
},
},
relations,
order,
order: historyTracksOrder,
})
}

Expand Down
49 changes: 31 additions & 18 deletions src/modules/tracks/tracks.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { NotFoundException } from '@nestjs/common'
import { paginate } from 'nestjs-typeorm-paginate'

import { TracksController } from './tracks.controller'
import { TracksRepository } from './tracks.repository'
import { Track } from './track.entity'
import { TracksRepository, tracksRelations } from './tracks.repository'

import {
createQueryBuilderFactoryMock,
trackEntityMock,
generatePaginatedResponseFactoryMock,
paginatedResponseMockImplementation,
Expand All @@ -16,8 +14,6 @@ import {
vi.mock('nestjs-typeorm-paginate')

describe('TracksController', () => {
const queryBuilderMock = createQueryBuilderFactoryMock(Track)

let tracksController: TracksController
let tracksRepository: TracksRepository

Expand All @@ -28,7 +24,6 @@ describe('TracksController', () => {
{
provide: TracksRepository,
useValue: {
createQueryBuilder: queryBuilderMock,
findTrackById: vi.fn(),
},
},
Expand Down Expand Up @@ -56,10 +51,16 @@ describe('TracksController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(10)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
limit: 10,
page: 1,
})
expect(paginateSpy).toHaveBeenCalledWith(
tracksRepository,
{
limit: 10,
page: 1,
},
{
relations: tracksRelations,
}
)
})

test('should get paginated tracks with limit', async () => {
Expand All @@ -74,10 +75,16 @@ describe('TracksController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(limit)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
limit,
page: 1,
})
expect(paginateSpy).toHaveBeenCalledWith(
tracksRepository,
{
limit,
page: 1,
},
{
relations: tracksRelations,
}
)
})

test('should get paginated tracks with page', async () => {
Expand All @@ -93,10 +100,16 @@ describe('TracksController', () => {

expect(response).toEqual(paginatedResponseMock)
expect(response.items.length).toEqual(10)
expect(paginateSpy).toHaveBeenCalledWith(queryBuilderMock(), {
limit: 10,
page,
})
expect(paginateSpy).toHaveBeenCalledWith(
tracksRepository,
{
limit: 10,
page,
},
{
relations: tracksRelations,
}
)
})
})

Expand Down
18 changes: 8 additions & 10 deletions src/modules/tracks/tracks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@nestjs/swagger'
import { Pagination, paginate } from 'nestjs-typeorm-paginate'

import { TracksRepository } from './tracks.repository'
import { TracksRepository, tracksRelations } from './tracks.repository'
import { Track } from './track.entity'

import { NOT_BEEN_FOUND, ONE_IS_INVALID } from '@common/constants'
Expand All @@ -37,15 +37,13 @@ export class TracksController {
type: [Pagination<Track>],
})
getTracks(@Query() { limit = 10, page = 1 }: PaginatedQuery) {
const queryBuilder = this.tracksRepository.createQueryBuilder('t')

queryBuilder
.leftJoinAndSelect('t.artists', 'artists')
.leftJoinAndSelect('t.album', 'album')
.leftJoinAndSelect('album.images', 'images')
.orderBy('t.name', 'ASC')

return paginate(queryBuilder, { limit, page })
return paginate(
this.tracksRepository,
{ limit, page },
{
relations: tracksRelations,
}
)
}

@Get(':id')
Expand Down
Loading

0 comments on commit cca36e4

Please sign in to comment.