Skip to content

Commit d7482ee

Browse files
feat: add launch record apis
1 parent df6d81b commit d7482ee

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/api/index.ts

+39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { zValidator } from '@hono/zod-validator'
22
import { z } from 'zod'
3+
import { createLaunchRecord } from '@/controllers/create-launch-record.ts'
34
import { deleteRom } from '@/controllers/delete-rom.ts'
5+
import { getLaunchRecords } from '@/controllers/get-launch-records.ts'
46
import { createRoms } from '../controllers/create-roms.ts'
57
import { createState } from '../controllers/create-state.ts'
68
import { getRomContent } from '../controllers/get-rom-content.ts'
@@ -110,4 +112,41 @@ app.get('state/:id/thumbnail', async (c) => {
110112
}
111113
})
112114

115+
app.post(
116+
'launch_record/new',
117+
118+
zValidator(
119+
'form',
120+
z.object({
121+
core: z.string(),
122+
platform: z.string(),
123+
rom: z.string(),
124+
}),
125+
),
126+
127+
async (c) => {
128+
const form = c.req.valid('form')
129+
await createLaunchRecord(form)
130+
return c.json(null)
131+
},
132+
)
133+
134+
app.get(
135+
'launch_records',
136+
137+
zValidator(
138+
'query',
139+
z.object({
140+
page: z.number().default(1),
141+
page_size: z.number().default(50),
142+
}),
143+
),
144+
145+
async (c) => {
146+
const query = c.req.valid('query')
147+
const result = await getLaunchRecords(query)
148+
return c.json(result)
149+
},
150+
)
151+
113152
export { app as api }
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { getContextData } from 'waku/middleware/context'
2+
import { launchRecordTable } from '../databases/library/schema.ts'
3+
4+
interface CreateRomParams {
5+
core: string
6+
platform: string
7+
rom: string
8+
}
9+
10+
export async function createLaunchRecord(params: CreateRomParams) {
11+
const { currentUser, db } = getContextData()
12+
const { library } = db
13+
14+
const [result] = await library
15+
.insert(launchRecordTable)
16+
.values({
17+
core: params.core,
18+
platform: params.platform,
19+
rom_id: params.rom,
20+
user_id: currentUser.id,
21+
})
22+
.returning()
23+
24+
return result
25+
}

src/controllers/get-launch-records.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { eq, max } from 'drizzle-orm'
2+
import { getContextData } from 'waku/middleware/context'
3+
import { launchRecordTable } from '../databases/library/schema.ts'
4+
5+
export async function getLaunchRecords({ page = 1, pageSize = 50 }: { page?: number; pageSize?: number }) {
6+
const { currentUser, db } = getContextData()
7+
const { library } = db
8+
9+
const offset = (page - 1) * pageSize
10+
const results = await library
11+
.select({
12+
core: launchRecordTable.core,
13+
lastLaunched: max(launchRecordTable.created_at),
14+
romId: launchRecordTable.rom_id,
15+
userId: launchRecordTable.user_id,
16+
})
17+
.from(launchRecordTable)
18+
.where(eq(launchRecordTable.user_id, currentUser.id))
19+
.groupBy(launchRecordTable.rom_id)
20+
.offset(offset)
21+
.limit(pageSize)
22+
23+
return results
24+
}

0 commit comments

Comments
 (0)