From f8414ef54d340bef4cd6b6634e934f5b5866a88c Mon Sep 17 00:00:00 2001 From: bikilaketema Date: Tue, 11 Feb 2025 18:35:11 +0300 Subject: [PATCH 1/2] Refactor channel retrieval to include pagination and validation --- .../admin/channel/channel.controller.ts | 8 ++++-- src/modules/admin/channel/channel.service.ts | 26 ++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/modules/admin/channel/channel.controller.ts b/src/modules/admin/channel/channel.controller.ts index a1bbe2b..e511e7d 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,10 +34,13 @@ 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') findOne(@Param('id') id: string, @Query() getChannel: GetChannelDto) { return this.channelService.findOne(id, getChannel); diff --git a/src/modules/admin/channel/channel.service.ts b/src/modules/admin/channel/channel.service.ts index a88c5b0..db34491 100644 --- a/src/modules/admin/channel/channel.service.ts +++ b/src/modules/admin/channel/channel.service.ts @@ -5,6 +5,9 @@ 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 +54,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 { From ed5c8e47e870e4e8c82fe9f106b022181e4579fb Mon Sep 17 00:00:00 2001 From: bikilaketema Date: Tue, 11 Feb 2025 18:36:18 +0300 Subject: [PATCH 2/2] Clean up unused imports in channel controller and service files --- src/modules/admin/channel/channel.controller.ts | 3 +-- src/modules/admin/channel/channel.service.ts | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/modules/admin/channel/channel.controller.ts b/src/modules/admin/channel/channel.controller.ts index e511e7d..4f496bf 100644 --- a/src/modules/admin/channel/channel.controller.ts +++ b/src/modules/admin/channel/channel.controller.ts @@ -8,7 +8,7 @@ import { Delete, Query, UseGuards, - ValidationPipe + ValidationPipe, } from '@nestjs/common'; import { ChannelService } from './channel.service'; import { CreateChannelDto } from './dto/create-channel.dto'; @@ -40,7 +40,6 @@ export class ChannelController { return this.channelService.findAll(query); } - @Get(':id') findOne(@Param('id') id: string, @Query() getChannel: GetChannelDto) { return this.channelService.findOne(id, getChannel); diff --git a/src/modules/admin/channel/channel.service.ts b/src/modules/admin/channel/channel.service.ts index db34491..38fc839 100644 --- a/src/modules/admin/channel/channel.service.ts +++ b/src/modules/admin/channel/channel.service.ts @@ -8,7 +8,6 @@ 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 { constructor(