chore: add .gitignore, remove VSCode settings, and optimize database queries

This commit is contained in:
ethan.chen
2025-06-13 14:42:57 +08:00
parent e610605c76
commit 499321a842
7 changed files with 20 additions and 30 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.vscode
.DS_Store
**/*.db

View File

@@ -1,5 +0,0 @@
{
"recommendations": [
"denoland.vscode-deno"
]
}

View File

@@ -1,6 +0,0 @@
{
"deno.enablePaths": [
"./"
],
"editor.inlayHints.enabled": "off"
}

View File

@@ -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 }

Binary file not shown.

View File

@@ -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,

View File

@@ -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) {