-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathflashcards.ts
More file actions
executable file
·42 lines (39 loc) · 1.47 KB
/
flashcards.ts
File metadata and controls
executable file
·42 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import crypto from 'crypto'
import db from '../../utils/database/keyv'
export function flashcardRoutes(app: any) {
app.post('/flashcards', async (req: any, res: any) => {
try {
const { question, answer, tag } = req.body
if (!question || !answer || !tag) return res.status(400).send({ error: 'question, answer, tag required' })
const id = crypto.randomUUID()
const card = { id, question, answer, tag, created: Date.now() }
let cards = await db.get('flashcards') || []
cards.push(card)
await db.set(`flashcard:${id}`, card)
await db.set('flashcards', cards)
res.send({ ok: true, flashcard: card })
} catch (e: any) {
res.status(500).send({ ok: false, error: e?.message || 'failed' })
}
})
app.get('/flashcards', async (_: any, res: any) => {
try {
res.send({ ok: true, flashcards: await db.get('flashcards') || [] })
} catch (e: any) {
res.status(500).send({ ok: false, error: e?.message || 'failed' })
}
})
app.delete('/flashcards/:id', async (req: any, res: any) => {
try {
const id = req.params.id
if (!id) return res.status(400).send({ error: 'id required' })
await db.delete(`flashcard:${id}`)
let cards = await db.get('flashcards') || []
cards = cards.filter((c: any) => c.id !== id)
await db.set('flashcards', cards)
res.send({ ok: true })
} catch (e: any) {
res.status(500).send({ ok: false, error: e?.message || 'failed' })
}
})
}