diff --git a/src/modules/admin/channel/channel.controller.ts b/src/modules/admin/channel/channel.controller.ts index a1bbe2b..4f496bf 100644 --- a/src/modules/admin/channel/channel.controller.ts +++ b/src/modules/admin/channel/channel.controller.ts @@ -8,6 +8,7 @@ import { Delete, Query, UseGuards, + ValidationPipe, } from '@nestjs/common'; import { ChannelService } from './channel.service'; import { CreateChannelDto } from './dto/create-channel.dto'; @@ -33,8 +34,10 @@ export class ChannelController { } @Get() - findAll(@Query() getChannel: GetChannelDto) { - return this.channelService.findAll(getChannel); + async getChannels( + @Query(new ValidationPipe({ transform: true })) query: Record, + ) { + return this.channelService.findAll(query); } @Get(':id') diff --git a/src/modules/admin/channel/channel.service.ts b/src/modules/admin/channel/channel.service.ts index a88c5b0..38fc839 100644 --- a/src/modules/admin/channel/channel.service.ts +++ b/src/modules/admin/channel/channel.service.ts @@ -5,6 +5,8 @@ import { REQUEST } from '@nestjs/core'; import { PrismaService } from 'src/modules/prisma/prisma.service'; import { Channel } from './entities/channel.entity'; import { GetChannelDto } from './dto/get-channel.dto'; +import { PaginationDto } from 'src/common/dto/pagination.dto'; +import { paginate, PaginationResult } from 'src/common/helpers/pagination'; @Injectable() export class ChannelService { @@ -51,12 +53,23 @@ export class ChannelService { return Channel.create(channel); } - async findAll(getChannel: GetChannelDto): Promise { - const channels = await this.prisma.channel.findMany({ - where: { accountId: getChannel.accountId, deletedAt: null }, - }); - - return channels.map((channel) => Channel.create(channel)); + async findAll( + query: Record, + ): Promise> { + const getChannelDto = new GetChannelDto(); + getChannelDto.accountId = query.accountId; + + const paginationDto = new PaginationDto(); + paginationDto.page = query.page ? parseInt(query.page) : 1; + paginationDto.limit = query.limit ? parseInt(query.limit) : 10; + + return paginate( + this.prisma, + this.prisma.channel, + { accountId: getChannelDto.accountId, deletedAt: null }, + paginationDto.page, + paginationDto.limit, + ); } async findOne(id: string, getChannel: GetChannelDto): Promise {