init: 初始化
This commit is contained in:
151
routes/media.ts
Normal file
151
routes/media.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { Hono } from 'hono'
|
||||
import { db } from '../db/index.ts'
|
||||
import type { JwtVariables } from 'hono/jwt'
|
||||
|
||||
const media = new Hono<{ Variables: JwtVariables }>()
|
||||
|
||||
// 获取所有媒体记录
|
||||
media.get('/list', async (c) => {
|
||||
try {
|
||||
const mediaList = await db.prepare('SELECT * FROM media').all()
|
||||
return c.json({
|
||||
code: 0,
|
||||
data: mediaList,
|
||||
message: 'Success'
|
||||
})
|
||||
} catch (error: any) {
|
||||
return c.json({ code: 1, data: {}, message: error.message }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
// 创建新的媒体记录
|
||||
media.post('/create', async (c) => {
|
||||
try {
|
||||
const data = await c.req.json()
|
||||
const { title, type, rating, notes, platform, date } = data
|
||||
|
||||
const result = await 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)
|
||||
|
||||
return c.json({
|
||||
code: 0,
|
||||
data: newMedia,
|
||||
message: 'Created successfully'
|
||||
}, 201)
|
||||
} catch (error: any) {
|
||||
return c.json({ code: 2, data: {}, message: error.message }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
// 更新媒体记录
|
||||
media.put('/updateById/:id', async (c) => {
|
||||
try {
|
||||
const id = c.req.param('id')
|
||||
const data = await c.req.json()
|
||||
const { title, type, rating, notes, platform, date } = data
|
||||
|
||||
await 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)
|
||||
|
||||
if (!updatedMedia) {
|
||||
return c.json({ code: 1, data: {}, message: 'Media not found' }, 404)
|
||||
}
|
||||
|
||||
return c.json({
|
||||
code: 0,
|
||||
data: updatedMedia,
|
||||
message: 'Updated successfully'
|
||||
})
|
||||
} catch (error: any) {
|
||||
return c.json({ code: 2, data: {}, message: error.message }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
// 删除媒体记录
|
||||
media.delete('/deleteById/:id', async (c) => {
|
||||
try {
|
||||
const id = c.req.param('id')
|
||||
await 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)
|
||||
}
|
||||
})
|
||||
|
||||
// 分页查询媒体记录
|
||||
media.get('/page', async (c) => {
|
||||
try {
|
||||
const type = c.req.query('type')
|
||||
const currentPage = parseInt(c.req.query('currentPage') || '1')
|
||||
const pageSize = parseInt(c.req.query('pageSize') || '10')
|
||||
const title = c.req.query('title') || ''
|
||||
const startDate = c.req.query('startDate')
|
||||
const endDate = c.req.query('endDate')
|
||||
const sortBy = c.req.query('sortBy') || 'date'
|
||||
const sortType = c.req.query('sortType') || 'desc'
|
||||
|
||||
if (!type) {
|
||||
return c.json({ code: 1, data: {}, message: 'Type is required' }, 400)
|
||||
}
|
||||
|
||||
let query = 'SELECT * FROM media WHERE type = ?'
|
||||
const params = [type]
|
||||
|
||||
if (title) {
|
||||
query += ' AND title LIKE ?'
|
||||
params.push(`%${title}%`)
|
||||
}
|
||||
if (startDate) {
|
||||
query += ' AND date >= ?'
|
||||
params.push(startDate)
|
||||
}
|
||||
if (endDate) {
|
||||
query += ' AND date <= ?'
|
||||
params.push(endDate)
|
||||
}
|
||||
|
||||
// 添加排序
|
||||
if (sortBy === 'date') {
|
||||
query += ` ORDER BY date ${sortType.toUpperCase()}`
|
||||
} else if (sortBy === 'score') {
|
||||
query += ` ORDER BY rating ${sortType.toUpperCase()}`
|
||||
}
|
||||
|
||||
// 添加分页
|
||||
const offset = (currentPage - 1) * pageSize
|
||||
query += ' LIMIT ? OFFSET ?'
|
||||
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 total = totalResult?.total || 0
|
||||
|
||||
// 获取分页数据
|
||||
const mediaList = await db.prepare(query).all(...params)
|
||||
|
||||
return c.json({
|
||||
code: 0,
|
||||
data: {
|
||||
list: mediaList,
|
||||
total,
|
||||
currentPage,
|
||||
pageSize
|
||||
},
|
||||
message: 'Success'
|
||||
})
|
||||
} catch (error: any) {
|
||||
return c.json({ code: 1, data: {}, message: error.message }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
export default media
|
||||
40
routes/user.ts
Normal file
40
routes/user.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* @Date: 2025-06-11 17:57:58
|
||||
* @LastEditors: 陈子健
|
||||
* @LastEditTime: 2025-06-13 13:50:14
|
||||
* @FilePath: /my-score/honoback/routes/user.ts
|
||||
*/
|
||||
import { Hono } from 'hono'
|
||||
import { sign } from 'hono/jwt'
|
||||
import type { JwtVariables } from 'hono/jwt'
|
||||
import { db } from '../db/index.ts'
|
||||
|
||||
const user = new Hono<{ Variables: JwtVariables }>()
|
||||
|
||||
const AUTH_SECRET = 'it-is-a-secret'
|
||||
|
||||
// 登录路由
|
||||
user.post('/login', async (c) => {
|
||||
console.log(c.req.json(), 111)
|
||||
const { username, password } = await c.req.json()
|
||||
|
||||
// 从数据库验证用户
|
||||
const user = await db.prepare('SELECT * FROM users WHERE username = ? AND password = ?')
|
||||
.get(username, password)
|
||||
|
||||
if (user) {
|
||||
const token = await sign({ username: user.username }, AUTH_SECRET)
|
||||
return c.json({
|
||||
code: 200,
|
||||
data: { token },
|
||||
message: '登录成功'
|
||||
})
|
||||
}
|
||||
|
||||
return c.json({
|
||||
code: 401,
|
||||
message: '用户名或密码错误'
|
||||
}, 401)
|
||||
})
|
||||
|
||||
export default user
|
||||
Reference in New Issue
Block a user