routes_date_notes.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { Hono } from 'hono'
  2. import type { Variables, WithAuth } from "./middlewares.ts";
  3. export function createDateNotesRoutes(withAuth: WithAuth) {
  4. const dateNotesRoutes = new Hono<{ Variables: Variables }>()
  5. // 创建日期笔记
  6. dateNotesRoutes.post('/', withAuth, async (c) => {
  7. try {
  8. const apiClient = c.get('apiClient')
  9. const { code, note_date, note } = await c.req.json()
  10. if (!code || !note_date || !note) {
  11. return c.json({ error: '缺少必要参数' }, 400)
  12. }
  13. const user = c.get('user')
  14. if (!user) return c.json({ error: '未授权访问' }, 401)
  15. const [noteId] = await apiClient.database.table('date_notes').insert({
  16. code,
  17. note_date,
  18. note,
  19. created_at: apiClient.database.fn.now(),
  20. updated_at: apiClient.database.fn.now()
  21. })
  22. return c.json({
  23. message: '日期笔记创建成功',
  24. data: { id: noteId }
  25. }, 201)
  26. } catch (error) {
  27. console.error('创建日期笔记失败:', error)
  28. return c.json({ error: '创建日期笔记失败' }, 500)
  29. }
  30. })
  31. // 获取日期笔记列表(支持日期范围查询)
  32. dateNotesRoutes.get('/', withAuth, async (c) => {
  33. try {
  34. const apiClient = c.get('apiClient')
  35. const page = Number(c.req.query('page')) || 1
  36. const pageSize = Number(c.req.query('pageSize')) || 20
  37. const startDate = c.req.query('startDate')
  38. const endDate = c.req.query('endDate')
  39. const user = c.get('user')
  40. if (!user) return c.json({ error: '未授权访问' }, 401)
  41. const query = apiClient.database.table('date_notes')
  42. .orderBy('note_date', 'desc')
  43. .limit(pageSize)
  44. .offset((page - 1) * pageSize)
  45. // 日期范围查询
  46. if (startDate) query.where('note_date', '>=', startDate)
  47. if (endDate) query.where('note_date', '<=', endDate)
  48. const countQuery = query.clone()
  49. const notes = await query
  50. // 获取总数用于分页
  51. const total = await countQuery.count()
  52. const totalCount = Number(total)
  53. const totalPages = Math.ceil(totalCount / pageSize)
  54. return c.json({
  55. data: notes,
  56. pagination: {
  57. total: totalCount,
  58. current: page,
  59. pageSize,
  60. totalPages
  61. }
  62. })
  63. } catch (error) {
  64. console.error('获取日期笔记列表失败:', error)
  65. return c.json({ error: '获取日期笔记列表失败',message: error }, 500)
  66. }
  67. })
  68. // 获取单个日期笔记详情
  69. dateNotesRoutes.get('/:id', withAuth, async (c) => {
  70. try {
  71. const apiClient = c.get('apiClient')
  72. const noteId = c.req.param('id')
  73. const user = c.get('user')
  74. if (!user) return c.json({ error: '未授权访问' }, 401)
  75. const note = await apiClient.database.table('date_notes')
  76. .where('id', noteId)
  77. .first()
  78. if (!note) {
  79. return c.json({ error: '日期笔记不存在或无权访问' }, 404)
  80. }
  81. return c.json({
  82. message: '获取日期笔记成功',
  83. data: note
  84. })
  85. } catch (error) {
  86. console.error('获取日期笔记详情失败:', error)
  87. return c.json({ error: '获取日期笔记详情失败' }, 500)
  88. }
  89. })
  90. // 更新日期笔记
  91. dateNotesRoutes.put('/:id', withAuth, async (c) => {
  92. try {
  93. const apiClient = c.get('apiClient')
  94. const { code, note_date, note } = await c.req.json()
  95. if (!code || !note_date || !note) {
  96. return c.json({ error: '缺少必要参数' }, 400)
  97. }
  98. const user = c.get('user')
  99. if (!user) return c.json({ error: '未授权访问' }, 401)
  100. const noteId = c.req.param('id')
  101. await apiClient.database.table('date_notes')
  102. .where('id', noteId)
  103. .update({
  104. code,
  105. note_date,
  106. note,
  107. updated_at: apiClient.database.fn.now()
  108. })
  109. return c.json({ message: '日期笔记更新成功' })
  110. } catch (error) {
  111. console.error('更新日期笔记失败:', error)
  112. return c.json({ error: '更新日期笔记失败' }, 500)
  113. }
  114. })
  115. // 删除日期笔记
  116. dateNotesRoutes.delete('/:id', withAuth, async (c) => {
  117. try {
  118. const apiClient = c.get('apiClient')
  119. const user = c.get('user')
  120. if (!user) return c.json({ error: '未授权访问' }, 401)
  121. const noteId = c.req.param('id')
  122. await apiClient.database.table('date_notes')
  123. .where('id', noteId)
  124. .delete()
  125. return c.json({ message: '日期笔记已删除' })
  126. } catch (error) {
  127. console.error('删除日期笔记失败:', error)
  128. return c.json({ error: '删除日期笔记失败' }, 500)
  129. }
  130. })
  131. return dateNotesRoutes
  132. }