41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { profiles, canvases, follows } from '~/server/utils/db'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const userId = getRouterParam(event, 'id')
|
|
|
|
if (!userId) {
|
|
throw createError({ statusCode: 400, message: 'User ID is required' })
|
|
}
|
|
|
|
const profile = profiles.get(userId)
|
|
if (!profile) {
|
|
throw createError({ statusCode: 404, message: 'Profile not found' })
|
|
}
|
|
|
|
// Count user's canvases
|
|
const userCanvases = Array.from(canvases.values()).filter(
|
|
canvas => canvas.ownerId === userId
|
|
)
|
|
|
|
// Count followers and following
|
|
const followers = Array.from(follows.values()).filter(
|
|
follow => follow.followingId === userId
|
|
).length
|
|
|
|
const following = Array.from(follows.values()).filter(
|
|
follow => follow.followerId === userId
|
|
).length
|
|
|
|
// Update profile with current stats
|
|
const updatedProfile = {
|
|
...profile,
|
|
canvasCount: userCanvases.length,
|
|
followerCount: followers,
|
|
followingCount: following
|
|
}
|
|
|
|
profiles.set(userId, updatedProfile)
|
|
|
|
return { profile: updatedProfile }
|
|
})
|