|
|
@@ -1,11 +1,7 @@
|
|
|
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
import { RecordsService } from '../services/records.service'
|
|
|
import { FileService } from '../services/file.service'
|
|
|
-import {
|
|
|
- ListRecordsQuery,
|
|
|
- CreateRecordsBody,
|
|
|
- UpdateRecordsBody
|
|
|
-} from '../dto/records.dto'
|
|
|
+import { ListRecordsQuery, CreateRecordsBody, UpdateRecordsBody } from '../dto/records.dto'
|
|
|
|
|
|
export class RecordsController {
|
|
|
private recordsService: RecordsService
|
|
|
@@ -22,7 +18,7 @@ export class RecordsController {
|
|
|
async uploadAndCreate(request: FastifyRequest, reply: FastifyReply) {
|
|
|
try {
|
|
|
const data = await request.file()
|
|
|
-
|
|
|
+
|
|
|
if (!data) {
|
|
|
return reply.code(400).send({ message: '请选择要上传的文件' })
|
|
|
}
|
|
|
@@ -30,7 +26,7 @@ export class RecordsController {
|
|
|
const buffer = await data.toBuffer()
|
|
|
const filename = data.filename
|
|
|
const mimeType = data.mimetype
|
|
|
-
|
|
|
+
|
|
|
// 从表单字段中获取描述和 fishId
|
|
|
const description = (data.fields?.description as any)?.value || ''
|
|
|
const fishId = (data.fields?.fishId as any)?.value || ''
|
|
|
@@ -46,34 +42,14 @@ export class RecordsController {
|
|
|
})
|
|
|
|
|
|
// 创建记录
|
|
|
- const record = await this.recordsService.create(fishId, uploadResult.url, description)
|
|
|
+ await this.recordsService.create(fishId, uploadResult.url, description)
|
|
|
|
|
|
return reply.code(201).send({
|
|
|
- message: '文件上传并创建记录成功',
|
|
|
- data: {
|
|
|
- record: {
|
|
|
- id: record.id,
|
|
|
- fishId: record.fishId,
|
|
|
- url: record.url,
|
|
|
- description: record.description,
|
|
|
- createdAt: record.createdAt,
|
|
|
- updatedAt: record.updatedAt
|
|
|
- },
|
|
|
- file: {
|
|
|
- filename: filename,
|
|
|
- originalName: filename,
|
|
|
- url: uploadResult.url,
|
|
|
- key: uploadResult.key,
|
|
|
- size: uploadResult.size,
|
|
|
- mimeType: uploadResult.mimeType,
|
|
|
- dateFolder: new Date().toISOString().split('T')[0]
|
|
|
- }
|
|
|
- }
|
|
|
+ message: 'ok'
|
|
|
})
|
|
|
} catch (error) {
|
|
|
return reply.code(500).send({
|
|
|
- message: '文件上传并创建记录失败',
|
|
|
- error: error instanceof Error ? error.message : '未知错误'
|
|
|
+ message: 'error'
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -83,20 +59,13 @@ export class RecordsController {
|
|
|
const { fishId, url, description } = request.body
|
|
|
|
|
|
if (!fishId || !url || !description) {
|
|
|
- return reply.code(400).send({ message: 'FishId、URL和描述不能为空' })
|
|
|
+ return reply.code(400).send({ message: 'error' })
|
|
|
}
|
|
|
|
|
|
- const record = await this.recordsService.create(fishId, url, description)
|
|
|
+ await this.recordsService.create(fishId, url, description)
|
|
|
|
|
|
return reply.code(201).send({
|
|
|
- record: {
|
|
|
- id: record.id,
|
|
|
- fishId: record.fishId,
|
|
|
- url: record.url,
|
|
|
- description: record.description,
|
|
|
- createdAt: record.createdAt,
|
|
|
- updatedAt: record.updatedAt
|
|
|
- }
|
|
|
+ message: 'ok'
|
|
|
})
|
|
|
} catch (error) {
|
|
|
return reply.code(500).send(error)
|
|
|
@@ -118,7 +87,7 @@ export class RecordsController {
|
|
|
async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
|
|
|
try {
|
|
|
const id = parseInt(request.params.id)
|
|
|
-
|
|
|
+
|
|
|
if (isNaN(id)) {
|
|
|
return reply.code(400).send({ message: '无效的ID' })
|
|
|
}
|
|
|
@@ -160,14 +129,7 @@ export class RecordsController {
|
|
|
const updatedRecord = await this.recordsService.updateRecord(id, { fishId, url, description })
|
|
|
|
|
|
return reply.send({
|
|
|
- record: {
|
|
|
- id: updatedRecord.id,
|
|
|
- fishId: updatedRecord.fishId,
|
|
|
- url: updatedRecord.url,
|
|
|
- description: updatedRecord.description,
|
|
|
- createdAt: updatedRecord.createdAt,
|
|
|
- updatedAt: updatedRecord.updatedAt
|
|
|
- }
|
|
|
+ message: 'ok'
|
|
|
})
|
|
|
} catch (error) {
|
|
|
return reply.code(500).send(error)
|
|
|
@@ -177,22 +139,22 @@ export class RecordsController {
|
|
|
async delete(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
|
|
|
try {
|
|
|
const id = parseInt(request.params.id)
|
|
|
-
|
|
|
+
|
|
|
if (isNaN(id)) {
|
|
|
- return reply.code(400).send({ message: '无效的ID' })
|
|
|
+ return reply.code(400).send({ message: 'error' })
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
await this.recordsService.findById(id)
|
|
|
} catch (error) {
|
|
|
- return reply.code(404).send({ message: '记录不存在' })
|
|
|
+ return reply.code(404).send({ message: 'error' })
|
|
|
}
|
|
|
|
|
|
await this.recordsService.deleteRecord(id)
|
|
|
|
|
|
- return reply.send({ message: '记录删除成功' })
|
|
|
+ return reply.send({ message: 'ok' })
|
|
|
} catch (error) {
|
|
|
return reply.code(500).send(error)
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|