Skip to content

Commit

Permalink
refactor: 중속 service logic 추상화 #21, #60
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 17, 2022
1 parent 1132435 commit 08813ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
12 changes: 6 additions & 6 deletions server/apps/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export class UserController {
@Get('followers')
async getFollowers() {
try {
const _id = '63734e98384f478a32c3a1cc';
const _id = '63734f4eca63eaf1876a2c3b';
// TODO: Request Header에서 access token으로 현재 사용자 알아내기
const result = await this.userService.getFollowers(_id);
return responseForm(200, { ...result });
const result = await this.userService.getRelatedUsers(_id, 'followers');
return responseForm(200, { followers: result });
} catch (error) {
this.logger.error(JSON.stringify(error.response));
return error.response ?? error;
Expand All @@ -63,10 +63,10 @@ export class UserController {
@Get('followings')
async getFollowings() {
try {
const _id = '63734af9e62b37012c73e399';
const _id = '63739b643969101c3fec8849';
// TODO: Request Header에서 access token으로 현재 사용자 알아내기
const result = await this.userService.getFollowings(_id);
return responseForm(200, { ...result });
const result = await this.userService.getRelatedUsers(_id, 'followings');
return responseForm(200, { followings: result });
} catch (error) {
this.logger.error(JSON.stringify(error.response));
return error.response ?? error;
Expand Down
26 changes: 6 additions & 20 deletions server/apps/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,19 @@ export class UserService {
return getUserBasicInfo(user);
}

async getFollowers(_id: string) {
async getRelatedUsers(_id: string, option: string) {
const user = await this.userRepository.findById(_id);
if (!user) {
throw new BadRequestException('요청한 사용자는 없는 사용자입니다.');
}
const { followers } = user;
// TODO: 배열을 순회하면서 찾지 않고 한번에 db에서 찾도록하는 mongoose 명령 있는지 확인하기
const result = await Promise.all(
followers.map(async (followerId) => {
return getUserBasicInfo(await this.userRepository.findById(followerId));
}),
);
return { followers: result };
}

async getFollowings(_id: string) {
const user = await this.userRepository.findById(_id);
if (!user) {
throw new BadRequestException('요청한 사용자는 없는 사용자입니다.');
}
const { followings } = user;
const { followings, followers } = user;
const userIdList = option == 'followers' ? followers : followings;
// TODO: 배열을 순회하면서 찾지 않고 한번에 db에서 찾도록하는 mongoose 명령 있는지 확인하기
const result = await Promise.all(
followings.map(async (followingId) => {
return getUserBasicInfo(await this.userRepository.findById(followingId));
return await Promise.all(
userIdList.map(async (userId) => {
return getUserBasicInfo(await this.userRepository.findById(userId));
}),
);
return { followers: result };
}
}

0 comments on commit 08813ec

Please sign in to comment.