Add a get endpoint for icon marketplace icons, and also fixes
This commit is contained in:
13
src/index.ts
13
src/index.ts
@@ -28,6 +28,7 @@ import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/p
|
||||
import { handler as berrydashProfilePostsPostHandler } from './routes/berrydash/profile/posts/post'
|
||||
import { handler as berrydashProfilePostsPutHandler } from './routes/berrydash/profile/posts/put'
|
||||
|
||||
import { handler as berryDashIconMarketplaceGetHandler } from './routes/berrydash/icon-marketplace/get'
|
||||
import { handler as berryDashIconMarketplacePostHandler } from './routes/berrydash/icon-marketplace/post'
|
||||
import { handler as berryDashIconMarketplaceUploadPostHandler } from './routes/berrydash/icon-marketplace/upload/post'
|
||||
import { handler as berryDashIconMarketplaceIconGetHandler } from './routes/berrydash/icon-marketplace/icon/get'
|
||||
@@ -388,13 +389,23 @@ app.put(
|
||||
})
|
||||
}
|
||||
)
|
||||
app.get(
|
||||
'/berrydash/icon-marketplace',
|
||||
context => berryDashIconMarketplaceGetHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description: 'The endpoint for getting the icon marketplace icons.',
|
||||
tags: ['Berry Dash', 'Icon Marketplace']
|
||||
}
|
||||
}
|
||||
)
|
||||
app.post(
|
||||
'/berrydash/icon-marketplace',
|
||||
context => berryDashIconMarketplacePostHandler(context),
|
||||
{
|
||||
detail: {
|
||||
description:
|
||||
'The endpoint for getting the icon marketplace icons.\n\nPretty much none of the body types are correct.',
|
||||
'The endpoint for getting the icon marketplace icons with filters.\n\nPretty much none of the body types are correct.',
|
||||
tags: ['Berry Dash', 'Icon Marketplace']
|
||||
},
|
||||
body: t.Object({
|
||||
|
||||
58
src/routes/berrydash/icon-marketplace/get.ts
Normal file
58
src/routes/berrydash/icon-marketplace/get.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Context } from 'elysia'
|
||||
import { getDatabaseConnection, jsonResponse } from '../../../lib/util'
|
||||
import { berryDashMarketplaceIcons, users } from '../../../lib/tables'
|
||||
import { desc, eq, inArray } 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 icons = await db1
|
||||
.select({
|
||||
id: berryDashMarketplaceIcons.id,
|
||||
userId: berryDashMarketplaceIcons.userId,
|
||||
data: berryDashMarketplaceIcons.data,
|
||||
hash: berryDashMarketplaceIcons.hash,
|
||||
timestamp: berryDashMarketplaceIcons.timestamp,
|
||||
state: berryDashMarketplaceIcons.state,
|
||||
price: berryDashMarketplaceIcons.price,
|
||||
name: berryDashMarketplaceIcons.name
|
||||
})
|
||||
.from(berryDashMarketplaceIcons)
|
||||
.where(eq(berryDashMarketplaceIcons.state, 1))
|
||||
.orderBy(desc(berryDashMarketplaceIcons.place))
|
||||
.execute()
|
||||
|
||||
const userIds = Array.from(new Set(icons.map(i => i.userId)))
|
||||
const usersData = await db0
|
||||
.select({ id: users.id, username: users.username })
|
||||
.from(users)
|
||||
.where(inArray(users.id, userIds))
|
||||
.execute()
|
||||
|
||||
const usersMap = Object.fromEntries(usersData.map(u => [u.id, u.username]))
|
||||
|
||||
const result = icons.map(i => ({
|
||||
username: usersMap[i.userId] ?? 'Unknown',
|
||||
userId: i.userId,
|
||||
data: i.data,
|
||||
id: i.id,
|
||||
price: i.price,
|
||||
buyable: i.state == 1,
|
||||
name: atob(i.name)
|
||||
}))
|
||||
|
||||
connection0.end()
|
||||
connection1.end()
|
||||
|
||||
return jsonResponse({ success: true, message: null, data: result })
|
||||
}
|
||||
@@ -102,20 +102,7 @@ export async function handler (context: Context) {
|
||||
? null
|
||||
: dataQuery && dataQuery.toLowerCase() == 'false'
|
||||
? null
|
||||
: (() => {
|
||||
const q = Math.floor(icon[0].data.length / 4)
|
||||
const hq = Math.floor(icon[0].hash.length / 4)
|
||||
return (
|
||||
icon[0].data.slice(0, q) +
|
||||
icon[0].hash.slice(0, hq) +
|
||||
icon[0].data.slice(q, q * 2) +
|
||||
icon[0].hash.slice(hq, hq * 2) +
|
||||
icon[0].data.slice(q * 2, q * 3) +
|
||||
icon[0].hash.slice(hq * 2, hq * 3) +
|
||||
icon[0].data.slice(q * 3) +
|
||||
icon[0].hash.slice(hq * 3)
|
||||
)
|
||||
})(),
|
||||
: icon[0].data,
|
||||
id: icon[0].id,
|
||||
price: icon[0].price,
|
||||
buyable: icon[0].state == 1,
|
||||
@@ -145,23 +132,7 @@ export async function handler (context: Context) {
|
||||
const result = icons.map(i => ({
|
||||
username: usersMap[i.userId] ?? 'Unknown',
|
||||
userId: i.userId,
|
||||
data:
|
||||
dataQuery && dataQuery.toLowerCase() == 'false'
|
||||
? null
|
||||
: (() => {
|
||||
const q = Math.floor(i.data.length / 4)
|
||||
const hq = Math.floor(i.hash.length / 4)
|
||||
return (
|
||||
i.data.slice(0, q) +
|
||||
i.hash.slice(0, hq) +
|
||||
i.data.slice(q, q * 2) +
|
||||
i.hash.slice(hq, hq * 2) +
|
||||
i.data.slice(q * 2, q * 3) +
|
||||
i.hash.slice(hq * 2, hq * 3) +
|
||||
i.data.slice(q * 3) +
|
||||
i.hash.slice(hq * 3)
|
||||
)
|
||||
})(),
|
||||
data: dataQuery && dataQuery.toLowerCase() == 'false' ? null : i.data,
|
||||
id: i.id,
|
||||
price: i.price,
|
||||
buyable: i.state == 1,
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
jsonResponse
|
||||
} from '../../../lib/util'
|
||||
import { berryDashMarketplaceIcons, users } from '../../../lib/tables'
|
||||
import { and, eq, inArray, or, sql, not } from 'drizzle-orm'
|
||||
import { and, eq, inArray, or, sql, not, desc, asc } from 'drizzle-orm'
|
||||
import { checkAuthorization } from '../../../lib/bd/auth'
|
||||
|
||||
type Body = {
|
||||
@@ -81,12 +81,7 @@ export async function handler (context: Context) {
|
||||
body2.currentIcons = JSON.parse(atob(body.currentIcons))
|
||||
const body3: Body = body2 as Body
|
||||
|
||||
const filters: any[] = [
|
||||
or(
|
||||
eq(berryDashMarketplaceIcons.state, 1),
|
||||
eq(berryDashMarketplaceIcons.state, 2)
|
||||
)
|
||||
]
|
||||
const filters: any[] = [eq(berryDashMarketplaceIcons.state, 1)]
|
||||
|
||||
if (body3.priceRangeEnabled) {
|
||||
filters.push(
|
||||
@@ -120,16 +115,16 @@ export async function handler (context: Context) {
|
||||
let orderBy: any
|
||||
switch (body3.sortBy) {
|
||||
case 1:
|
||||
orderBy = sql`price ASC`
|
||||
orderBy = asc(berryDashMarketplaceIcons.price)
|
||||
break
|
||||
case 2:
|
||||
orderBy = sql`place ASC`
|
||||
orderBy = asc(berryDashMarketplaceIcons.place)
|
||||
break
|
||||
case 3:
|
||||
orderBy = sql`place DESC`
|
||||
orderBy = desc(berryDashMarketplaceIcons.place)
|
||||
break
|
||||
default:
|
||||
orderBy = sql`price DESC`
|
||||
orderBy = desc(berryDashMarketplaceIcons.price)
|
||||
break
|
||||
}
|
||||
|
||||
@@ -161,20 +156,7 @@ export async function handler (context: Context) {
|
||||
const result = icons.map(i => ({
|
||||
username: usersMap[i.userId] ?? 'Unknown',
|
||||
userId: i.userId,
|
||||
data: (() => {
|
||||
const q = Math.floor(i.data.length / 4)
|
||||
const hq = Math.floor(i.hash.length / 4)
|
||||
return (
|
||||
i.data.slice(0, q) +
|
||||
i.hash.slice(0, hq) +
|
||||
i.data.slice(q, q * 2) +
|
||||
i.hash.slice(hq, hq * 2) +
|
||||
i.data.slice(q * 2, q * 3) +
|
||||
i.hash.slice(hq * 2, hq * 3) +
|
||||
i.data.slice(q * 3) +
|
||||
i.hash.slice(hq * 3)
|
||||
)
|
||||
})(),
|
||||
data: i.data,
|
||||
id: i.id,
|
||||
price: i.price,
|
||||
buyable: i.state == 1,
|
||||
|
||||
Reference in New Issue
Block a user