Skip to content

Commit

Permalink
♻️ refactor: getRawOne() -\> getOne() 변경, Response DTO 변환 후 전송
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo-Minseok committed Jan 3, 2025
1 parent 3db287a commit c3c6276
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
44 changes: 38 additions & 6 deletions server/src/feed/dto/feed-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FeedView } from '../feed.entity';

export class FeedResponseDto {
export class FeedPaginationResponseDto {
private constructor(
private id: number,
private author: string,
Expand All @@ -13,8 +13,8 @@ export class FeedResponseDto {
private isNew: boolean,
) {}

private static mapFeedToFeedResponseDto(feed: FeedResponse) {
return new FeedResponseDto(
private static toFeedPaginationResponseDto(feed: FeedPaginationResult) {
return new FeedPaginationResponseDto(
feed.feedId,
feed.blogName,
feed.blogPlatform,
Expand All @@ -27,9 +27,41 @@ export class FeedResponseDto {
);
}

public static mapFeedsToFeedResponseDtoArray(FeedList: FeedResponse[]) {
return FeedList.map(this.mapFeedToFeedResponseDto);
public static mapToPaginationResponseDtoArray(
FeedList: FeedPaginationResult[],
) {
return FeedList.map(this.toFeedPaginationResponseDto);
}
}

export type FeedResponse = FeedView & { isNew: boolean };
export type FeedPaginationResult = FeedView & { isNew: boolean };

export class FeedTrendResponseDto {
private constructor(
private id: number,
private author: string,
private blogPlatform: string,
private title: string,
private path: string,
private createdAt: Date,
private thumbnail: string,
private viewCount: number,
) {}

private static toFeedTrendResponseDto(feed: FeedView) {
return new FeedTrendResponseDto(
feed.feedId,
feed.blogName,
feed.blogPlatform,
feed.title,
feed.path,
feed.createdAt,
feed.thumbnail,
feed.viewCount,
);
}

public static toFeedTrendResponseDtoArray(FeedList: FeedView[]) {
return FeedList.map(this.toFeedTrendResponseDto);
}
}
25 changes: 4 additions & 21 deletions server/src/feed/feed.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export class FeedRepository extends Repository<Feed> {
offset: number,
) {
const queryBuilder = this.createQueryBuilder('feed')
.leftJoinAndSelect('feed.blog', 'rss_accept')
.innerJoinAndSelect('feed.blog', 'rss_accept')
.addSelect(this.getMatchAgainstExpression(type, 'find'), 'relevance')
.where(this.getWhereCondition(type))
.setParameters({ find })
.where(this.getWhereCondition(type), { find })
.orderBy('relevance', 'DESC')
.addOrderBy('feed.createdAt', 'DESC')
.skip(offset)
Expand Down Expand Up @@ -71,14 +70,6 @@ export class FeedViewRepository extends Repository<FeedView> {
async findFeedPagination(queryFeedDto: QueryFeedDto) {
const { lastId, limit } = queryFeedDto;
const query = this.createQueryBuilder()
.select('feed_id', 'feedId')
.addSelect('blog_name', 'blogName')
.addSelect('blog_platform', 'blogPlatform')
.addSelect('feed_title', 'title')
.addSelect('feed_path', 'path')
.addSelect('feed_created_at', 'createdAt')
.addSelect('feed_thumbnail', 'thumbnail')
.addSelect('feed_view_count', 'viewCount')
.where((qb) => {
if (lastId) {
const subQuery = qb
Expand All @@ -94,21 +85,13 @@ export class FeedViewRepository extends Repository<FeedView> {
.orderBy('order_id', 'DESC')
.take(limit + 1);

return await query.getRawMany();
return await query.getMany();
}

async findFeedById(feedId: number) {
const feed = await this.createQueryBuilder()
.select('feed_id', 'id')
.addSelect('feed_title', 'title')
.addSelect('feed_path', 'path')
.addSelect('feed_created_at', 'createdAt')
.addSelect('feed_thumbnail', 'thumbnail')
.addSelect('feed_view_count', 'viewCount')
.addSelect('blog_name', 'author')
.addSelect('blog_platform', 'blogPlatform')
.where('feed_id = :feedId', { feedId })
.getRawOne();
.getOne();

if (!feed) {
return null;
Expand Down
24 changes: 16 additions & 8 deletions server/src/feed/feed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
import { FeedRepository, FeedViewRepository } from './feed.repository';
import { QueryFeedDto } from './dto/query-feed.dto';
import { FeedView } from './feed.entity';
import { FeedResponse, FeedResponseDto } from './dto/feed-response.dto';
import {
FeedPaginationResult,
FeedPaginationResponseDto,
FeedTrendResponseDto,
} from './dto/feed-response.dto';
import { RedisService } from '../common/redis/redis.service';
import { Cron, CronExpression } from '@nestjs/schedule';
import { EventEmitter2 } from '@nestjs/event-emitter';
Expand Down Expand Up @@ -37,7 +41,9 @@ export class FeedService {
const lastId = this.getLastIdFromFeedList(feedList);
const newCheckFeedList = await this.checkNewFeeds(feedList);
const result =
FeedResponseDto.mapFeedsToFeedResponseDtoArray(newCheckFeedList);
FeedPaginationResponseDto.mapToPaginationResponseDtoArray(
newCheckFeedList,
);
return { result, lastId, hasMore };
}

Expand All @@ -49,15 +55,15 @@ export class FeedService {
return feedList.length ? feedList[feedList.length - 1].feedId : 0;
}

private async checkNewFeeds(feedList: FeedView[]): Promise<FeedResponse[]> {
private async checkNewFeeds(feedList: FeedView[]) {
const newFeedIds = (
await this.redisService.redisClient.keys(redisKeys.FEED_RECENT_ALL_KEY)
).map((key) => {
const id = key.match(/feed:recent:(\d+)/);
return parseInt(id[1]);
});

return feedList.map((feed): FeedResponse => {
return feedList.map((feed): FeedPaginationResult => {
return {
...feed,
isNew: newFeedIds.includes(feed.feedId),
Expand All @@ -76,7 +82,9 @@ export class FeedService {
this.feedViewRepository.findFeedById(parseInt(feedId)),
),
);
return trendFeeds.filter((feed) => feed !== null);
return FeedTrendResponseDto.toFeedTrendResponseDtoArray(
trendFeeds.filter((feed) => feed !== null),
);
}

@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
Expand Down Expand Up @@ -208,7 +216,7 @@ export class FeedService {
async readRecentFeedList() {
const redis = this.redisService.redisClient;
const recentKeys = await redis.keys(redisKeys.FEED_RECENT_ALL_KEY);
const recentFeedList: FeedResponse[] = [];
const recentFeedList: FeedPaginationResult[] = [];

if (!recentKeys.length) {
return recentFeedList;
Expand All @@ -220,9 +228,9 @@ export class FeedService {
}
const result = await pipeLine.exec();
recentFeedList.push(
...result.map(([, feed]: [any, FeedResponse]) => {
...result.map(([, feed]: [any, FeedPaginationResult]) => {
feed.isNew = true;
return feed as FeedResponse;
return feed;
}),
);

Expand Down

0 comments on commit c3c6276

Please sign in to comment.