Skip to content

Commit c55f9ee

Browse files
feat: add a ROM deletion menu
1 parent 29b1d9e commit c55f9ee

File tree

8 files changed

+236
-139
lines changed

8 files changed

+236
-139
lines changed

pnpm-lock.yaml

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/index.ts

+101-96
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { zValidator } from '@hono/zod-validator'
22
import { z } from 'zod'
3+
import { deleteRom } from '@/controllers/delete-rom.ts'
34
import { createRoms } from '../controllers/create-roms.ts'
45
import { createState } from '../controllers/create-state.ts'
56
import { getRomContent } from '../controllers/get-rom-content.ts'
@@ -8,101 +9,105 @@ import { getStates } from '../controllers/get-states.ts'
89
import { app } from './app.ts'
910
import { createFileResponse } from './utils.ts'
1011

11-
app
12-
.put(
13-
'rom/new',
14-
15-
zValidator(
16-
'form',
17-
z.object({
18-
'files[]': z.instanceof(File).array().max(10),
19-
platform: z.string(),
20-
}),
21-
),
22-
23-
async (c) => {
24-
const form = c.req.valid('form')
25-
const roms = await createRoms({ files: form['files[]'], platform: form.platform })
26-
return c.json(roms)
27-
},
28-
)
29-
30-
.get('rom/:id/content', async (c) => {
31-
const object = await getRomContent(c.req.param('id'))
32-
if (object) {
33-
return createFileResponse(object)
34-
}
35-
})
36-
37-
.get(
38-
'rom/:id/states',
39-
40-
zValidator(
41-
'query',
42-
z.object({
43-
type: z.enum(['auto', 'manual']).optional(),
44-
}),
45-
),
46-
47-
async (c) => {
48-
const query = c.req.valid('query')
49-
const rom = c.req.param('id')
50-
const states = await getStates({ rom, type: query.type })
51-
return c.json(states)
52-
},
53-
)
54-
55-
.get(
56-
'states',
57-
58-
zValidator(
59-
'query',
60-
z.object({
61-
rom: z.string(),
62-
type: z.enum(['manual', 'auto']),
63-
}),
64-
),
65-
66-
async (c) => {
67-
const query = c.req.valid('query')
68-
const states = await getStates({ rom: query.rom, type: query.type })
69-
return c.json(states)
70-
},
71-
)
72-
73-
.put(
74-
'state/new',
75-
76-
zValidator(
77-
'form',
78-
z.object({
79-
core: z.string(),
80-
rom: z.string(),
81-
state: z.instanceof(File),
82-
thumbnail: z.instanceof(File),
83-
type: z.enum(['auto', 'manual']),
84-
}),
85-
),
86-
87-
async (c) => {
88-
const form = c.req.valid('form')
89-
const state = await createState(form)
90-
return c.json(state)
91-
},
92-
)
93-
94-
.get('state/:id/content', async (c) => {
95-
const state = await getStateContent(c.req.param('id'))
96-
if (state) {
97-
return createFileResponse(state)
98-
}
99-
})
100-
101-
.get('state/:id/thumbnail', async (c) => {
102-
const file = await getStateContent(c.req.param('id'), 'thumbnail')
103-
if (file) {
104-
return createFileResponse(file)
105-
}
106-
})
12+
app.put(
13+
'rom/new',
14+
15+
zValidator(
16+
'form',
17+
z.object({
18+
'files[]': z.instanceof(File).array().max(10),
19+
platform: z.string(),
20+
}),
21+
),
22+
23+
async (c) => {
24+
const form = c.req.valid('form')
25+
const roms = await createRoms({ files: form['files[]'], platform: form.platform })
26+
return c.json(roms)
27+
},
28+
)
29+
30+
app.get('rom/:id/content', async (c) => {
31+
const object = await getRomContent(c.req.param('id'))
32+
if (object) {
33+
return createFileResponse(object)
34+
}
35+
})
36+
37+
app.get(
38+
'rom/:id/states',
39+
40+
zValidator(
41+
'query',
42+
z.object({
43+
type: z.enum(['auto', 'manual']).optional(),
44+
}),
45+
),
46+
47+
async (c) => {
48+
const query = c.req.valid('query')
49+
const rom = c.req.param('id')
50+
const states = await getStates({ rom, type: query.type })
51+
return c.json(states)
52+
},
53+
)
54+
55+
app.delete('rom/:id', async (c) => {
56+
await deleteRom(c.req.param('id'))
57+
return c.json(null)
58+
})
59+
60+
app.get(
61+
'states',
62+
63+
zValidator(
64+
'query',
65+
z.object({
66+
rom: z.string(),
67+
type: z.enum(['manual', 'auto']),
68+
}),
69+
),
70+
71+
async (c) => {
72+
const query = c.req.valid('query')
73+
const states = await getStates({ rom: query.rom, type: query.type })
74+
return c.json(states)
75+
},
76+
)
77+
78+
app.put(
79+
'state/new',
80+
81+
zValidator(
82+
'form',
83+
z.object({
84+
core: z.string(),
85+
rom: z.string(),
86+
state: z.instanceof(File),
87+
thumbnail: z.instanceof(File),
88+
type: z.enum(['auto', 'manual']),
89+
}),
90+
),
91+
92+
async (c) => {
93+
const form = c.req.valid('form')
94+
const state = await createState(form)
95+
return c.json(state)
96+
},
97+
)
98+
99+
app.get('state/:id/content', async (c) => {
100+
const state = await getStateContent(c.req.param('id'))
101+
if (state) {
102+
return createFileResponse(state)
103+
}
104+
})
105+
106+
app.get('state/:id/thumbnail', async (c) => {
107+
const file = await getStateContent(c.req.param('id'), 'thumbnail')
108+
if (file) {
109+
return createFileResponse(file)
110+
}
111+
})
107112

108113
export { app as api }

src/controllers/delete-rom.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { and, eq } from 'drizzle-orm'
2+
import { getContextData } from 'waku/middleware/context'
3+
import { romTable } from '../databases/library/schema.ts'
4+
5+
export async function deleteRom(id: string) {
6+
const { currentUser, db } = getContextData()
7+
const { library } = db
8+
9+
await library
10+
.update(romTable)
11+
.set({ status: 0 })
12+
.where(and(eq(romTable.id, id), eq(romTable.user_id, currentUser.id)))
13+
.returning()
14+
}

src/pages/library/atoms.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { atom } from 'jotai'
22
import { defaultPreference, type Preference } from '@/constants/preference.ts'
3+
import type { getRoms } from '@/controllers/get-roms'
34

45
export const serverDataAtom = atom<{ [key: string]: any; preference: Preference }>({
56
preference: defaultPreference,
67
})
8+
9+
export const romsAtom = atom<Awaited<ReturnType<typeof getRoms>>>([])

0 commit comments

Comments
 (0)