From 5dea62d63594ef41c235bb2d4bf5084ac4e88b1b Mon Sep 17 00:00:00 2001 From: niamu01 Date: Thu, 14 Dec 2023 16:10:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20MakeFollow:=20=ED=94=84?= =?UTF-8?q?=EB=A1=A0=ED=8A=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=9A=A9=20?= =?UTF-8?q?=EC=9E=84=EC=8B=9C=20=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - #405 --- app/src/follow/follow.resolve.ts | 14 +++++++-- app/src/follow/follow.service.ts | 51 ++++++++++++++++++++++++++++++-- app/src/schema.gql | 3 +- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/src/follow/follow.resolve.ts b/app/src/follow/follow.resolve.ts index 4bbe135b..7a0a001d 100644 --- a/app/src/follow/follow.resolve.ts +++ b/app/src/follow/follow.resolve.ts @@ -12,6 +12,16 @@ import { FollowResult, FollowUserList } from './model/follow.model'; export class FollowResolver { constructor(private readonly followService: FollowService) {} + // 프론트 테스트용 임시 함수 + @Mutation((_returns) => FollowResult) + async MakeFollow( + @Args('to') to: string, + @Args('from') from: string, + @Args('type') type: 'follow' | 'unfollow', + ): Promise { + return await this.followService.MakeFollowUser(to, from, type); + } + @Mutation((_returns) => FollowResult) async followUser( @MyUserId() userId: number, @@ -21,11 +31,11 @@ export class FollowResolver { } @Mutation((_returns) => FollowResult) - async unFollowUser( + async unfollowUser( @MyUserId() userId: number, @Args('login') login: string, ): Promise { - return await this.followService.unFollowUser(userId, login); + return await this.followService.unfollowUser(userId, login); } @Mutation((_returns) => FollowUserList) diff --git a/app/src/follow/follow.service.ts b/app/src/follow/follow.service.ts index 963cbec2..43d617fc 100644 --- a/app/src/follow/follow.service.ts +++ b/app/src/follow/follow.service.ts @@ -14,6 +14,51 @@ export class FollowService { private readonly cursusUserService: CursusUserService, ) {} + // 프론트 테스트용 임시 함수 + async MakeFollowUser( + to: string, + from: string, + type: 'follow' | 'unfollow', + ): Promise { + const toId = await this.cursusUserService + .findOneAndLean({ + filter: { 'user.login': to }, + }) + .then((following) => following?.user.id); + + const fromId = await this.cursusUserService + .findOneAndLean({ + filter: { 'user.login': from }, + }) + .then((following) => following?.user.id); + + if (type === 'follow') { + await this.followModel + .create({ userId: fromId, followId: toId }) + .then((result) => result.toObject()); + + return { + message: 'OK', + userId: fromId!, + followId: toId!, + }; + } else if (type === 'unfollow') { + await this.followModel + .deleteOne({ + userId: fromId, + followId: toId, + }) + .then((result) => result.deletedCount); + + return { + message: 'OK', + userId: fromId!, + followId: toId!, + }; + } + return { message: 'fail' }; + } + // input으로 들어온 유저를 팔로우 함 async followUser( followerId: number, @@ -36,7 +81,9 @@ export class FollowService { followId: following, }); - if (!following || followerId === following || !alreadyFollow) { + console.log(alreadyFollow.length); + + if (!following || followerId === following || alreadyFollow.length) { return { message: 'fail' }; } @@ -53,7 +100,7 @@ export class FollowService { // input으로 들어온 유저를 언팔로우 함 // todo: unfollow 성공도 같은걸 (상태) 반환해서 이름 다시 지어야함 - async unFollowUser( + async unfollowUser( followerId: number, followingLogin: string, ): Promise { diff --git a/app/src/schema.gql b/app/src/schema.gql index 9b8c4666..baa7ecff 100644 --- a/app/src/schema.gql +++ b/app/src/schema.gql @@ -646,8 +646,9 @@ type Mutation { refreshToken(refreshToken: String!): LoginSuccess! logout: Int! deleteAccount: Int! + MakeFollow(to: String!, from: String!, type: String!): FollowResult! followUser(login: String!): FollowResult! - unFollowUser(login: String!): FollowResult! + unfollowUser(login: String!): FollowResult! getFollowerList(login: String!): FollowUserList! getFollowingList(login: String!): FollowUserList! }