From 9a7ac6c1a4689260d7741ba899a41fe167364d4e Mon Sep 17 00:00:00 2001 From: jinyongp Date: Fri, 10 Nov 2023 20:00:01 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20isMyFollower,=20isMyFollowing=20?= =?UTF-8?q?=EB=8F=99=EC=9E=91=20=EC=95=88=20=ED=95=98=EB=8A=94=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/my/followers/:id([0-9]+).ts | 2 +- src/controllers/my/followings/:id([0-9]+).ts | 17 ++++++++-------- .../users/:userId([0-9]+)/index.ts | 2 +- src/models/User.ts | 1 - src/services/users.ts | 20 ++++++++++++++----- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/controllers/my/followers/:id([0-9]+).ts b/src/controllers/my/followers/:id([0-9]+).ts index 75b3825..fa16024 100644 --- a/src/controllers/my/followers/:id([0-9]+).ts +++ b/src/controllers/my/followers/:id([0-9]+).ts @@ -8,7 +8,7 @@ export const unfollowUser = createRouter({ authorized: true, async handler(ctx): Promise { const userId = +ctx.param.id; - const toUser = await users.findById(userId); + const toUser = await users.findById(ctx.auth.user.id, userId); if (!toUser) { throw new HttpError("사용자를 찾을 수 없습니다.", "NOT_FOUND"); } diff --git a/src/controllers/my/followings/:id([0-9]+).ts b/src/controllers/my/followings/:id([0-9]+).ts index 4eb7ccc..d1f0257 100644 --- a/src/controllers/my/followings/:id([0-9]+).ts +++ b/src/controllers/my/followings/:id([0-9]+).ts @@ -1,19 +1,20 @@ import { createRouter } from "router"; import { users } from "services"; import { HttpError } from "utils/http"; +import { User, toUser } from "models"; export const followUser = createRouter({ description: "다른 사용자를 팔로우합니다.", method: "POST", authorized: true, - async handler(ctx): Promise { + async handler(ctx): Promise { const userId = +ctx.param.id; if (ctx.auth.user.id === userId) { throw new HttpError("본인을 팔로우할 수 없습니다.", "BAD_REQUEST"); } - const toUser = await users.findById(userId); - if (!toUser) { + const user = await users.findById(ctx.auth.user.id, userId); + if (!user) { throw new HttpError("사용자를 찾을 수 없습니다.", "NOT_FOUND"); } @@ -22,7 +23,7 @@ export const followUser = createRouter({ throw new HttpError("이미 팔로우한 사용자입니다.", "BAD_REQUEST"); } - return true; + return toUser(result); }, }); @@ -30,10 +31,10 @@ export const unfollowUser = createRouter({ description: "내가 팔로우한 사용자를 언팔로우합니다.", method: "DELETE", authorized: true, - async handler(ctx): Promise { + async handler(ctx): Promise { const userId = +ctx.param.id; - const toUser = await users.findById(userId); - if (!toUser) { + const user = await users.findById(ctx.auth.user.id, userId); + if (!user) { throw new HttpError("사용자를 찾을 수 없습니다.", "NOT_FOUND"); } @@ -42,6 +43,6 @@ export const unfollowUser = createRouter({ throw new HttpError("팔로우하지 않은 사용자입니다.", "BAD_REQUEST"); } - return true; + return toUser(result); }, }); diff --git a/src/controllers/users/:userId([0-9]+)/index.ts b/src/controllers/users/:userId([0-9]+)/index.ts index f23d3ea..cbecc3d 100644 --- a/src/controllers/users/:userId([0-9]+)/index.ts +++ b/src/controllers/users/:userId([0-9]+)/index.ts @@ -6,7 +6,7 @@ export const getUserProfile = createRouter({ description: "다른 사용자의 프로필을 가져옵니다.", authorized: true, async handler(ctx): Promise { - const user = await users.findById(+ctx.param.userId); + const user = await users.findById(ctx.auth.user.id, +ctx.param.userId); return toUser(user); }, }); diff --git a/src/models/User.ts b/src/models/User.ts index 7ba1b95..5017912 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -37,7 +37,6 @@ export interface RawUser { } export function toUser(raw: RawUser): User { - console.log(raw.id, raw.followers); return { id: raw.id, username: raw.username, diff --git a/src/services/users.ts b/src/services/users.ts index 171c311..1fedea2 100644 --- a/src/services/users.ts +++ b/src/services/users.ts @@ -49,11 +49,11 @@ export async function findAuthUser(id: number) { }); } -export async function findById(id: number) { +export async function findById(authUserId: number, userId: number) { try { return await prisma.user.findUniqueOrThrow({ - where: { id }, - include: userInclude(id), + where: { id: userId }, + include: userInclude(authUserId), }); } catch (error) { if (error instanceof PrismaClientKnownRequestError) { @@ -212,12 +212,17 @@ export async function follow(fromId: number, toId: number) { return null; } - return prisma.follow.create({ + await prisma.follow.create({ data: { followerId: fromId, followingId: toId, }, }); + + return prisma.user.findUnique({ + where: { id: toId }, + include: userInclude(fromId), + }); } export async function unfollow(fromId: number, toId: number) { @@ -229,7 +234,7 @@ export async function unfollow(fromId: number, toId: number) { return null; } - return prisma.follow.delete({ + await prisma.follow.delete({ where: { followerFollowingIndex: { followerId: fromId, @@ -237,4 +242,9 @@ export async function unfollow(fromId: number, toId: number) { }, }, }); + + return prisma.user.findUnique({ + where: { id: toId }, + include: userInclude(fromId), + }); }