Skip to content

Commit

Permalink
fix: isMyFollower, isMyFollowing 동작 안 하는 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyongp committed Nov 10, 2023
1 parent fbfa250 commit 9a7ac6c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/controllers/my/followers/:id([0-9]+).ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const unfollowUser = createRouter({
authorized: true,
async handler(ctx): Promise<boolean> {
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");
}
Expand Down
17 changes: 9 additions & 8 deletions src/controllers/my/followings/:id([0-9]+).ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
async handler(ctx): Promise<User> {
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");
}

Expand All @@ -22,18 +23,18 @@ export const followUser = createRouter({
throw new HttpError("이미 팔로우한 사용자입니다.", "BAD_REQUEST");
}

return true;
return toUser(result);
},
});

export const unfollowUser = createRouter({
description: "내가 팔로우한 사용자를 언팔로우합니다.",
method: "DELETE",
authorized: true,
async handler(ctx): Promise<boolean> {
async handler(ctx): Promise<User> {
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");
}

Expand All @@ -42,6 +43,6 @@ export const unfollowUser = createRouter({
throw new HttpError("팔로우하지 않은 사용자입니다.", "BAD_REQUEST");
}

return true;
return toUser(result);
},
});
2 changes: 1 addition & 1 deletion src/controllers/users/:userId([0-9]+)/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const getUserProfile = createRouter({
description: "다른 사용자의 프로필을 가져옵니다.",
authorized: true,
async handler(ctx): Promise<User> {
const user = await users.findById(+ctx.param.userId);
const user = await users.findById(ctx.auth.user.id, +ctx.param.userId);
return toUser(user);
},
});
1 change: 0 additions & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions src/services/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -229,12 +234,17 @@ export async function unfollow(fromId: number, toId: number) {
return null;
}

return prisma.follow.delete({
await prisma.follow.delete({
where: {
followerFollowingIndex: {
followerId: fromId,
followingId: toId,
},
},
});

return prisma.user.findUnique({
where: { id: toId },
include: userInclude(fromId),
});
}

0 comments on commit 9a7ac6c

Please sign in to comment.