Skip to content

Commit

Permalink
refactor: follow, unfollow toggle 방식으로 변경 #20,#50
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 17, 2022
1 parent 08813ec commit 0249e4d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 55 deletions.
2 changes: 1 addition & 1 deletion server/apps/api/src/user/dto/follower.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsString } from 'class-validator';

export class followerDto {
export class FollowerDto {
@IsString()
myId: string;

Expand Down
32 changes: 16 additions & 16 deletions server/apps/api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Delete, Get, Inject, LoggerService, Param, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { followerDto } from '@user/dto/follower.dto';
import { FollowerDto } from '@user/dto/follower.dto';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { responseForm } from '@utils/responseForm';
import { ObjectIdValidationPipe } from '@custom_pipe/mongodbObjectIdValidation.pipe';
Expand All @@ -24,28 +24,28 @@ export class UserController {
try {
const myId = '63739b643969101c3fec8849';
// TODO: Request Header에서 access token으로 현재 사용자 알아내기
const addFollowingDto: followerDto = { myId, followId: id };
await this.userService.addFollowing(addFollowingDto);
const addFollowingDto: FollowerDto = { myId, followId: id };
const result = await this.userService.toggleFollowing(addFollowingDto);
return responseForm(200, {});
} catch (error) {
this.logger.error(JSON.stringify(error.response));
return error.response;
}
}

@Delete('following/:id')
async unFollowing(@Param('id', ObjectIdValidationPipe) id: string) {
try {
const myId = '63734e98384f478a32c3a1cc';
// TODO: Request Header에서 access token으로 현재 사용자 알아내기
const unFollowingDto: followerDto = { myId, followId: id };
await this.userService.unFollowing(unFollowingDto);
return responseForm(200, {});
} catch (error) {
this.logger.error(JSON.stringify(error.response));
return error.response;
}
}
// @Delete('following/:id')
// async unFollowing(@Param('id', ObjectIdValidationPipe) id: string) {
// try {
// const myId = '63734e98384f478a32c3a1cc';
// // TODO: Request Header에서 access token으로 현재 사용자 알아내기
// const unFollowingDto: followerDto = { myId, followId: id };
// await this.userService.toggleFollowing(unFollowingDto);
// return responseForm(200, {});
// } catch (error) {
// this.logger.error(JSON.stringify(error.response));
// return error.response;
// }
// }

@Get('followers')
async getFollowers() {
Expand Down
112 changes: 74 additions & 38 deletions server/apps/api/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BadRequestException, ConflictException, Injectable, Param } from '@nestjs/common';
import { followerDto } from '@user/dto/follower.dto';
import { FollowerDto } from '@user/dto/follower.dto';
import { UserRepository } from '@repository/user.repository';
import { getUserBasicInfo } from '@user/dto/user-basic-info.dto';

Expand All @@ -11,49 +11,85 @@ export class UserService {
// this.userRepository.create(createUserDto);
// }

async addFollowing(addFollowingDto: followerDto) {
const user = await this.userRepository.findById(addFollowingDto.myId);
const otherUser = await this.userRepository.findById(addFollowingDto.followId);
if (!user || !otherUser) {
throw new BadRequestException('해당하는 사용자의 _id가 올바르지 않습니다.');
} else if (user.followings.includes(addFollowingDto.followId)) {
throw new BadRequestException('팔로우 요청한 사용자는 이미 팔로우되어있습니다.');
} else if (otherUser.followers.includes(addFollowingDto.myId)) {
throw new ConflictException('상대방의 팔로워 목록에 이미 있습니다.');
}

this.userRepository.appendElementAtArr(
{ _id: addFollowingDto.myId },
{ followings: addFollowingDto.followId },
);
this.userRepository.appendElementAtArr(
{ _id: addFollowingDto.followId },
{ followers: addFollowingDto.myId },
);
}
// async addFollowing(addFollowingDto: followerDto) {
// const user = await this.userRepository.findById(addFollowingDto.myId);
// const otherUser = await this.userRepository.findById(addFollowingDto.followId);
// if (!user || !otherUser) {
// throw new BadRequestException('해당하는 사용자의 _id가 올바르지 않습니다.');
// } else if (user.followings.includes(addFollowingDto.followId)) {
// throw new BadRequestException('팔로우 요청한 사용자는 이미 팔로우되어있습니다.');
// } else if (otherUser.followers.includes(addFollowingDto.myId)) {
// throw new ConflictException('상대방의 팔로워 목록에 이미 있습니다.');
// }
//
// this.userRepository.appendElementAtArr(
// { _id: addFollowingDto.myId },
// { followings: addFollowingDto.followId },
// );
// this.userRepository.appendElementAtArr(
// { _id: addFollowingDto.followId },
// { followers: addFollowingDto.myId },
// );
// }
//
// async unFollowing(unFollowingDto: followerDto) {
// const user = await this.userRepository.findById(unFollowingDto.myId);
// const otherUser = await this.userRepository.findById(unFollowingDto.followId);
// if (!user || !otherUser) {
// throw new BadRequestException('해당하는 사용자의 _id가 올바르지 않습니다.');
// } else if (!user.followings.includes(unFollowingDto.followId)) {
// throw new BadRequestException(
// `팔로우 요청한 사용자 ${user.nickname}은 ${otherUser.nickname}을 팔로우하고 있지 않습니다.`,
// );
// } else if (!otherUser.followers.includes(unFollowingDto.myId)) {
// throw new ConflictException(
// `${otherUser.nickname}의 팔로워 목록에 ${user.nickname}가 없습니다.`,
// );
// }
// this.userRepository.deleteElementAtArr(
// { _id: unFollowingDto.myId },
// { followings: [unFollowingDto.followId] },
// );
// this.userRepository.deleteElementAtArr(
// { _id: unFollowingDto.followId },
// { followers: [unFollowingDto.myId] },
// );
// }

async unFollowing(unFollowingDto: followerDto) {
const user = await this.userRepository.findById(unFollowingDto.myId);
const otherUser = await this.userRepository.findById(unFollowingDto.followId);
async toggleFollowing(followerDto: FollowerDto) {
const user = await this.userRepository.findById(followerDto.myId);
const otherUser = await this.userRepository.findById(followerDto.followId);
const isAlreadyFollow = user.followings.includes(followerDto.followId);
const isAlreadyFollowAtOther = otherUser.followers.includes(followerDto.myId);
if (!user || !otherUser) {
throw new BadRequestException('해당하는 사용자의 _id가 올바르지 않습니다.');
} else if (!user.followings.includes(unFollowingDto.followId)) {
throw new BadRequestException(
`팔로우 요청한 사용자 ${user.nickname}${otherUser.nickname}을 팔로우하고 있지 않습니다.`,
);
} else if (!otherUser.followers.includes(unFollowingDto.myId)) {
} else if (!isAlreadyFollow && isAlreadyFollowAtOther) {
throw new ConflictException('갱신 이상! (팔로우 안되어있으나, 상대방에겐 내가 팔로우됨)');
} else if (isAlreadyFollow && !isAlreadyFollowAtOther) {
throw new ConflictException(
`${otherUser.nickname}의 팔로워 목록에 ${user.nickname}가 없습니다.`,
'갱신 이상! (팔로우 되어있으나, 상대방에겐 내가 팔로우되어있지 않음)',
);
} else if (!isAlreadyFollow && !isAlreadyFollowAtOther) {
// 팔로우 되어있지 않은 경우 팔로우 필요
this.userRepository.appendElementAtArr(
{ _id: followerDto.myId },
{ followings: followerDto.followId },
);
this.userRepository.appendElementAtArr(
{ _id: followerDto.followId },
{ followers: followerDto.myId },
);
} else if (isAlreadyFollow && isAlreadyFollowAtOther) {
// 팔로우 되어있어 언팔로우 필요
this.userRepository.deleteElementAtArr(
{ _id: followerDto.myId },
{ followings: [followerDto.followId] },
);
this.userRepository.deleteElementAtArr(
{ _id: followerDto.followId },
{ followers: [followerDto.myId] },
);
}
this.userRepository.deleteElementAtArr(
{ _id: unFollowingDto.myId },
{ followings: [unFollowingDto.followId] },
);
this.userRepository.deleteElementAtArr(
{ _id: unFollowingDto.followId },
{ followers: [unFollowingDto.myId] },
);
}

async getUser(_id: string) {
Expand Down

0 comments on commit 0249e4d

Please sign in to comment.