forked from BRETT/BRETT
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { count, eq } from 'drizzle-orm'
|
|
import { comments, uploads, users } from '../../../db/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const idParam = getRouterParam(event, 'id') ?? ''
|
|
if (!/^\d+$/.test(idParam)) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Ungültige Nutzer-ID' })
|
|
}
|
|
const userId = Number(idParam)
|
|
|
|
const db = useDb()
|
|
const [user] = await db
|
|
.select({ id: users.id, name: users.name, color: users.color, avatar: users.avatar, cardEffect: users.cardEffect, cardEffectColor: users.cardEffectColor, cardVignette: users.cardVignette, cardBorderEffect: users.cardBorderEffect, cardBorderEffectColor: users.cardBorderEffectColor, createdAt: users.createdAt })
|
|
.from(users)
|
|
.where(eq(users.id, userId))
|
|
.limit(1)
|
|
|
|
if (!user) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Nutzer nicht gefunden' })
|
|
}
|
|
|
|
const [uploadStats] = await db
|
|
.select({ count: count() })
|
|
.from(uploads)
|
|
.where(eq(uploads.userId, user.id))
|
|
|
|
const [commentStats] = await db
|
|
.select({ count: count() })
|
|
.from(comments)
|
|
.where(eq(comments.userId, user.id))
|
|
|
|
return {
|
|
id: user.id,
|
|
name: user.name,
|
|
color: user.color,
|
|
cardEffect: user.cardEffect,
|
|
cardEffectColor: user.cardEffectColor, cardVignette: user.cardVignette,
|
|
cardBorderEffect: user.cardBorderEffect,
|
|
cardBorderEffectColor: user.cardBorderEffectColor,
|
|
avatarUrl: avatarUrl(user.avatar),
|
|
createdAt: user.createdAt,
|
|
stats: {
|
|
uploads: uploadStats?.count ?? 0,
|
|
comments: commentStats?.count ?? 0,
|
|
},
|
|
}
|
|
})
|