Add a endpoint to fetch users
This commit is contained in:
118
src/index.ts
118
src/index.ts
@@ -17,10 +17,10 @@ import { handler as launcherLatestHandler } from './routes/launcher/latest'
|
|||||||
import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader/latest'
|
import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader/latest'
|
||||||
import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data'
|
import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data'
|
||||||
|
|
||||||
import { handler as berryDashAccountLoginPostHandler } from './routes/account/login/post'
|
import { handler as accountLoginPostHandler } from './routes/account/login/post'
|
||||||
import { handler as berryDashAccountRegisterPostHandler } from './routes/account/register/post'
|
import { handler as accountRegisterPostHandler } from './routes/account/register/post'
|
||||||
import { handler as berryDashAccountChangeUsernamePostHandler } from './routes/account/change-username/post'
|
import { handler as accountChangeUsernamePostHandler } from './routes/account/change-username/post'
|
||||||
import { handler as berryDashAccountChangePasswordPostHandler } from './routes/account/change-password/post'
|
import { handler as accountChangePasswordPostHandler } from './routes/account/change-password/post'
|
||||||
import { handler as accountForgotUsernamePostHandler } from './routes/account/forgot-username/post'
|
import { handler as accountForgotUsernamePostHandler } from './routes/account/forgot-username/post'
|
||||||
import { handler as accountForgotPasswordPostHandler } from './routes/account/forgot-password/post'
|
import { handler as accountForgotPasswordPostHandler } from './routes/account/forgot-password/post'
|
||||||
import { handler as accountResetPasswordPostHandler } from './routes/account/reset-password/post'
|
import { handler as accountResetPasswordPostHandler } from './routes/account/reset-password/post'
|
||||||
@@ -40,6 +40,7 @@ import { handler as berryDashIconMarketplacePostHandler } from './routes/berryda
|
|||||||
import { handler as berryDashIconMarketplaceUploadPostHandler } from './routes/berrydash/icon-marketplace/upload/post'
|
import { handler as berryDashIconMarketplaceUploadPostHandler } from './routes/berrydash/icon-marketplace/upload/post'
|
||||||
import { handler as berryDashIconMarketplaceIconGetHandler } from './routes/berrydash/icon-marketplace/icon/get'
|
import { handler as berryDashIconMarketplaceIconGetHandler } from './routes/berrydash/icon-marketplace/icon/get'
|
||||||
|
|
||||||
|
import { handler as berryDashAccountGetHandler } from './routes/berrydash/account/get'
|
||||||
import { handler as berryDashAccountSaveGetHandler } from './routes/berrydash/account/save/get'
|
import { handler as berryDashAccountSaveGetHandler } from './routes/berrydash/account/save/get'
|
||||||
import { handler as berryDashAccountSavePostHandler } from './routes/berrydash/account/save/post'
|
import { handler as berryDashAccountSavePostHandler } from './routes/berrydash/account/save/post'
|
||||||
|
|
||||||
@@ -589,57 +590,49 @@ app.get(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
app.post(
|
app.post('/account/login', context => accountLoginPostHandler(context), {
|
||||||
'/account/login',
|
detail: {
|
||||||
context => berryDashAccountLoginPostHandler(context),
|
description:
|
||||||
{
|
'The endpoint for logging into an account. This is also the endpoint for refreshing login.',
|
||||||
detail: {
|
tags: ['Accounts']
|
||||||
description:
|
},
|
||||||
'The endpoint for logging into an account. This is also the endpoint for refreshing login.',
|
body: t.Object({
|
||||||
tags: ['Accounts']
|
username: t.String(),
|
||||||
},
|
password: t.String()
|
||||||
body: t.Object({
|
}),
|
||||||
username: t.String(),
|
headers: t.Object({
|
||||||
password: t.String()
|
'x-forwarded-for': t.Optional(
|
||||||
}),
|
t.String({
|
||||||
headers: t.Object({
|
description:
|
||||||
'x-forwarded-for': t.Optional(
|
'Ignore this header. It cannot be set or overridden and is required for endpoints to work properly'
|
||||||
t.String({
|
})
|
||||||
description:
|
)
|
||||||
'Ignore this header. It cannot be set or overridden and is required for endpoints to work properly'
|
})
|
||||||
})
|
})
|
||||||
)
|
app.post('/account/register', context => accountRegisterPostHandler(context), {
|
||||||
})
|
detail: {
|
||||||
}
|
description: 'The endpoint for registering an account.',
|
||||||
)
|
tags: ['Accounts']
|
||||||
app.post(
|
},
|
||||||
'/account/register',
|
body: t.Object({
|
||||||
context => berryDashAccountRegisterPostHandler(context),
|
token: t.Optional(t.String()),
|
||||||
{
|
verifyCode: t.Optional(t.String()),
|
||||||
detail: {
|
username: t.String(),
|
||||||
description: 'The endpoint for registering an account.',
|
password: t.String(),
|
||||||
tags: ['Accounts']
|
email: t.String()
|
||||||
},
|
}),
|
||||||
body: t.Object({
|
headers: t.Object({
|
||||||
token: t.Optional(t.String()),
|
'x-forwarded-for': t.Optional(
|
||||||
verifyCode: t.Optional(t.String()),
|
t.String({
|
||||||
username: t.String(),
|
description:
|
||||||
password: t.String(),
|
'Ignore this header. It cannot be set or overridden and is required for endpoints to work properly'
|
||||||
email: t.String()
|
})
|
||||||
}),
|
)
|
||||||
headers: t.Object({
|
})
|
||||||
'x-forwarded-for': t.Optional(
|
})
|
||||||
t.String({
|
|
||||||
description:
|
|
||||||
'Ignore this header. It cannot be set or overridden and is required for endpoints to work properly'
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
)
|
|
||||||
app.post(
|
app.post(
|
||||||
'/account/change-username',
|
'/account/change-username',
|
||||||
context => berryDashAccountChangeUsernamePostHandler(context),
|
context => accountChangeUsernamePostHandler(context),
|
||||||
{
|
{
|
||||||
detail: {
|
detail: {
|
||||||
description: "The endpoint for changing the account's user name.",
|
description: "The endpoint for changing the account's user name.",
|
||||||
@@ -663,7 +656,7 @@ app.post(
|
|||||||
)
|
)
|
||||||
app.post(
|
app.post(
|
||||||
'/account/change-password',
|
'/account/change-password',
|
||||||
context => berryDashAccountChangePasswordPostHandler(context),
|
context => accountChangePasswordPostHandler(context),
|
||||||
{
|
{
|
||||||
detail: {
|
detail: {
|
||||||
description: "The endpoint for changing the account's password.",
|
description: "The endpoint for changing the account's password.",
|
||||||
@@ -1214,6 +1207,23 @@ app.post(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
app.get('/berrydash/account', context => berryDashAccountGetHandler(context), {
|
||||||
|
detail: {
|
||||||
|
description: 'The endpoint for getting a list of users',
|
||||||
|
tags: ['Berry Dash', 'Accounts']
|
||||||
|
},
|
||||||
|
query: t.Object({
|
||||||
|
username: t.String()
|
||||||
|
}),
|
||||||
|
headers: t.Object({
|
||||||
|
'x-forwarded-for': t.Optional(
|
||||||
|
t.String({
|
||||||
|
description:
|
||||||
|
'Ignore this header. It cannot be set or overridden and is required for endpoints to work properly'
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
app.get(
|
app.get(
|
||||||
'/berrydash/account/save',
|
'/berrydash/account/save',
|
||||||
context => berryDashAccountSaveGetHandler(context),
|
context => berryDashAccountSaveGetHandler(context),
|
||||||
|
|||||||
73
src/routes/berrydash/account/get.ts
Normal file
73
src/routes/berrydash/account/get.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { Context } from 'elysia'
|
||||||
|
import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
||||||
|
import { berryDashUserData, users } from '../../../lib/tables'
|
||||||
|
import { eq, sql } 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
|
||||||
|
|
||||||
|
const usernameToSearch = context.query.username
|
||||||
|
|
||||||
|
const userRows = await db0
|
||||||
|
.select({ id: users.id, username: users.username })
|
||||||
|
.from(users)
|
||||||
|
.where(
|
||||||
|
sql`LOWER(${
|
||||||
|
users.username
|
||||||
|
}) LIKE ${`%${usernameToSearch.toLowerCase()}%`}`
|
||||||
|
)
|
||||||
|
.execute()
|
||||||
|
|
||||||
|
const result = await Promise.all(
|
||||||
|
userRows.map(async (row: any) => {
|
||||||
|
const userData = await db1
|
||||||
|
.select({ saveData: berryDashUserData.saveData })
|
||||||
|
.from(berryDashUserData)
|
||||||
|
.where(eq(berryDashUserData.id, row.id))
|
||||||
|
.execute()
|
||||||
|
const savedata = JSON.parse(userData[0].saveData)
|
||||||
|
row.icon = savedata?.bird?.icon ?? 1
|
||||||
|
row.overlay = savedata?.bird?.overlay ?? 0
|
||||||
|
row.birdColor = savedata?.settings?.colors?.icon ?? [255, 255, 255]
|
||||||
|
row.overlayColor = savedata?.settings?.colors?.overlay ?? [255, 255, 255]
|
||||||
|
row.customIcon = savedata?.bird?.customIcon?.selected ?? null
|
||||||
|
row.stats = {
|
||||||
|
highScore: parseInt(savedata?.gameStore?.highScore ?? 0),
|
||||||
|
totalNormalBerries: parseInt(
|
||||||
|
savedata?.gameStore?.totalNormalBerries ?? 0
|
||||||
|
),
|
||||||
|
totalPoisonBerries: parseInt(
|
||||||
|
savedata?.gameStore?.totalPoisonBerries ?? 0
|
||||||
|
),
|
||||||
|
totalSlowBerries: parseInt(savedata?.gameStore?.totalSlowBerries ?? 0),
|
||||||
|
totalUltraBerries: parseInt(
|
||||||
|
savedata?.gameStore?.totalUltraBerries ?? 0
|
||||||
|
),
|
||||||
|
totalSpeedyBerries: parseInt(
|
||||||
|
savedata?.gameStore?.totalSpeedyBerries ?? 0
|
||||||
|
),
|
||||||
|
totalCoinBerries: parseInt(savedata?.gameStore?.totalCoinBerries ?? 0),
|
||||||
|
totalRandomBerries: parseInt(
|
||||||
|
savedata?.gameStore?.totalRandomBerries ?? 0
|
||||||
|
),
|
||||||
|
totalAntiBerries: parseInt(savedata?.gameStore?.totalAntiBerries ?? 0),
|
||||||
|
coins: savedata?.bird?.customIcon?.balance ?? 0
|
||||||
|
}
|
||||||
|
return row
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
connection0.end()
|
||||||
|
connection1.end()
|
||||||
|
|
||||||
|
return jsonResponse({ success: true, message: null, data: result }, 200)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user