This commit is contained in:
Rene Kievits
2025-10-19 22:52:32 +02:00
commit a728add8af
64 changed files with 11693 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import express, { type Router, type Request, type Response } from 'express'
import { ApiKeyModel } from '../../../models/apikey.model.ts'
const router = express.Router()
router.get('/', async (_req: Request, res: Response) => {
try {
const doc = await ApiKeyModel.findOne({})
const apiKey = doc?.apiKey || ''
res.json({ apiKey })
} catch (error) {
console.error('Error fetching API key:', error)
res.status(500).json({ error: 'Failed to fetch API key' })
}
})
router.post('/', async (req: Request, res: Response) => {
try {
const { apiKey } = req.body as { apiKey?: string }
if (!apiKey || !apiKey.trim()) {
return res.status(400).json({ error: 'Invalid API key' })
}
await ApiKeyModel.updateOne(
{},
{ $set: { apiKey: apiKey } },
{ upsert: true }
)
res.json({ success: true })
} catch (error) {
console.error('Error saving API key:', error)
res.status(500).json({ error: 'Failed to save API key' })
}
})
router.delete('/', async (_req: Request, res: Response) => {
try {
const result = await ApiKeyModel.deleteOne({})
if (result.deletedCount === 0) {
console.log('No API key found to delete.')
return res.status(204).end()
}
console.log('API key document deleted.')
res.json({ success: true, message: 'API key deleted' })
} catch (error) {
console.error('Error deleting API key:', error)
res.status(500).json({ error: 'Failed to delete API key' })
}
})
export default router as Router

View File

@@ -0,0 +1,18 @@
import express, { type Router, type Request, type Response } from 'express'
import { KanjiModel } from '../../../models/kanji.model.ts'
const router = express.Router()
router.get('/', async (_req: Request, res: Response) => {
try {
const doc = await KanjiModel.find()
console.log(doc)
res.json(doc)
} catch (error) {
console.error('Error fetching Kanji Subjects', error)
res.status(500).json({ error: 'Failed to fetch Kanji Subjects' })
}
})
export default router as Router

View File

@@ -0,0 +1,18 @@
import express, { type Router, type Request, type Response } from 'express'
import { VocabularyModel } from '../../../models/vocabulary.model.ts'
const router = express.Router()
router.get('/', async (_req: Request, res: Response) => {
try {
const doc = await VocabularyModel.find()
res.json(doc)
} catch (error) {
console.error('Error fetching API key:', error)
res.status(500).json({ error: 'Failed to fetch API key' })
}
})
export default router as Router

View File

@@ -0,0 +1,31 @@
import express, { Router } from 'express'
import { ApiKeyModel } from '../../../models/apikey.model.ts'
import { syncWanikaniData } from '../../../services/wanikaniService.ts'
const router = express.Router()
interface ApiKeyDocument {
apiKey?: string;
}
router.get('/sync', async (req, res) => {
try {
const apiKeyDoc = await ApiKeyModel.findOne() as ApiKeyDocument | null
const apiKey = apiKeyDoc?.apiKey
console.log(apiKey, apiKeyDoc)
if (!apiKey || apiKey.trim() === '') {
return res.status(401).json({ error: 'API Key not configured. Please sync your key first.' })
}
await syncWanikaniData(apiKey)
res.json({ success: true })
} catch (err) {
console.error(err)
res.status(500).json({ error: 'Failed to sync WaniKani data' })
}
})
export default router as Router