records.controller.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { RecordsService } from '../services/records.service'
  3. import { FileService } from '../services/file.service'
  4. import {
  5. ListRecordsQuery,
  6. CreateRecordsBody,
  7. UpdateRecordsBody
  8. } from '../dto/records.dto'
  9. export class RecordsController {
  10. private recordsService: RecordsService
  11. private fileService: FileService
  12. constructor(app: FastifyInstance) {
  13. this.recordsService = new RecordsService(app)
  14. this.fileService = new FileService(app)
  15. }
  16. /**
  17. * 上传文件并创建记录
  18. */
  19. async uploadAndCreate(request: FastifyRequest, reply: FastifyReply) {
  20. try {
  21. const data = await request.file()
  22. if (!data) {
  23. return reply.code(400).send({ message: '请选择要上传的文件' })
  24. }
  25. const buffer = await data.toBuffer()
  26. const filename = data.filename
  27. const mimeType = data.mimetype
  28. // 从表单字段中获取描述
  29. const description = (data.fields?.description as any)?.value || ''
  30. // 上传文件到OSS
  31. const uploadResult = await this.fileService.uploadFile(buffer, filename, mimeType, {
  32. folder: 'tweb',
  33. maxSize: 50 * 1024 * 1024 // 50MB
  34. })
  35. // 创建记录
  36. const record = await this.recordsService.create(uploadResult.url, description)
  37. return reply.code(201).send({
  38. message: '文件上传并创建记录成功',
  39. data: {
  40. record: {
  41. id: record.id,
  42. url: record.url,
  43. description: record.description,
  44. createdAt: record.createdAt,
  45. updatedAt: record.updatedAt
  46. },
  47. file: {
  48. filename: filename,
  49. originalName: filename,
  50. url: uploadResult.url,
  51. key: uploadResult.key,
  52. size: uploadResult.size,
  53. mimeType: uploadResult.mimeType,
  54. dateFolder: new Date().toISOString().split('T')[0]
  55. }
  56. }
  57. })
  58. } catch (error) {
  59. return reply.code(500).send({
  60. message: '文件上传并创建记录失败',
  61. error: error instanceof Error ? error.message : '未知错误'
  62. })
  63. }
  64. }
  65. async create(request: FastifyRequest<{ Body: CreateRecordsBody }>, reply: FastifyReply) {
  66. try {
  67. const { url, description } = request.body
  68. if (!url || !description) {
  69. return reply.code(400).send({ message: 'URL和描述不能为空' })
  70. }
  71. const record = await this.recordsService.create(url, description)
  72. return reply.code(201).send({
  73. record: {
  74. id: record.id,
  75. url: record.url,
  76. description: record.description,
  77. createdAt: record.createdAt,
  78. updatedAt: record.updatedAt
  79. }
  80. })
  81. } catch (error) {
  82. return reply.code(500).send(error)
  83. }
  84. }
  85. async list(request: FastifyRequest<{ Querystring: ListRecordsQuery }>, reply: FastifyReply) {
  86. try {
  87. const { page, size, url, description } = request.query
  88. const result = await this.recordsService.findAll(page, size, url, description)
  89. return reply.send(result)
  90. } catch (error) {
  91. return reply.code(500).send(error)
  92. }
  93. }
  94. async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  95. try {
  96. const id = parseInt(request.params.id)
  97. if (isNaN(id)) {
  98. return reply.code(400).send({ message: '无效的ID' })
  99. }
  100. const record = await this.recordsService.findById(id)
  101. return reply.send({
  102. record: {
  103. id: record.id,
  104. url: record.url,
  105. description: record.description,
  106. createdAt: record.createdAt,
  107. updatedAt: record.updatedAt
  108. }
  109. })
  110. } catch (error) {
  111. if (error && typeof error === 'object' && 'name' in error && error.name === 'EntityNotFoundError') {
  112. return reply.code(404).send({ message: '记录不存在' })
  113. }
  114. return reply.code(500).send(error)
  115. }
  116. }
  117. async update(request: FastifyRequest<{ Body: UpdateRecordsBody }>, reply: FastifyReply) {
  118. try {
  119. const { id, url, description } = request.body
  120. if (!id) {
  121. return reply.code(400).send({ message: 'ID不能为空' })
  122. }
  123. try {
  124. await this.recordsService.findById(id)
  125. } catch (error) {
  126. return reply.code(404).send({ message: '记录不存在' })
  127. }
  128. const updatedRecord = await this.recordsService.updateRecord(id, { url, description })
  129. return reply.send({
  130. record: {
  131. id: updatedRecord.id,
  132. url: updatedRecord.url,
  133. description: updatedRecord.description,
  134. createdAt: updatedRecord.createdAt,
  135. updatedAt: updatedRecord.updatedAt
  136. }
  137. })
  138. } catch (error) {
  139. return reply.code(500).send(error)
  140. }
  141. }
  142. async delete(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  143. try {
  144. const id = parseInt(request.params.id)
  145. if (isNaN(id)) {
  146. return reply.code(400).send({ message: '无效的ID' })
  147. }
  148. try {
  149. await this.recordsService.findById(id)
  150. } catch (error) {
  151. return reply.code(404).send({ message: '记录不存在' })
  152. }
  153. await this.recordsService.deleteRecord(id)
  154. return reply.send({ message: '记录删除成功' })
  155. } catch (error) {
  156. return reply.code(500).send(error)
  157. }
  158. }
  159. }