Add post delete endpoint
This commit is contained in:
@@ -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 launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data'
|
||||||
import { handler as berrydashLeaderboardsHandler } from './routes/berrydash/leaderboards'
|
import { handler as berrydashLeaderboardsHandler } from './routes/berrydash/leaderboards'
|
||||||
import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get'
|
import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get'
|
||||||
|
import { handler as berrydashProfilePostsDeleteHandler } from './routes/berrydash/profile/posts/delete'
|
||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
@@ -45,6 +46,9 @@ app.get('/berrydash/leaderboards/total', context =>
|
|||||||
app.get('/berrydash/profile/posts', context =>
|
app.get('/berrydash/profile/posts', context =>
|
||||||
berrydashProfilePostsGetHandler(context)
|
berrydashProfilePostsGetHandler(context)
|
||||||
)
|
)
|
||||||
|
app.delete('/berrydash/profile/posts', context =>
|
||||||
|
berrydashProfilePostsDeleteHandler(context)
|
||||||
|
)
|
||||||
app.all('*', () =>
|
app.all('*', () =>
|
||||||
jsonResponse(
|
jsonResponse(
|
||||||
{
|
{
|
||||||
|
|||||||
79
src/routes/berrydash/profile/posts/delete.ts
Normal file
79
src/routes/berrydash/profile/posts/delete.ts
Normal 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)
|
||||||
|
}
|
||||||
@@ -19,8 +19,10 @@ export async function handler (context: Context) {
|
|||||||
const { connection: connection0, db: db0 } = dbInfo0
|
const { connection: connection0, db: db0 } = dbInfo0
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0
|
let userIdQuery = context.query.userId
|
||||||
if (!idQuery || idQuery < 1) {
|
? parseInt(context.query.userId, 10)
|
||||||
|
: 0
|
||||||
|
if (!userIdQuery || userIdQuery < 1) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
@@ -32,7 +34,7 @@ export async function handler (context: Context) {
|
|||||||
const user = await db0
|
const user = await db0
|
||||||
.select({ id: users.id })
|
.select({ id: users.id })
|
||||||
.from(users)
|
.from(users)
|
||||||
.where(eq(users.id, idQuery))
|
.where(eq(users.id, userIdQuery))
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
if (!user[0]) {
|
if (!user[0]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user