67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import { prisma } from '~/server/utils/prisma'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const userId = getRouterParam(event, 'id')
|
|
|
|
if (!userId) {
|
|
throw createError({ statusCode: 400, message: 'User ID is required' })
|
|
}
|
|
|
|
let profile = await prisma.profile.findFirst({
|
|
where: {
|
|
OR: [
|
|
{ userId: userId },
|
|
{ user: { username: userId } }
|
|
]
|
|
},
|
|
include: {
|
|
user: {
|
|
include: {
|
|
_count: {
|
|
select: { canvases: true, followers: true, following: true }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
// If profile doesn't exist but user does, create it
|
|
if (!profile) {
|
|
const user = await prisma.user.findFirst({
|
|
where: {
|
|
OR: [{ id: userId }, { username: userId }]
|
|
}
|
|
})
|
|
|
|
if (!user) {
|
|
throw createError({ statusCode: 404, message: 'User not found' })
|
|
}
|
|
|
|
profile = await prisma.profile.create({
|
|
data: {
|
|
userId: user.id,
|
|
displayName: user.username,
|
|
avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${user.id}`
|
|
},
|
|
include: {
|
|
user: {
|
|
include: {
|
|
_count: {
|
|
select: { canvases: true, followers: true, following: true }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
return {
|
|
profile: {
|
|
...profile,
|
|
canvasCount: profile.user._count.canvases,
|
|
followerCount: profile.user._count.followers,
|
|
followingCount: profile.user._count.following
|
|
}
|
|
}
|
|
})
|