Skip to content

Commit

Permalink
refactor: ♻️ 타입, 변수명 변경
Browse files Browse the repository at this point in the history
FollowList[] -> Follow[], cache되던 형태의 FollowListCacheType를 Follow로 변경하고 원래 Follow였던 최종 반환 형태를 MyFollow로 변경, 이에 따른 dto파일명이나 pagination 타입도 함께 맞추어 변경

- #413
  • Loading branch information
niamu01 committed Feb 20, 2024
1 parent 81e8fe5 commit bb376d6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export enum FollowSortOrder {
registerEnumType(FollowSortOrder, { name: 'FollowSortOrder' });

@ArgsType()
export class FollowListPaginatedArgs extends PaginationIndexArgs {
export class FollowPaginatedArgs extends PaginationIndexArgs {
@Field()
targetId: number;

Expand Down
13 changes: 4 additions & 9 deletions app/src/follow/follow.cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable } from '@nestjs/common';
import { CacheUtilService } from 'src/cache/cache.util.service';
import { FollowListCacheType } from './model/follow.model';
import { Follow } from './model/follow.model';

export const FOLLOW_LISTS = 'followLists';

Expand All @@ -19,7 +19,7 @@ export class FollowCacheService {
}: {
id: number;
type: 'follower' | 'following';
list: FollowListCacheType[];
list: Follow[];
}): Promise<void> {
const key = `${id}:${type}:${FOLLOW_LISTS}`;

Expand All @@ -28,15 +28,10 @@ export class FollowCacheService {
await this.cacheUtilService.set(key, list, 0);
}

async get(
userId: number,
type: 'follower' | 'following',
): Promise<FollowListCacheType[]> {
async get(userId: number, type: 'follower' | 'following'): Promise<Follow[]> {
const key = `${userId}:${type}:${FOLLOW_LISTS}`;

const cachedData = await this.cacheUtilService.get<FollowListCacheType[]>(
key,
);
const cachedData = await this.cacheUtilService.get<Follow[]>(key);

if (!cachedData) {
return []; //todo: 흠,,,
Expand Down
16 changes: 8 additions & 8 deletions app/src/follow/follow.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { PubSub } from 'graphql-subscriptions';
import { MyUserId } from 'src/auth/myContext';
import { StatAuthGuard } from 'src/auth/statAuthGuard';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { FollowListPaginatedArgs } from './dto/follow.dto.getFollowList';
import { FollowPaginatedArgs } from './dto/follow.dto';
import { FollowService } from './follow.service';
import { FollowListPaginated, FollowSuccess } from './model/follow.model';
import { FollowSuccess, MyFollowPaginated } from './model/follow.model';

const pubSub = new PubSub();

Expand Down Expand Up @@ -70,20 +70,20 @@ export class FollowResolver {
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListPaginated)
@Query((_returns) => MyFollowPaginated)
async getFollowerPaginated(
@MyUserId() userId: number,
@Args() args: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
@Args() args: FollowPaginatedArgs,
): Promise<MyFollowPaginated> {
return await this.followService.followerPaginated(userId, args);
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListPaginated)
@Query((_returns) => MyFollowPaginated)
async getFollowingPaginated(
@MyUserId() userId: number,
@Args() args: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
@Args() args: FollowPaginatedArgs,
): Promise<MyFollowPaginated> {
return await this.followService.followingPaginated(userId, args);
}
}
51 changes: 24 additions & 27 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import {
} from 'src/database/mongoose/database.mongoose.query';
import { PaginationIndexService } from 'src/pagination/index/pagination.index.service';
import { follow } from './db/follow.database.schema';
import {
FollowSortOrder,
type FollowListPaginatedArgs,
} from './dto/follow.dto.getFollowList';
import { FollowSortOrder, type FollowPaginatedArgs } from './dto/follow.dto';
import { FollowCacheService } from './follow.cache.service';
import type {
FollowList,
FollowListCacheType,
FollowListPaginated,
Follow,
FollowSuccess,
MyFollow,
MyFollowPaginated,
} from './model/follow.model';

@Injectable()
Expand Down Expand Up @@ -148,7 +145,7 @@ export class FollowService {
targetId: number,
sortOrder: FollowSortOrder,
filter?: FilterQuery<follow>,
): Promise<FollowList[]> {
): Promise<MyFollow[]> {
const aggregate = this.followModel.aggregate<follow>();

const cachedFollowerList = await this.followCacheService.get(
Expand All @@ -159,7 +156,7 @@ export class FollowService {
if (cachedFollowerList.length) {
return await this.checkFollowing({
userId,
cachedFollowList: cachedFollowerList,
followList: cachedFollowerList,
});
}

Expand All @@ -174,7 +171,7 @@ export class FollowService {

const followerList = await this.checkFollowing({
userId,
cachedFollowList: followerUserPreview,
followList: followerUserPreview,
});

await this.followCacheService.set({
Expand All @@ -188,11 +185,11 @@ export class FollowService {

async followerPaginated(
userId: number,
{ pageNumber, pageSize, targetId, sortOrder }: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
{ pageNumber, pageSize, targetId, sortOrder }: FollowPaginatedArgs,
): Promise<MyFollowPaginated> {
const followList = await this.followerList(userId, targetId, sortOrder);

return this.paginationIndexService.toPaginated<FollowList>(followList, {
return this.paginationIndexService.toPaginated<MyFollow>(followList, {
pageNumber,
pageSize,
});
Expand All @@ -203,7 +200,7 @@ export class FollowService {
targetId: number,
sortOrder: FollowSortOrder,
filter?: FilterQuery<follow>,
): Promise<FollowList[]> {
): Promise<MyFollow[]> {
const aggregate = this.followModel.aggregate<follow>();

const cachedFollowingList = await this.followCacheService.get(
Expand All @@ -214,7 +211,7 @@ export class FollowService {
if (cachedFollowingList.length) {
const followingList = await this.checkFollowing({
userId,
cachedFollowList: cachedFollowingList,
followList: cachedFollowingList,
});

if (sortOrder === FollowSortOrder.FOLLOW_AT_ASC) {
Expand All @@ -241,7 +238,7 @@ export class FollowService {

const followingList = await this.checkFollowing({
userId,
cachedFollowList: followingUserPreview,
followList: followingUserPreview,
});

await this.followCacheService.set({
Expand All @@ -255,11 +252,11 @@ export class FollowService {

async followingPaginated(
userId: number,
{ pageNumber, pageSize, targetId, sortOrder }: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
{ pageNumber, pageSize, targetId, sortOrder }: FollowPaginatedArgs,
): Promise<MyFollowPaginated> {
const followList = await this.followingList(userId, targetId, sortOrder);

return this.paginationIndexService.toPaginated<FollowList>(followList, {
return this.paginationIndexService.toPaginated<MyFollow>(followList, {
pageSize,
pageNumber,
});
Expand Down Expand Up @@ -289,7 +286,7 @@ export class FollowService {
async followingListCache(
targetId: number,
sortOrder: FollowSortOrder,
): Promise<FollowListCacheType[]> {
): Promise<Follow[]> {
const following: Pick<follow, 'followId' | 'followAt'>[] =
await this.findAllAndLean({
filter: { userId: targetId },
Expand Down Expand Up @@ -327,7 +324,7 @@ export class FollowService {
async followerListCache(
targetId: number,
sortOrder: FollowSortOrder,
): Promise<FollowListCacheType[]> {
): Promise<Follow[]> {
const follower: Pick<follow, 'userId' | 'followAt'>[] =
await this.findAllAndLean({
filter: { followId: targetId },
Expand Down Expand Up @@ -364,20 +361,20 @@ export class FollowService {

async checkFollowing({
userId,
cachedFollowList,
followList,
}: {
userId: number;
cachedFollowList: FollowListCacheType[];
}): Promise<FollowList[]> {
followList: Follow[];
}): Promise<MyFollow[]> {
const followingList = await this.followCacheService.get(
userId,
'following',
);

const followingListIds = followingList.map((e) => e.userPreview.id);

const followList = Promise.all(
cachedFollowList.map(async (follow) => {
const myFollow = Promise.all(
followList.map(async (follow) => {
const isFollowing = followingListIds.includes(follow.userPreview.id);

return {
Expand All @@ -388,7 +385,7 @@ export class FollowService {
}),
);

return followList;
return myFollow;
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/follow/model/follow.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UserPreview } from 'src/common/models/common.user.model';
import { IndexPaginated } from 'src/pagination/index/models/pagination.index.model';

@ObjectType()
export class FollowList {
export class MyFollow {
@Field()
isFollowing: boolean;

Expand All @@ -14,10 +14,10 @@ export class FollowList {
followAt: Date;
}

export type FollowListCacheType = Omit<FollowList, 'isFollowing'>;
export type Follow = Omit<MyFollow, 'isFollowing'>;

@ObjectType()
export class FollowListPaginated extends IndexPaginated(FollowList) {}
export class MyFollowPaginated extends IndexPaginated(MyFollow) {}

@ObjectType()
export class FollowSuccess {
Expand Down
2 changes: 1 addition & 1 deletion app/src/lambda/lambda.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { CacheUtilRankingService } from 'src/cache/cache.util.ranking.service';
import { CacheUtilService } from 'src/cache/cache.util.service';
import { DateRangeService } from 'src/dateRange/dateRange.service';
import { DateTemplate } from 'src/dateRange/dtos/dateRange.dto';
import { FollowSortOrder } from 'src/follow/dto/follow.dto.getFollowList';
import { FollowSortOrder } from 'src/follow/dto/follow.dto';
import { FollowCacheService } from 'src/follow/follow.cache.service';
import { FollowService } from 'src/follow/follow.service';

Expand Down
10 changes: 5 additions & 5 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ type UserRankingIndexPaginated {
pageNumber: Int!
}

type FollowList {
type MyFollow {
isFollowing: Boolean!
userPreview: UserPreview!
followAt: DateTime!
}

type FollowListPaginated {
nodes: [FollowList!]!
type MyFollowPaginated {
nodes: [MyFollow!]!
totalCount: Int!
pageSize: Int!
pageNumber: Int!
Expand Down Expand Up @@ -640,8 +640,8 @@ type Query {
getSetting: Setting!
getExpTable: [ExpTable!]!
getIsFollowing(targetId: Int!): Boolean!
getFollowerPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, targetId: Int!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowingPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, targetId: Int!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowerPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, targetId: Int!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): MyFollowPaginated!
getFollowingPaginated(pageSize: Int! = 10, pageNumber: Int! = 1, targetId: Int!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): MyFollowPaginated!
}

enum EvalLogSortOrder {
Expand Down

0 comments on commit bb376d6

Please sign in to comment.