From 0b579800d5f173e96e58debcfe50f480903cb38a Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Fri, 16 Jan 2026 15:45:56 -0700 Subject: [PATCH] Add post delete endpoint --- src/index.ts | 4 + src/routes/berrydash/profile/posts/delete.ts | 79 ++++++++++++++++++++ src/routes/berrydash/profile/posts/get.ts | 8 +- 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/routes/berrydash/profile/posts/delete.ts diff --git a/src/index.ts b/src/index.ts index 01f1469..7f3c631 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data' import { handler as berrydashLeaderboardsHandler } from './routes/berrydash/leaderboards' import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get' +import { handler as berrydashProfilePostsDeleteHandler } from './routes/berrydash/profile/posts/delete' dotenv.config() @@ -45,6 +46,9 @@ app.get('/berrydash/leaderboards/total', context => app.get('/berrydash/profile/posts', context => berrydashProfilePostsGetHandler(context) ) +app.delete('/berrydash/profile/posts', context => + berrydashProfilePostsDeleteHandler(context) +) app.all('*', () => jsonResponse( { diff --git a/src/routes/berrydash/profile/posts/delete.ts b/src/routes/berrydash/profile/posts/delete.ts new file mode 100644 index 0000000..54420fe --- /dev/null +++ b/src/routes/berrydash/profile/posts/delete.ts @@ -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) +} diff --git a/src/routes/berrydash/profile/posts/get.ts b/src/routes/berrydash/profile/posts/get.ts index 9e8e907..568fdbb 100644 --- a/src/routes/berrydash/profile/posts/get.ts +++ b/src/routes/berrydash/profile/posts/get.ts @@ -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]) {