From 58e13acdcb0067aaaac0107d4aa637065306f9a6 Mon Sep 17 00:00:00 2001 From: NaayoungKwon Date: Wed, 16 Nov 2022 00:53:05 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=ED=8C=94=EB=A1=9C=EC=9A=B0=20?= =?UTF-8?q?=EC=B7=A8=EC=86=8C=20=EC=9A=94=EC=B2=AD=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=A9=EB=8F=8C=20=ED=95=B4=EA=B2=B0=20#50?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{add-following.dto.ts => follower.dto.ts} | 2 +- server/apps/api/src/user/user.controller.ts | 21 +++++++++---- server/apps/api/src/user/user.service.ts | 30 +++++++++++++++++-- server/dao/repository/user.repository.ts | 13 +++++--- 4 files changed, 53 insertions(+), 13 deletions(-) rename server/apps/api/src/user/dto/{add-following.dto.ts => follower.dto.ts} (78%) diff --git a/server/apps/api/src/user/dto/add-following.dto.ts b/server/apps/api/src/user/dto/follower.dto.ts similarity index 78% rename from server/apps/api/src/user/dto/add-following.dto.ts rename to server/apps/api/src/user/dto/follower.dto.ts index 69e0cc8b..99ffd33a 100644 --- a/server/apps/api/src/user/dto/add-following.dto.ts +++ b/server/apps/api/src/user/dto/follower.dto.ts @@ -1,6 +1,6 @@ import { IsString } from 'class-validator'; -export class AddFollowingDto { +export class followerDto { @IsString() myId: string; diff --git a/server/apps/api/src/user/user.controller.ts b/server/apps/api/src/user/user.controller.ts index 4792608d..9abcccc5 100644 --- a/server/apps/api/src/user/user.controller.ts +++ b/server/apps/api/src/user/user.controller.ts @@ -1,9 +1,7 @@ -import { Body, Controller, Get, Inject, LoggerService, Param, Post } from '@nestjs/common'; +import {Body, Controller, Delete, Get, Inject, LoggerService, Param, Post} from '@nestjs/common'; import { UserService } from './user.service'; -import { AddFollowingDto } from '@user/dto/add-following.dto'; +import { followerDto } from '@user/dto/follower.dto'; import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; -import { User } from '@schemas/user.schema'; -import { CreateUserDto } from '@user/dto/create-user.dto'; import { responseForm } from '@utils/responseForm'; @Controller('api/user') @@ -25,7 +23,7 @@ export class UserController { try { const myId = '63734e98384f478a32c3a1cc'; // TODO: Request Header에서 access token으로 현재 사용자 알아내기 - const addFollowingDto: AddFollowingDto = { myId, followId: id }; + const addFollowingDto: followerDto = { myId, followId: id }; await this.userService.addFollowing(addFollowingDto); return responseForm(200, {}); } catch (error) { @@ -34,6 +32,19 @@ export class UserController { } } + @Delete('following/:id') + async unFollowing(@Param('id') 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; + } + } // @Post() // createUser(@Body() createUserDto: CreateUserDto) { diff --git a/server/apps/api/src/user/user.service.ts b/server/apps/api/src/user/user.service.ts index 78ec1559..36eab8b9 100644 --- a/server/apps/api/src/user/user.service.ts +++ b/server/apps/api/src/user/user.service.ts @@ -1,5 +1,5 @@ import { BadRequestException, ConflictException, Injectable } from '@nestjs/common'; -import { AddFollowingDto } from '@user/dto/add-following.dto'; +import { followerDto } from '@user/dto/follower.dto'; import { CreateUserDto } from '@user/dto/create-user.dto'; import { UserRepository } from '@repository/user.repository'; @@ -11,7 +11,7 @@ export class UserService { // this.userRepository.create(createUserDto); // } - async addFollowing(addFollowingDto: AddFollowingDto) { + async addFollowing(addFollowingDto: followerDto) { const user = await this.userRepository.findById(addFollowingDto.myId); const otherUser = await this.userRepository.findById(addFollowingDto.followId); if (!user || !otherUser) { @@ -22,6 +22,30 @@ export class UserService { throw new ConflictException('상대방의 팔로워 목록에 이미 있습니다.'); } this.userRepository.appendFollowing(addFollowingDto); - this.userRepository.appendFollwer(addFollowingDto); + this.userRepository.appendFollower(addFollowingDto); + } + + 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] }, + ); } } diff --git a/server/dao/repository/user.repository.ts b/server/dao/repository/user.repository.ts index 8292e2c3..b9ce0fc8 100644 --- a/server/dao/repository/user.repository.ts +++ b/server/dao/repository/user.repository.ts @@ -1,8 +1,8 @@ import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { User, UserDocument } from '@schemas/user.schema'; -import { Model } from 'mongoose'; -import { AddFollowingDto } from '@user/dto/add-following.dto'; +import mongoose, { Model, Schema, Types } from 'mongoose'; +import { followerDto } from '@user/dto/follower.dto'; import { SignUpDto } from '@api/src/auth/dto'; @Injectable() @@ -22,7 +22,8 @@ export class UserRepository { return await this.userModel.findById(_id); } - appendFollowing(addFollowingDto: AddFollowingDto) { + appendFollowing(addFollowingDto: followerDto) { + // TODO : 하위 append follower와 합치도록 추상화하기 this.userModel.updateOne( { _id: addFollowingDto.myId }, { $push: { followings: addFollowingDto.followId } }, @@ -32,7 +33,7 @@ export class UserRepository { ); } - appendFollwer(addFollowingDto: AddFollowingDto) { + appendFollower(addFollowingDto: followerDto) { this.userModel.updateOne( { _id: addFollowingDto.followId }, { @@ -43,4 +44,8 @@ export class UserRepository { }, ); } + + async deleteElementAtArr(condition, removeElement) { + await this.userModel.updateOne(condition, { $pullAll: removeElement }); + } } From 19406be5c32d037e5a5dc69b24026f8b23915531 Mon Sep 17 00:00:00 2001 From: NaayoungKwon Date: Wed, 16 Nov 2022 01:00:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=ED=8C=94=EB=A1=9C=EC=9A=B0=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=20repository=20=EC=B6=94=EC=83=81=ED=99=94?= =?UTF-8?q?=20#20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/apps/api/src/user/user.service.ts | 12 ++++++++--- server/dao/repository/user.repository.ts | 27 ++++-------------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/server/apps/api/src/user/user.service.ts b/server/apps/api/src/user/user.service.ts index 36eab8b9..428a3061 100644 --- a/server/apps/api/src/user/user.service.ts +++ b/server/apps/api/src/user/user.service.ts @@ -1,6 +1,5 @@ import { BadRequestException, ConflictException, Injectable } from '@nestjs/common'; import { followerDto } from '@user/dto/follower.dto'; -import { CreateUserDto } from '@user/dto/create-user.dto'; import { UserRepository } from '@repository/user.repository'; @Injectable() @@ -21,8 +20,15 @@ export class UserService { } else if (otherUser.followers.includes(addFollowingDto.myId)) { throw new ConflictException('상대방의 팔로워 목록에 이미 있습니다.'); } - this.userRepository.appendFollowing(addFollowingDto); - this.userRepository.appendFollower(addFollowingDto); + + this.userRepository.appendElementAtArr( + { _id: addFollowingDto.myId }, + { followings: addFollowingDto.followId }, + ); + this.userRepository.appendElementAtArr( + { _id: addFollowingDto.followId }, + { followers: addFollowingDto.myId }, + ); } async unFollowing(unFollowingDto: followerDto) { diff --git a/server/dao/repository/user.repository.ts b/server/dao/repository/user.repository.ts index b9ce0fc8..8172ab67 100644 --- a/server/dao/repository/user.repository.ts +++ b/server/dao/repository/user.repository.ts @@ -22,30 +22,11 @@ export class UserRepository { return await this.userModel.findById(_id); } - appendFollowing(addFollowingDto: followerDto) { - // TODO : 하위 append follower와 합치도록 추상화하기 - this.userModel.updateOne( - { _id: addFollowingDto.myId }, - { $push: { followings: addFollowingDto.followId } }, - (err, res) => { - if (err) throw err; - }, - ); + async appendElementAtArr(filter, appendElement) { + await this.userModel.updateOne(filter, { $push: appendElement }); } - appendFollower(addFollowingDto: followerDto) { - this.userModel.updateOne( - { _id: addFollowingDto.followId }, - { - $push: { followers: addFollowingDto.myId }, - }, - (err, res) => { - if (err) throw err; - }, - ); - } - - async deleteElementAtArr(condition, removeElement) { - await this.userModel.updateOne(condition, { $pullAll: removeElement }); + async deleteElementAtArr(filter, removeElement) { + await this.userModel.updateOne(filter, { $pullAll: removeElement }); } }