Skip to content

Commit

Permalink
feat: 각 날짜마다 감정 별 댓글 갯수를 반환하는 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
blaxsior committed Dec 6, 2023
1 parent 0a061a8 commit 7df7dfc
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 9 deletions.
16 changes: 11 additions & 5 deletions backend/server/src/analysis-comment/analysis-comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Between, DataSource, FindOperator, Repository } from 'typeorm';
import { AnalysisComment } from './entity/analysis-comment.entity';
import { CreateCommentReqDto } from './dtos/create-comment.dto';
import { ArticleContent } from './entity/article-content.entity';
import { CommentsQueriesReqDto } from './dtos/get-comments-query.dto';

@Injectable()
export class AnalysisCommentService {
Expand Down Expand Up @@ -61,8 +60,8 @@ export class AnalysisCommentService {
*/
async findManyByInfo(info: {
keyword_id: number;
emotion: string;
count: number;
emotion?: string;
count?: number;
from?: string;
to?: string;
}) {
Expand All @@ -72,7 +71,7 @@ export class AnalysisCommentService {
between = Between(new Date(from), new Date(to));
}

await this.comment_repo.find({
return await this.comment_repo.find({
where: {
keyword_id: keyword_id,
emotion: emotion,
Expand All @@ -93,7 +92,13 @@ export class AnalysisCommentService {
head_id,
from,
to,
}: CommentsQueriesReqDto) {
}: {
search: string;
head_id?: number;
psize?: number;
from?: string;
to?: string;
}) {
if (!search) return [];

const qb = this.comment_repo.createQueryBuilder();
Expand All @@ -117,6 +122,7 @@ export class AnalysisCommentService {

return await qb.limit(psize).orderBy('id', 'DESC').getRawMany();
}

/**
* 기간 내 감정 별로 최대 공감수를 받은 댓글 목록을 반환한다.
*/
Expand Down
21 changes: 21 additions & 0 deletions backend/server/src/search/dtos/commentQuery.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IsDateString, IsNumber, IsOptional, IsString } from 'class-validator';

export class GetCommentQueryReqDto {
@IsNumber()
keyword_id: number;

@IsString()
@IsOptional()
emotion?: string;

@IsNumber()
count: number;

@IsDateString()
@IsOptional()
from?: string;

@IsDateString()
@IsOptional()
to?: string;
}
10 changes: 10 additions & 0 deletions backend/server/src/search/dtos/emotion-counts.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsDateString, IsNumber } from 'class-validator';

export class GetEmotionCountsReqDto {
@IsNumber()
keyword_id: number;
@IsDateString()
from: string;
@IsDateString()
to: string;
}
17 changes: 17 additions & 0 deletions backend/server/src/search/search.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
KeywordWithTopCommentsReqQueryDto,
KeywordWithTopCommentsResDto,
} from './dtos/keyword-with-top-comments.dto';
import { GetCommentQueryReqDto } from './dtos/commentQuery.dto';
import { GetEmotionCountsReqDto } from './dtos/emotion-counts.dto';

@ApiTags('Search')
@Controller('search')
Expand Down Expand Up @@ -39,6 +41,12 @@ export class SearchController {
return results;
}

@Get('comments')
async getComments(@Query() query: GetCommentQueryReqDto) {
const info = query;
return await this.service.getCommentList(info);
}

/**
* 키워드 정보 및 기간 내 감정 별 공감 수 최대 댓글 반환
*/
Expand Down Expand Up @@ -69,4 +77,13 @@ export class SearchController {
async getCommentWithSentences(@Param('id') id: number) {
return await this.service.getCommentWithSentences(id);
}

/**
* 감정에 대한 연관된 개수 목록을 가져온다.
* @returns 날짜 + 감정 별 댓글 개수. 각 날짜에 대해 특정 감정은 존재하지 않을 수 있다.
*/
@Get('emotion-counts')
async getCountsEachEmotion(@Query() query: GetEmotionCountsReqDto) {
return await this.service.getCountsEachEmotion(query);
}
}
58 changes: 54 additions & 4 deletions backend/server/src/search/search.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { AnalysisCommentService } from '../analysis-comment/analysis-comment.service';
import { ConfigService } from '@nestjs/config';
import { KeywordService } from '../keyword/keyword.service';
import { Keyword } from '../keyword/keyword.entity';
import { Redis } from 'ioredis';
import { RedisToken } from '../redis/redis.provider';
import { AnalysisCommentService } from '../analysis-comment/analysis-comment.service';
import { ConfigService } from '@nestjs/config';

import { getDateString } from 'src/util/date';

@Injectable()
export class SearchService {
Expand Down Expand Up @@ -78,13 +80,61 @@ export class SearchService {
return commentWithSentences;
}

async getRelatedKeywordList(info: {
async getCommentList(info: {
keyword_id: number;
emotion: string;
emotion?: string;
count: number;
from?: string;
to?: string;
}) {
return await this.commentService.findManyByInfo(info);
}

async getCountsEachEmotion(info: {
keyword_id: number;
from: string;
to: string;
}) {
const { keyword_id, from, to } = info;
const comments = await this.commentService.findManyByInfo({
keyword_id,
from,
to,
});

// 임시로 데이터를 저장하기 위한 캐시
const cache = new Map();

// 댓글 갯수 많아지면 분할 처리 필요. -> 리팩토링
for (const comment of comments) {
const date_key = getDateString(comment.createdAt);

let countsForEachEmotion = cache.get(date_key);

if (!countsForEachEmotion) {
countsForEachEmotion = {};
cache.set(date_key, countsForEachEmotion);
}

if (countsForEachEmotion[comment.emotion] === undefined) {
countsForEachEmotion[comment.emotion] = 0;
}

countsForEachEmotion[comment.emotion]++;
}

const result: {
date: string;
emotions: Record<string, string>;
}[] = [];

for (const [key, value] of cache.entries()) {
result.push({
date: key,
emotions: value,
});
}

return result;
}
}
5 changes: 5 additions & 0 deletions backend/server/src/util/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function getDateString(date: Date) {
return `${date.getFullYear()}-${(date.getMonth() + 1)
.toString()
.padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
}

0 comments on commit 7df7dfc

Please sign in to comment.