refactor: remove communityPresets schema, API, and routes

This commit is contained in:
2026-04-27 14:29:30 +08:00
parent 5100888216
commit 07467a4812
9 changed files with 1677 additions and 211 deletions
-5
View File
@@ -4,11 +4,6 @@ export { gamesRoutes, gameVersionsRoutes } from "./games"
export { hardwareRoutes } from "./hardware"
export { performanceRoutes, performanceVerifyRoutes } from "./performance"
export { performanceSubmitRoutes } from "./performance-submit"
export {
presetsRoutes,
presetUpvoteRoutes,
presetSettingsRoutes,
} from "./presets"
export { commentsRoutes } from "./comments"
export { gameStatsRoutes } from "./game-stats"
export { hardwareStatsRoutes } from "./hardware-stats"
-143
View File
@@ -1,143 +0,0 @@
import { Elysia, t } from "elysia"
import { createCrudRoutes } from "./crud-builder"
import { communityPresets } from "@/lib/db/schema"
import { db } from "@/lib/db/index"
import { eq, sql } from "drizzle-orm"
import { requireRole } from "@/lib/auth/guard"
// ── Community Presets CRUD ────────────────────────────────────────
export const presetsRoutes = createCrudRoutes(communityPresets, {
prefix: "/presets",
name: "Preset",
auth: { read: "public", write: "user", delete: "admin" },
search: { fields: ["name", "description"] },
filter: { fields: ["gameId", "hardwareSlug"] },
paramName: "presetId",
})
// ── Upvote endpoint ───────────────────────────────────────────────
export const presetUpvoteRoutes = new Elysia({ prefix: "/presets" }).post(
"/:presetId/upvote",
async ({ params, request, set }) => {
const guard = await requireRole(request.headers, [
"user",
"contributor",
"admin",
])
if (!guard.ok) {
set.status = guard.status
return { error: guard.error }
}
const [preset] = await db
.select()
.from(communityPresets)
.where(eq(communityPresets.id, params.presetId))
.limit(1)
if (!preset) {
set.status = 404
return { error: "Preset not found" }
}
const [updated] = await db
.update(communityPresets)
.set({
upvotes: sql`${communityPresets.upvotes} + 1`,
updatedAt: new Date(),
})
.where(eq(communityPresets.id, params.presetId))
.returning()
return updated
},
{
params: t.Object({ presetId: t.String() }),
},
)
// ── Preset Settings (nested under /presets/:presetId/settings) ────
// Settings are stored as freeform JSON on the preset itself.
export const presetSettingsRoutes = new Elysia({
prefix: "/presets/:presetId/settings",
})
// GET the settings JSON for a preset
.get(
"/",
async ({ params, set }) => {
const [preset] = await db
.select({ settingsJson: communityPresets.settingsJson })
.from(communityPresets)
.where(eq(communityPresets.id, params.presetId))
.limit(1)
if (!preset) {
set.status = 404
return { error: "Preset not found" }
}
return preset.settingsJson ?? []
},
{
params: t.Object({ presetId: t.String() }),
},
)
// PUT (replace) the settings JSON for a preset
.put(
"/",
async ({ params, body, request, set }) => {
const guard = await requireRole(request.headers, [
"user",
"contributor",
"admin",
])
if (!guard.ok) {
set.status = guard.status
return { error: guard.error }
}
// Verify preset exists and user is the creator (or admin)
const [preset] = await db
.select()
.from(communityPresets)
.where(eq(communityPresets.id, params.presetId))
.limit(1)
if (!preset) {
set.status = 404
return { error: "Preset not found" }
}
if (preset.createdBy !== guard.user.id && guard.user.role !== "admin") {
set.status = 403
return { error: "Not authorized to modify this preset" }
}
const [updated] = await db
.update(communityPresets)
.set({
settingsJson: body.settings,
updatedAt: new Date(),
})
.where(eq(communityPresets.id, params.presetId))
.returning()
return updated
},
{
params: t.Object({ presetId: t.String() }),
body: t.Object({
settings: t.Array(
t.Object({
category: t.String(),
settings: t.Array(
t.Object({
title: t.String(),
value: t.Union([t.String(), t.Number(), t.Boolean()]),
}),
),
}),
),
}),
},
)
-53
View File
@@ -1,53 +0,0 @@
import {
integer,
jsonb,
pgTable,
text,
timestamp,
unique,
} from "drizzle-orm/pg-core"
import { games } from "./games"
import { hardware } from "./hardware"
import { performanceEntries } from "./performanceEntries"
import { user } from "./auth"
export const communityPresets = pgTable(
"community_presets",
{
id: text("id")
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
gameId: text("game_id")
.notNull()
.references(() => games.id, { onDelete: "cascade" }),
hardwareSlug: text("hardware_slug")
.notNull()
.references(() => hardware.slug, { onDelete: "restrict" }),
name: text("name").notNull(),
description: text("description"),
createdBy: text("created_by").references(() => user.id, {
onDelete: "set null",
}),
upvotes: integer("upvotes").default(0).notNull(),
performanceEntryId: text("performance_entry_id").references(
() => performanceEntries.id,
{ onDelete: "set null" },
),
// Freeform settings JSON — same flexible structure as performanceEntries.settingsJson
settingsJson: jsonb("settings_json").$type<
{
category: string
settings: { title: string; value: string | number | boolean }[]
}[]
>(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").defaultNow().notNull(),
},
(table) => [
unique("preset_game_hardware_name_unique").on(
table.gameId,
table.hardwareSlug,
table.name,
),
],
)
-1
View File
@@ -5,6 +5,5 @@ export * from "./gameVersions"
export * from "./hardware"
export * from "./performanceEntries"
export * from "./gamePlatformSupport"
export * from "./communityPresets"
export * from "./gameComments"
export * from "./savedGames"