From 566d704ba10663ba7a4845cc39a27db716ade59b Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Mon, 19 Jan 2026 21:09:33 -0700 Subject: [PATCH] Profile like/dislike endpoint --- src/index.ts | 3 + src/routes/berrydash/profile/posts/put.ts | 111 ++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/routes/berrydash/profile/posts/put.ts diff --git a/src/index.ts b/src/index.ts index 74d5551..df11801 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ import { handler as berrydashProfileGetHandler } from './routes/berrydash/profil import { handler as berrydashProfilePostsDeleteHandler } from './routes/berrydash/profile/posts/delete' import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get' import { handler as berrydashProfilePostsPostHandler } from './routes/berrydash/profile/posts/post' +import { handler as berrydashProfilePostsPutHandler } from './routes/berrydash/profile/posts/put' dotenv.config() @@ -55,6 +56,8 @@ app.get('/berrydash/profile/posts', context => app.post('/berrydash/profile/posts', context => berrydashProfilePostsPostHandler(context) ) +app.put('/berrydash/profile/posts', context => + berrydashProfilePostsPutHandler(context) ) app.all('*', () => jsonResponse( diff --git a/src/routes/berrydash/profile/posts/put.ts b/src/routes/berrydash/profile/posts/put.ts new file mode 100644 index 0000000..8ff77e8 --- /dev/null +++ b/src/routes/berrydash/profile/posts/put.ts @@ -0,0 +1,111 @@ +import { Context } from 'elysia' +import { getDatabaseConnection, jsonResponse } from '../../../../lib/util' +import { berryDashUserData, berryDashUserPosts } from '../../../../lib/tables' +import { and, eq } from 'drizzle-orm' + +type Body = { + liked: string +} + +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: connection1, db: db1 } = dbInfo1 + + let authorizationToken = context.headers.authorization + let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0 + let likedQuery = context.query.liked as string + if (!idQuery || idQuery < 1) { + connection1.end() + return jsonResponse( + { success: false, message: 'No valid post ID provided', data: null }, + 400 + ) + } + if ( + !likedQuery || + (likedQuery.toLowerCase() != 'true' && likedQuery.toLowerCase() != 'false') + ) { + connection1.end() + return jsonResponse( + { + success: false, + message: 'Missing or invalid liked value', + data: null + }, + 400 + ) + } + if (!authorizationToken) { + connection1.end() + return jsonResponse( + { success: false, message: 'Unauthorized', data: null }, + 401 + ) + } + + const userData = await db1 + .select({ id: berryDashUserData.id }) + .from(berryDashUserData) + .where(eq(berryDashUserData.token, authorizationToken)) + .execute() + + if (!userData[0]) { + connection1.end() + return jsonResponse( + { success: false, message: 'Unauthorized', data: null }, + 401 + ) + } + + const votesResult = await db1 + .select({ votes: berryDashUserPosts.votes }) + .from(berryDashUserPosts) + .where( + and( + eq(berryDashUserPosts.id, idQuery), + eq(berryDashUserPosts.deletedAt, 0) + ) + ) + .limit(1) + .execute() + if (!votesResult[0]) + return jsonResponse( + { + success: true, + message: 'Unable to get information on post', + data: null + }, + 400 + ) + const votes = JSON.parse(votesResult[0].votes) + if (votes[userData[0].id.toString()]) { + let likes = 0 + for (const vote of Object.values(votes) as boolean[]) likes += vote ? 1 : -1 + return jsonResponse({ success: true, message: null, data: { likes } }, 200) + } + votes[userData[0].id.toString()] = likedQuery.toLowerCase() == 'true' + + await db1 + .update(berryDashUserPosts) + .set({ votes: JSON.stringify(votes) }) + .where( + and( + eq(berryDashUserPosts.id, idQuery), + eq(berryDashUserPosts.deletedAt, 0) + ) + ) + .execute() + + connection1.end() + + let likes = 0 + for (const vote of Object.values(votes) as boolean[]) likes += vote ? 1 : -1 + return jsonResponse({ success: true, message: null, data: { likes } }, 200) +}