Skip to content

Commit

Permalink
♻️ refactor: statistic controller, service, api-docs 메서드명 수정 및 dao 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
asn6878 committed Dec 2, 2024
1 parent 7e4ff94 commit be14d56
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 62 deletions.
10 changes: 10 additions & 0 deletions server/src/feed/feed.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ export class FeedRepository extends Repository<Feed> {
relations: ['blog'],
});
}

async findAllStatisticsOrderByViewCount(limit: number) {
return this.find({
select: ['id', 'title', 'viewCount'],
order: {
viewCount: 'DESC',
},
take: limit,
});
}
}
42 changes: 42 additions & 0 deletions server/src/statistic/api-docs/readPlatformStatistic.api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { applyDecorators } from '@nestjs/common';
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger';

export function ApiReadPlatformStatistic() {
return applyDecorators(
ApiOperation({
summary: '블로그 플랫폼 통계 조회 API',
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
data: {
type: 'array',
items: {
properties: {
platform: {
type: 'string',
},
count: {
type: 'number',
},
},
},
},
},
},
example: {
message: '블로그 플랫폼 통계 조회 완료',
data: [
{
platform: 'test',
count: 30,
},
],
},
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {
ApiOkResponse,
ApiOperation,
ApiQuery,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';


export function ApiStatistic(category: 'today' | 'all') {
const type = category === 'all' ? '전체' : '금일';
return applyDecorators(
Expand Down Expand Up @@ -66,43 +64,3 @@ export function ApiStatistic(category: 'today' | 'all') {
}),
);
}

export function ApiPlatformStatistic() {
return applyDecorators(
ApiOperation({
summary: '블로그 플랫폼 통계 조회 API',
}),
ApiOkResponse({
description: 'Ok',
schema: {
properties: {
message: {
type: 'string',
},
data: {
type: 'array',
items: {
properties: {
platform: {
type: 'string',
},
count: {
type: 'number',
},
},
},
},
},
},
example: {
message: '블로그 플랫폼 통계 조회 완료',
data: [
{
platform: 'test',
count: 30,
},
],
},
}),
);
}
19 changes: 9 additions & 10 deletions server/src/statistic/statistic.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import {
Controller,
Get,
Query,
UseGuards,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiPlatformStatistic, ApiStatistic } from './statistic.api-docs';
import { StatisticService } from './statistic.service';
import { ApiResponse } from '../common/response/common.response';
import { ApiTags } from '@nestjs/swagger';
import { CookieAuthGuard } from '../common/guard/auth.guard';
import { StatisticQueryDto } from './dto/statistic-query.dto';
import { ApiReadPlatformStatistic } from './api-docs/readPlatformStatistic.api-docs';
import { ApiStatistic } from './api-docs/statistic.api-docs';

@ApiTags('Statistic')
@Controller('statistic')
Expand All @@ -25,8 +24,8 @@ export class StatisticController {
transform: true,
}),
)
async getTodayStatistic(@Query() queryObj: StatisticQueryDto) {
const data = await this.statisticService.getTodayViewCount(queryObj.limit);
async readTodayStatistic(@Query() queryObj: StatisticQueryDto) {
const data = await this.statisticService.readTodayStatistic(queryObj.limit);
return ApiResponse.responseWithData('금일 조회수 통계 조회 완료', data);
}

Expand All @@ -37,15 +36,15 @@ export class StatisticController {
transform: true,
}),
)
async getAllStatistic(@Query() queryObj: StatisticQueryDto) {
const data = await this.statisticService.getAllViewCount(queryObj.limit);
async readAllStatistic(@Query() queryObj: StatisticQueryDto) {
const data = await this.statisticService.readAllStatistic(queryObj.limit);
return ApiResponse.responseWithData('전체 조회수 통계 조회 완료', data);
}

@ApiPlatformStatistic()
@ApiReadPlatformStatistic()
@Get('platform')
async getPlatformStatistic() {
const data = await this.statisticService.getPlatformGroupCount();
async readPlatformStatistic() {
const data = await this.statisticService.readPlatformStatistic();
return ApiResponse.responseWithData('블로그 플랫폼 통계 조회 완료', data);
}
}
15 changes: 5 additions & 10 deletions server/src/statistic/statistic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class StatisticService {
private readonly feedRepository: FeedRepository,
private readonly rssAcceptRepository: RssAcceptRepository,
) {}
async getTodayViewCount(limit: number) {
async readTodayStatistic(limit: number) {
const ranking = await this.redisService.redisClient.zrevrange(
redisKeys.FEED_TREND_KEY,
0,
Expand All @@ -37,18 +37,13 @@ export class StatisticService {
return result;
}

async getAllViewCount(limit: number) {
const ranking = await this.feedRepository.find({
select: ['id', 'title', 'viewCount'],
order: {
viewCount: 'DESC',
},
take: limit,
});
async readAllStatistic(limit: number) {
const ranking =
await this.feedRepository.findAllStatisticsOrderByViewCount(limit);
return ranking;
}

async getPlatformGroupCount() {
async readPlatformStatistic() {
const platformStatistics =
await this.rssAcceptRepository.countByBlogPlatform();
return PlatformResponseDto.platformToResults(platformStatistics);
Expand Down

0 comments on commit be14d56

Please sign in to comment.