mirror of
https://github.com/ArthurDanjou/arthome.git
synced 2026-01-14 12:14:33 +01:00
Working
This commit is contained in:
@@ -8,12 +8,7 @@ export default defineEventHandler(async (event) => {
|
||||
const body = await useValidatedBody(event, UpdateCategorySchema)
|
||||
await useDrizzle()
|
||||
.update(tables.categories)
|
||||
.set({
|
||||
name: body.name,
|
||||
icon: body.icon,
|
||||
color: body.color,
|
||||
nameVisible: body.nameVisible,
|
||||
})
|
||||
.set(body)
|
||||
.where(
|
||||
and(
|
||||
eq(tables.categories.id, id),
|
||||
|
||||
@@ -6,11 +6,8 @@ export default defineEventHandler(async (event) => {
|
||||
const user = await getUserSession(event)
|
||||
const body = await useValidatedBody(event, CreateCategorySchema)
|
||||
await useDrizzle().insert(tables.categories).values({
|
||||
name: body.name,
|
||||
icon: body.icon,
|
||||
color: body.color,
|
||||
nameVisible: body.nameVisible,
|
||||
userId: user.id,
|
||||
...body,
|
||||
})
|
||||
return { statusCode: 200 }
|
||||
}
|
||||
|
||||
@@ -7,13 +7,7 @@ export default defineEventHandler(async (event) => {
|
||||
const body = await useValidatedBody(event, UpdateTabSchema)
|
||||
await useDrizzle()
|
||||
.update(tables.tabs)
|
||||
.set({
|
||||
name: body.name,
|
||||
icon: body.icon,
|
||||
color: body.color,
|
||||
primary: body.primary,
|
||||
link: body.link,
|
||||
})
|
||||
.set(body)
|
||||
.where(
|
||||
and(
|
||||
eq(tables.tabs.id, id),
|
||||
|
||||
@@ -4,14 +4,7 @@ import { CreateTabSchema } from '~~/types/types'
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const body = await useValidatedBody(event, CreateTabSchema)
|
||||
await useDrizzle().insert(tables.tabs).values({
|
||||
name: body.name,
|
||||
icon: body.icon,
|
||||
color: body.color,
|
||||
nameVisible: body.nameVisible,
|
||||
categoryId: body.categoryId,
|
||||
link: body.link,
|
||||
})
|
||||
await useDrizzle().insert(tables.tabs).values(body)
|
||||
return { statusCode: 200 }
|
||||
}
|
||||
catch (err) {
|
||||
|
||||
19
server/api/users/[username].get.ts
Normal file
19
server/api/users/[username].get.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { username } = await getRouterParams(event)
|
||||
const user = await useDrizzle()
|
||||
.query
|
||||
.users
|
||||
.findFirst({
|
||||
where: eq(tables.users.username, username.toLowerCase()),
|
||||
with: {
|
||||
categories: {
|
||||
with: {
|
||||
tabs: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return user || {
|
||||
message: 'User not found',
|
||||
}
|
||||
})
|
||||
24
server/api/users/avatars/index.delete.ts
Normal file
24
server/api/users/avatars/index.delete.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { user, session } = await requireUserSession(event)
|
||||
|
||||
if (!user.avatar) {
|
||||
return sendNoContent(event, 204)
|
||||
}
|
||||
|
||||
await deleteProfilePicture(user.avatar)
|
||||
|
||||
const updatedUser = {
|
||||
...user,
|
||||
avatar: null,
|
||||
}
|
||||
await updateUser(user.id, { avatar: updatedUser.avatar })
|
||||
await replaceUserSession(event, {
|
||||
id: user.id,
|
||||
user: updatedUser,
|
||||
})
|
||||
|
||||
return {
|
||||
statusCode: 204,
|
||||
message: 'Avatar deleted',
|
||||
}
|
||||
})
|
||||
40
server/api/users/avatars/index.post.ts
Normal file
40
server/api/users/avatars/index.post.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { randomUUID } from 'uncrypto'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { user } = await requireUserSession(event)
|
||||
|
||||
const form = await readFormData(event)
|
||||
const file = form.get('file') as File
|
||||
|
||||
if (!file || !file.size) {
|
||||
throw createError({ statusCode: 400, message: 'No file provided' })
|
||||
}
|
||||
|
||||
ensureBlob(file, {
|
||||
maxSize: '1MB',
|
||||
types: ['image'],
|
||||
})
|
||||
|
||||
if (user.avatar) {
|
||||
await deleteProfilePicture(user.avatar)
|
||||
}
|
||||
|
||||
const filename = randomUUID()
|
||||
|
||||
const avatar = await hubBlob().put(filename, file, {
|
||||
addRandomSuffix: false,
|
||||
prefix: 'avatars/',
|
||||
})
|
||||
|
||||
const updatedUser = {
|
||||
...user,
|
||||
avatar: avatar.pathname,
|
||||
}
|
||||
await updateUser(user.id, { avatar: updatedUser.avatar })
|
||||
await replaceUserSession(event, {
|
||||
id: user.id,
|
||||
user: updatedUser,
|
||||
})
|
||||
|
||||
return sendNoContent(event, 204)
|
||||
})
|
||||
13
server/api/users/limits.get.ts
Normal file
13
server/api/users/limits.get.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = await requireUserSession(event)
|
||||
return useDrizzle().query.users.findFirst({
|
||||
where: eq(tables.users.id, user.id),
|
||||
with: {
|
||||
categories: {
|
||||
with: {
|
||||
tabs: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
17
server/api/users/me.put.ts
Normal file
17
server/api/users/me.put.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useValidatedBody } from 'h3-zod'
|
||||
import { UpdateUserSchema } from '~~/types/types'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { user } = await requireUserSession(event)
|
||||
const body = await useValidatedBody(event, UpdateUserSchema)
|
||||
|
||||
const updatedUser = {
|
||||
...user,
|
||||
...body,
|
||||
}
|
||||
|
||||
await updateUser(user.id, updatedUser)
|
||||
await replaceUserSession(event, updatedUser)
|
||||
|
||||
return sendNoContent(event, 204)
|
||||
})
|
||||
Reference in New Issue
Block a user