From de02d5bc8bdee45e7a41e4a3a2ecb79adcafbc3c Mon Sep 17 00:00:00 2001 From: jinyongp Date: Sun, 29 Oct 2023 13:22:06 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=AA=A8=EB=93=A0=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=20=EC=A0=95=EB=B3=B4=EC=97=90=20=ED=86=B5=EA=B3=84=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/auth/sign-in.ts | 4 +++- src/controllers/user/:id([0-9]+).ts | 9 +++------ src/controllers/user/follower/list.ts | 7 ++++++- src/controllers/user/following/list.ts | 7 ++++++- src/controllers/user/profile.ts | 16 ++++++++-------- src/models/User.ts | 11 +++++++---- src/models/index.ts | 2 +- 7 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/controllers/auth/sign-in.ts b/src/controllers/auth/sign-in.ts index abffb84..9f8b954 100644 --- a/src/controllers/auth/sign-in.ts +++ b/src/controllers/auth/sign-in.ts @@ -24,9 +24,11 @@ export default createRouter({ .then((user) => user ?? users.create(input)) .then((user) => users.sync(user.id)); + const metrics = await users.metrics(user.id); + return { accessToken: jwt.sign({ id: user.id }), - user: toUser(user), + user: toUser({ ...user, ...metrics }), }; }, }); diff --git a/src/controllers/user/:id([0-9]+).ts b/src/controllers/user/:id([0-9]+).ts index 09bc86b..92d8b55 100644 --- a/src/controllers/user/:id([0-9]+).ts +++ b/src/controllers/user/:id([0-9]+).ts @@ -1,12 +1,12 @@ import { createRouter } from "router"; import { users } from "services"; -import { User, UserMetrics, toUser } from "models"; +import { User, toUser } from "models"; import { HttpError } from "utils/http"; export default createRouter({ authorized: true, - async handler(ctx): Promise { + async handler(ctx): Promise { if (!ctx.param.id) { throw new HttpError("사용자를 찾을 수 없습니다.", "NOT_FOUND"); } @@ -15,9 +15,6 @@ export default createRouter({ throw new HttpError("사용자를 찾을 수 없습니다.", "NOT_FOUND"); } const metrics = await users.metrics(user.id); - return { - ...toUser(user), - ...metrics, - }; + return toUser({ ...user, ...metrics }); }, }); diff --git a/src/controllers/user/follower/list.ts b/src/controllers/user/follower/list.ts index 043e5c7..78a5613 100644 --- a/src/controllers/user/follower/list.ts +++ b/src/controllers/user/follower/list.ts @@ -13,6 +13,11 @@ export default createRouter({ }; const list = await users.findFollowers(options); - return list.map(toUser); + return Promise.all( + list.map(async (user) => { + const metrics = await users.metrics(user.id); + return toUser({ ...user, ...metrics }); + }), + ); }, }); diff --git a/src/controllers/user/following/list.ts b/src/controllers/user/following/list.ts index 633238f..7513e3e 100644 --- a/src/controllers/user/following/list.ts +++ b/src/controllers/user/following/list.ts @@ -13,6 +13,11 @@ export default createRouter({ }; const list = await users.findFollowings(options); - return list.map(toUser); + return Promise.all( + list.map(async (user) => { + const metrics = await users.metrics(user.id); + return toUser({ ...user, ...metrics }); + }), + ); }, }); diff --git a/src/controllers/user/profile.ts b/src/controllers/user/profile.ts index 86b011b..86f7496 100644 --- a/src/controllers/user/profile.ts +++ b/src/controllers/user/profile.ts @@ -1,18 +1,15 @@ import { createRouter } from "router"; -import { User, UserMetrics, toUser } from "models"; +import { User, toUser } from "models"; import { users } from "services"; import { nullable, optional } from "utils/validator"; export default createRouter({ authorized: true, - async handler(ctx): Promise { + async handler(ctx): Promise { const user = ctx.auth.user; const metrics = await users.metrics(user.id); - return { - ...toUser(user), - ...metrics, - }; + return toUser({ ...user, ...metrics }); }, }); @@ -24,8 +21,11 @@ export const UPDATE = createRouter({ avatarUrl: optional(nullable("string")), }, async handler(ctx): Promise { - const user = await users.update(ctx.auth.user.id, ctx.body); - return toUser(user); + const [user, metrics] = await Promise.all([ + users.update(ctx.auth.user.id, ctx.body), + users.metrics(ctx.auth.user.id), + ]); + return toUser({ ...user, ...metrics }); }, }); diff --git a/src/models/User.ts b/src/models/User.ts index ea269c1..14d47f6 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -2,9 +2,6 @@ export class User { id!: number; username!: string | null; avatarUrl!: string | null; -} - -export class UserMetrics { posts!: number; followers!: number; followings!: number; @@ -14,12 +11,18 @@ export interface RawUser { id: number; username: string | null; avatarUrl: string | null; + posts: number; + followers: number; + followings: number; } -export function toUser(raw: RawUser) { +export function toUser(raw: RawUser): User { return { id: raw.id, username: raw.username, avatarUrl: raw.avatarUrl, + posts: raw.posts, + followers: raw.followers, + followings: raw.followings, }; } diff --git a/src/models/index.ts b/src/models/index.ts index ad966b7..2eacc13 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,5 +1,5 @@ export { Auth } from "./Auth"; -export { User, UserMetrics, toUser, type RawUser } from "./User"; +export { User, toUser, type RawUser } from "./User"; export { Blog, toBlog, type RawBlog } from "./Blog"; export { Post, toPost, type RawPost } from "./Post"; export { KeywordTags, toKeywordTags, type RawKeywordTags } from "./KeywordTags";