diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3568c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode +.DS_Store +**/*.db diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index c4eb3fe..0000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "recommendations": [ - "denoland.vscode-deno" - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index a2bb77c..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "deno.enablePaths": [ - "./" - ], - "editor.inlayHints.enabled": "off" -} \ No newline at end of file diff --git a/db/index.ts b/db/index.ts index dc4314c..2225144 100644 --- a/db/index.ts +++ b/db/index.ts @@ -1,21 +1,16 @@ /* * @Date: 2025-06-12 16:48:44 * @LastEditors: 陈子健 - * @LastEditTime: 2025-06-12 17:31:37 + * @LastEditTime: 2025-06-13 14:23:03 * @FilePath: /my-score/honoback/db/index.ts */ import { DatabaseSync } from "node:sqlite" import { join, dirname } from 'node:path' import { fileURLToPath } from 'node:url' -import { readFileSync } from 'node:fs' const __dirname = dirname(fileURLToPath(import.meta.url)) // 初始化数据库连接 const db = new DatabaseSync(join(__dirname, 'media.db')) -// 执行schema.sql中的SQL语句 -const schema = readFileSync(join(__dirname, 'schema.sql'), 'utf-8') -db.exec(schema) - -export { db } \ No newline at end of file +export { db } diff --git a/db/media.db b/db/media.db deleted file mode 100644 index 5f56da4..0000000 Binary files a/db/media.db and /dev/null differ diff --git a/routes/media.ts b/routes/media.ts index d680d5b..e258ad3 100644 --- a/routes/media.ts +++ b/routes/media.ts @@ -5,9 +5,9 @@ import type { JwtVariables } from 'hono/jwt' const media = new Hono<{ Variables: JwtVariables }>() // 获取所有媒体记录 -media.get('/list', async (c) => { +media.get('/list', (c) => { try { - const mediaList = await db.prepare('SELECT * FROM media').all() + const mediaList = db.prepare('SELECT * FROM media').all() return c.json({ code: 0, data: mediaList, @@ -24,12 +24,12 @@ media.post('/create', async (c) => { const data = await c.req.json() const { title, type, rating, notes, platform, date } = data - const result = await db.prepare(` + const result = db.prepare(` INSERT INTO media (title, type, rating, notes, platform, date, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'), datetime('now')) `).run(title, type, rating, notes, platform, date) - const newMedia = await db.prepare('SELECT * FROM media WHERE id = ?').get(result.lastInsertRowid) + const newMedia = db.prepare('SELECT * FROM media WHERE id = ?').get(result.lastInsertRowid) return c.json({ code: 0, @@ -48,13 +48,13 @@ media.put('/updateById/:id', async (c) => { const data = await c.req.json() const { title, type, rating, notes, platform, date } = data - await db.prepare(` + db.prepare(` UPDATE media SET title = ?, type = ?, rating = ?, notes = ?, platform = ?, date = ?, updated_at = datetime('now') WHERE id = ? `).run(title, type, rating, notes, platform, date, id) - const updatedMedia = await db.prepare('SELECT * FROM media WHERE id = ?').get(id) + const updatedMedia = db.prepare('SELECT * FROM media WHERE id = ?').get(id) if (!updatedMedia) { return c.json({ code: 1, data: {}, message: 'Media not found' }, 404) @@ -74,7 +74,7 @@ media.put('/updateById/:id', async (c) => { media.delete('/deleteById/:id', async (c) => { try { const id = c.req.param('id') - await db.prepare('DELETE FROM media WHERE id = ?').run(id) + db.prepare('DELETE FROM media WHERE id = ?').run(id) return c.json({ code: 0, data: {}, message: 'Deleted successfully' }) } catch (error: any) { return c.json({ code: 2, data: {}, message: error.message }, 500) @@ -126,12 +126,15 @@ media.get('/page', async (c) => { params.push(pageSize.toString(), offset.toString()) // 获取总数 - const countQuery = query.replace('SELECT *', 'SELECT COUNT(*) as total') - const totalResult = await db.prepare(countQuery).get(...params.slice(0, -2)) + const countQuery = query + .replace('SELECT *', 'SELECT COUNT(*) as total') + .replace(/ORDER BY.*$/, '') // 移除 ORDER BY 子句 + .replace(/LIMIT.*$/, '') // 移除 LIMIT 和 OFFSET 子句 + const totalResult = db.prepare(countQuery).get(...params.slice(0, -2)) const total = totalResult?.total || 0 // 获取分页数据 - const mediaList = await db.prepare(query).all(...params) + const mediaList = db.prepare(query).all(...params) return c.json({ code: 0, diff --git a/routes/user.ts b/routes/user.ts index 0763c8f..678050e 100644 --- a/routes/user.ts +++ b/routes/user.ts @@ -1,7 +1,7 @@ /* * @Date: 2025-06-11 17:57:58 * @LastEditors: 陈子健 - * @LastEditTime: 2025-06-13 13:50:14 + * @LastEditTime: 2025-06-13 14:23:37 * @FilePath: /my-score/honoback/routes/user.ts */ import { Hono } from 'hono' @@ -19,7 +19,7 @@ user.post('/login', async (c) => { const { username, password } = await c.req.json() // 从数据库验证用户 - const user = await db.prepare('SELECT * FROM users WHERE username = ? AND password = ?') + const user = db.prepare('SELECT * FROM users WHERE username = ? AND password = ?') .get(username, password) if (user) {