Add post delete endpoint

This commit is contained in:
2026-01-16 15:45:56 -07:00
parent 5589af810d
commit 0b579800d5
3 changed files with 88 additions and 3 deletions

View File

@@ -0,0 +1,79 @@
import { Context } from 'elysia'
import {
genTimestamp,
getDatabaseConnection,
jsonResponse
} from '../../../../lib/util'
import {
berryDashUserData,
berryDashUserPosts,
users
} from '../../../../lib/tables'
import { and, desc, eq } from 'drizzle-orm'
export async function handler (context: Context) {
const dbInfo0 = getDatabaseConnection(0)
const dbInfo1 = getDatabaseConnection(1)
if (!dbInfo0 || !dbInfo1)
return jsonResponse(
{ success: false, message: 'Failed to connect to database', data: null },
500
)
const { connection: connection0, db: db0 } = dbInfo0
const { connection: connection1, db: db1 } = dbInfo1
let authorizationToken = context.headers.authorization
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
if (!idQuery || idQuery < 1) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'No valid post ID provided', data: null },
400
)
}
if (!authorizationToken) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'Unauthorized', data: null },
401
)
}
const userData = await db1
.select({ id: users.id })
.from(berryDashUserData)
.where(eq(berryDashUserData.token, authorizationToken as string))
.execute()
if (!userData[0]) {
connection0.end()
connection1.end()
return jsonResponse(
{ success: false, message: 'Unauthorized', data: null },
401
)
}
const result = await db1
.update(berryDashUserPosts)
.set({ deletedAt: Math.floor(Date.now() / 1000) })
.where(
and(
eq(berryDashUserPosts.id, idQuery),
eq(berryDashUserPosts.userId, userData[0].id),
eq(berryDashUserPosts.deletedAt, 0)
)
)
.execute()
connection0.end()
connection1.end()
if (result[0])
return jsonResponse({ success: true, message: 'Success', data: null }, 200)
else
return jsonResponse({ success: false, message: 'Failed', data: null }, 400)
}

View File

@@ -19,8 +19,10 @@ export async function handler (context: Context) {
const { connection: connection0, db: db0 } = dbInfo0
const { connection: connection1, db: db1 } = dbInfo1
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
if (!idQuery || idQuery < 1) {
let userIdQuery = context.query.userId
? parseInt(context.query.userId, 10)
: 0
if (!userIdQuery || userIdQuery < 1) {
connection0.end()
connection1.end()
return jsonResponse(
@@ -32,7 +34,7 @@ export async function handler (context: Context) {
const user = await db0
.select({ id: users.id })
.from(users)
.where(eq(users.id, idQuery))
.where(eq(users.id, userIdQuery))
.execute()
if (!user[0]) {