Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor: Response DTO 적용, request/response 분리 #9

Merged
merged 9 commits into from
Jan 9, 2025
Prev Previous commit
Next Next commit
🧼 clean: 코드 일관성 있게 함수명 통일
  • Loading branch information
Jo-Minseok committed Jan 8, 2025
commit 4364ae96f6fa3583b57ad186e611751b80b3cce8
12 changes: 6 additions & 6 deletions server/src/feed/dto/response/feed-pagination.dto.ts
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ export class FeedResult {
private isNew: boolean,
) {}

private static toPaginationResultDto(feed: FeedPaginationResult) {
private static toResultDto(feed: FeedPaginationResult) {
return new FeedResult(
feed.feedId,
feed.blogName,
@@ -27,8 +27,8 @@ export class FeedResult {
);
}

public static toPaginationResultDtoArray(feedList: FeedPaginationResult[]) {
return feedList.map(this.toPaginationResultDto);
public static toResultDtoArray(feedList: FeedPaginationResult[]) {
return feedList.map(this.toResultDto);
}
}

@@ -54,7 +54,7 @@ export class FeedTrendResponseDto {
private viewCount: number,
) {}

private static toFeedTrendResponseDto(feed: FeedView) {
private static toResponseDto(feed: FeedView) {
return new FeedTrendResponseDto(
feed.feedId,
feed.blogName,
@@ -67,7 +67,7 @@ export class FeedTrendResponseDto {
);
}

public static toFeedTrendResponseDtoArray(FeedList: FeedView[]) {
return FeedList.map(this.toFeedTrendResponseDto);
public static toResponseDtoArray(FeedList: FeedView[]) {
return FeedList.map(this.toResponseDto);
}
}
6 changes: 3 additions & 3 deletions server/src/feed/dto/response/recent.dto.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ export class FeedRecentResponseDto {
private isNew: boolean,
) {}

static toRecentResponseDto(feed: FeedRecentRedis) {
static toResponseDto(feed: FeedRecentRedis) {
return new FeedRecentResponseDto(
feed.id,
feed.blogName,
@@ -25,8 +25,8 @@ export class FeedRecentResponseDto {
);
}

static toRecentResponseDtoArray(feeds: FeedRecentRedis[]) {
return feeds.map(this.toRecentResponseDto);
static toResponseDtoArray(feeds: FeedRecentRedis[]) {
return feeds.map(this.toResponseDto);
}
}

35 changes: 23 additions & 12 deletions server/src/feed/dto/response/search-feed.dto.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import { Feed } from '../../entity/feed.entity';

export class SearchFeedResult {
constructor(
private constructor(
private id: number,
private blogName: string,
private title: string,
private path: string,
private createdAt: Date,
) {}

static feedsToResults(feeds: Feed[]): SearchFeedResult[] {
return feeds.map((item) => {
return new SearchFeedResult(
item.id,
item.blog.name,
item.title,
item.path,
item.createdAt,
);
});
static toResultDto(feed: Feed) {
return new SearchFeedResult(
feed.id,
feed.blog.name,
feed.title,
feed.path,
feed.createdAt,
);
}

static toResultDtoArray(feeds: Feed[]) {
return feeds.map(this.toResultDto);
}
}

export class SearchFeedResponseDto {
constructor(
private constructor(
private totalCount: number,
private result: SearchFeedResult[],
private totalPages: number,
private limit: number,
) {}

static toResponseDto(
totalCount: number,
feeds: SearchFeedResult[],
totalPages: number,
limit: number,
) {
return new SearchFeedResponseDto(totalCount, feeds, totalPages, limit);
}
}
39 changes: 23 additions & 16 deletions server/src/feed/service/feed.service.ts
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ export class FeedService {
if (hasMore) feedList.pop();
const lastId = this.getLastIdFromFeedList(feedList);
const newCheckFeedList = await this.checkNewFeeds(feedList);
const result = FeedResult.toPaginationResultDtoArray(newCheckFeedList);
const result = FeedResult.toResultDtoArray(newCheckFeedList);
return new FeedPaginationResponseDto(result, lastId, hasMore);
}

@@ -87,7 +87,7 @@ export class FeedService {
this.feedViewRepository.findFeedById(parseInt(feedId)),
),
);
return FeedTrendResponseDto.toFeedTrendResponseDtoArray(
return FeedTrendResponseDto.toResponseDtoArray(
trendFeeds.filter((feed) => feed !== null),
);
}
@@ -121,20 +121,27 @@ export class FeedService {
async searchFeedList(searchFeedReq: SearchFeedRequestDto) {
const { find, page, limit, type } = searchFeedReq;
const offset = (page - 1) * limit;
if (this.validateSearchType(type)) {
const [result, totalCount] = await this.feedRepository.searchFeedList(
find,
limit,
type,
offset,
);

const results = SearchFeedResult.feedsToResults(result);
const totalPages = Math.ceil(totalCount / limit);

return new SearchFeedResponseDto(totalCount, results, totalPages, limit);

if (!this.validateSearchType(type)) {
throw new BadRequestException('검색 타입이 잘못되었습니다.');
}
throw new BadRequestException('검색 타입이 잘못되었습니다.');

const [result, totalCount] = await this.feedRepository.searchFeedList(
find,
limit,
type,
offset,
);

const feeds = SearchFeedResult.toResultDtoArray(result);
const totalPages = Math.ceil(totalCount / limit);

return SearchFeedResponseDto.toResponseDto(
totalCount,
feeds,
totalPages,
limit,
);
}

private validateSearchType(type: string) {
@@ -242,7 +249,7 @@ export class FeedService {
return dateNext.getTime() - dateCurrent.getTime();
});

return FeedRecentResponseDto.toRecentResponseDtoArray(recentFeedList);
return FeedRecentResponseDto.toResponseDtoArray(recentFeedList);
}

private getIp(request: Request) {