Skip to content

Commit

Permalink
fix: 모든 사용자 정보에 통계 정보 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyongp committed Oct 29, 2023
1 parent daa01ae commit de02d5b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/controllers/auth/sign-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
};
},
});
9 changes: 3 additions & 6 deletions src/controllers/user/:id([0-9]+).ts
Original file line number Diff line number Diff line change
@@ -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<User & UserMetrics> {
async handler(ctx): Promise<User> {
if (!ctx.param.id) {
throw new HttpError("사용자를 찾을 수 없습니다.", "NOT_FOUND");
}
Expand All @@ -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 });
},
});
7 changes: 6 additions & 1 deletion src/controllers/user/follower/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}),
);
},
});
7 changes: 6 additions & 1 deletion src/controllers/user/following/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}),
);
},
});
16 changes: 8 additions & 8 deletions src/controllers/user/profile.ts
Original file line number Diff line number Diff line change
@@ -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<User & UserMetrics> {
async handler(ctx): Promise<User> {
const user = ctx.auth.user;
const metrics = await users.metrics(user.id);
return {
...toUser(user),
...metrics,
};
return toUser({ ...user, ...metrics });
},
});

Expand All @@ -24,8 +21,11 @@ export const UPDATE = createRouter({
avatarUrl: optional(nullable("string")),
},
async handler(ctx): Promise<User> {
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 });
},
});

Expand Down
11 changes: 7 additions & 4 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ export class User {
id!: number;
username!: string | null;
avatarUrl!: string | null;
}

export class UserMetrics {
posts!: number;
followers!: number;
followings!: number;
Expand All @@ -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,
};
}
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit de02d5b

Please sign in to comment.