Skip to content

Commit

Permalink
refactor: ♻️ db와 cache에 저장되는 타입이 다름을 고려 후 로직 변경
Browse files Browse the repository at this point in the history
db에 요청: userId, followId 로 follower/following 리스트를 만든 후, userPreview와 followAt 을 추가해 cache와 같은 타입으로 변환 / cache에 요청, db요청 이후: {userPreview, followAt}[] 에 로그인 한 유저에 맞춰 isFollowing 추가 후 반환
fix: 🐛 존재하는 유저인지 검사 후 db에 write
fix: 🐛 실수 수정 following->follower
프론트와 디버깅 중 발견했습니다
feat: ✨ preload 임시 적용
이후 중복되는 코드를 정리해야합니다
refactor: ♻️ 주석 삭제 및 중복되는 함수 분리
fix: 🐛 팔로우 리스트 캐시 업데이트시 배열의 push를 사용하여 무조건 뒤에 붙던 일 임시 해결
sort를 밖에서 하는 방식으로 변경할 예정입니다
feat: 🐛 isFollowing 도 cache에서 확인하도록 변경
feat: 🐛 cache된 리스트를 반환할 때도 sort한 후 반환하도록 추가
refactor: ♻️ follow시 push하기 때문에 set을 다시 호출하던 부분 삭제, 못 찾는 경우와 삭제 실패가 같은 404를 throw하여 find하지 않고 바로 delete하여 로직 줄이기
refactor: ♻️ 타입, 변수명 변경
FollowList[] -> Follow[], cache되던 형태의 FollowListCacheType를 Follow로 변경하고 원래 Follow였던 최종 반환 형태를 MyFollow로 변경, 이에 따른 dto파일명이나 pagination 타입도 함께 맞추어 변경
refactor: ♻️ 사용하는 db 변경 temp_follows -> follows
fix: 🔥 콘솔로그 삭제

- close #405
- #413
  • Loading branch information
niamu01 committed Apr 10, 2024
1 parent 2d47eb4 commit 3364738
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 155 deletions.
17 changes: 17 additions & 0 deletions app/src/api/cursusUser/cursusUser.cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CacheUtilService,
type UserFullProfileMap,
} from 'src/cache/cache.util.service';
import { UserPreview } from 'src/common/models/common.user.model';
import { DateTemplate } from 'src/dateRange/dtos/dateRange.dto';
import { CursusUserService } from './cursusUser.service';

Expand All @@ -32,6 +33,22 @@ export class CursusUserCacheService {
private readonly cacheUtilRankingService: CacheUtilRankingService,
) {}

async getUserPreview(userId: number): Promise<UserPreview | undefined> {
const userFullProfile = await this.getUserFullProfile(userId);

const cursusUserProfile = userFullProfile?.cursusUser.user;

if (!cursusUserProfile) {
return undefined;
}

return {
id: cursusUserProfile.id,
login: cursusUserProfile.login,
imgUrl: cursusUserProfile.image.link,
};
}

async getUserFullProfile(
userId: number,
): Promise<UserFullProfileCache | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion app/src/follow/db/follow.database.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HydratedDocument } from 'mongoose';

export type UserDocument = HydratedDocument<follow>;

@Schema({ collection: 'temp_follows' })
@Schema({ collection: 'follows' })
export class follow {
@Prop({ required: true })
userId: number;
Expand Down
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
19 changes: 6 additions & 13 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 { FollowList } from './model/follow.model';
import { Follow } from './model/follow.model';

export const FOLLOW_LISTS = 'followLists';

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

console.log(`setting: ${key}`);

await this.cacheUtilService.set(key, list);
await this.cacheUtilService.set(key, list, 0);
}

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

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

if (!cachedData) {
return undefined;
return []; //todo: 흠,,,
}

return cachedData;
}

//updateCacheValue(key, data); //: 주어진 키에 해당하는 캐시 값을 업데이트합니다.
}
2 changes: 1 addition & 1 deletion app/src/follow/follow.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { FollowService } from './follow.service';
CacheUtilModule,
],
providers: [FollowResolver, FollowService, FollowCacheService],
exports: [FollowCacheService],
exports: [FollowService, FollowCacheService],
})
// eslint-disable-next-line
export class FollowModule {}
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);
}
}
Loading

0 comments on commit 3364738

Please sign in to comment.