routes_classroom_data.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { Hono } from 'hono'
  2. import type { Variables, WithAuth } from "./middlewares.ts";
  3. export function createClassroomDataRoutes(withAuth: WithAuth) {
  4. const classroomDataRoutes = new Hono<{ Variables: Variables }>()
  5. // 获取课堂数据列表
  6. classroomDataRoutes.get('/', withAuth, async (c) => {
  7. try {
  8. const apiClient = c.get('apiClient')
  9. const page = Number(c.req.query('page')) || 1
  10. const pageSize = Number(c.req.query('pageSize')) || 10
  11. const offset = (page - 1) * pageSize
  12. const search = c.req.query('search') || ''
  13. const classroomId = c.req.query('classroomId')
  14. let query = apiClient.database.table('classroom_data')
  15. .orderBy('id', 'desc')
  16. if (search) {
  17. query = query.where('title', 'like', `%${search}%`)
  18. }
  19. if (classroomId) {
  20. query = query.where('classroom_id', classroomId)
  21. }
  22. const total = await query.clone().count()
  23. const data = await query.select('*')
  24. .limit(pageSize).offset(offset)
  25. return c.json({
  26. data,
  27. pagination: {
  28. total: Number(total),
  29. current: page,
  30. pageSize
  31. }
  32. })
  33. } catch (error) {
  34. console.error('获取课堂数据列表失败:', error)
  35. return c.json({ error: '获取课堂数据列表失败' }, 500)
  36. }
  37. })
  38. // 获取单个课堂数据详情
  39. classroomDataRoutes.get('/:id', withAuth, async (c) => {
  40. try {
  41. const id = Number(c.req.param('id'))
  42. if (!id || isNaN(id)) {
  43. return c.json({ error: '无效的课堂数据ID' }, 400)
  44. }
  45. const apiClient = c.get('apiClient')
  46. const data = await apiClient.database.table('classroom_data')
  47. .where('id', id)
  48. .first()
  49. if (!data) {
  50. return c.json({ error: '课堂数据不存在' }, 404)
  51. }
  52. return c.json({
  53. data,
  54. message: '获取课堂数据详情成功'
  55. })
  56. } catch (error) {
  57. console.error('获取课堂数据详情失败:', error)
  58. return c.json({ error: '获取课堂数据详情失败' }, 500)
  59. }
  60. })
  61. // 创建课堂数据
  62. classroomDataRoutes.post('/', withAuth, async (c) => {
  63. try {
  64. const apiClient = c.get('apiClient')
  65. const body = await c.req.json()
  66. // 验证必填字段
  67. const { classroom_id, title, content } = body
  68. if (!classroom_id || !title || !content) {
  69. return c.json({ error: '缺少必要的课堂数据信息' }, 400)
  70. }
  71. // 创建课堂数据
  72. const [id] = await apiClient.database.table('classroom_data').insert({
  73. classroom_id,
  74. title,
  75. content,
  76. created_at: new Date(),
  77. updated_at: new Date()
  78. })
  79. const newData = await apiClient.database.table('classroom_data')
  80. .where('id', id)
  81. .first()
  82. return c.json({
  83. data: newData,
  84. message: '创建课堂数据成功'
  85. })
  86. } catch (error) {
  87. console.error('创建课堂数据失败:', error)
  88. return c.json({ error: '创建课堂数据失败' }, 500)
  89. }
  90. })
  91. // 更新课堂数据
  92. classroomDataRoutes.put('/:id', withAuth, async (c) => {
  93. try {
  94. const id = Number(c.req.param('id'))
  95. if (!id || isNaN(id)) {
  96. return c.json({ error: '无效的课堂数据ID' }, 400)
  97. }
  98. const apiClient = c.get('apiClient')
  99. const body = await c.req.json()
  100. // 验证必填字段
  101. const { title, content } = body
  102. if (!title || !content) {
  103. return c.json({ error: '缺少必要的课堂数据信息' }, 400)
  104. }
  105. // 检查数据是否存在
  106. const existingData = await apiClient.database.table('classroom_data')
  107. .where('id', id)
  108. .first()
  109. if (!existingData) {
  110. return c.json({ error: '课堂数据不存在' }, 404)
  111. }
  112. // 更新数据
  113. await apiClient.database.table('classroom_data')
  114. .where('id', id)
  115. .update({
  116. title,
  117. content,
  118. updated_at: new Date()
  119. })
  120. const updatedData = await apiClient.database.table('classroom_data')
  121. .where('id', id)
  122. .first()
  123. return c.json({
  124. data: updatedData,
  125. message: '更新课堂数据成功'
  126. })
  127. } catch (error) {
  128. console.error('更新课堂数据失败:', error)
  129. return c.json({ error: '更新课堂数据失败' }, 500)
  130. }
  131. })
  132. // 删除课堂数据
  133. classroomDataRoutes.delete('/:id', withAuth, async (c) => {
  134. try {
  135. const id = Number(c.req.param('id'))
  136. if (!id || isNaN(id)) {
  137. return c.json({ error: '无效的课堂数据ID' }, 400)
  138. }
  139. const apiClient = c.get('apiClient')
  140. // 检查数据是否存在
  141. const existingData = await apiClient.database.table('classroom_data')
  142. .where('id', id)
  143. .first()
  144. if (!existingData) {
  145. return c.json({ error: '课堂数据不存在' }, 404)
  146. }
  147. // 删除数据
  148. await apiClient.database.table('classroom_data')
  149. .where('id', id)
  150. .delete()
  151. return c.json({
  152. message: '删除课堂数据成功',
  153. id
  154. })
  155. } catch (error) {
  156. console.error('删除课堂数据失败:', error)
  157. return c.json({ error: '删除课堂数据失败' }, 500)
  158. }
  159. })
  160. return classroomDataRoutes
  161. }