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' export class RecordsController { private recordsService: RecordsService private fileService: FileService constructor(app: FastifyInstance) { this.recordsService = new RecordsService(app) this.fileService = new FileService(app) } /** * 上传文件并创建记录 */ async uploadAndCreate(request: FastifyRequest, reply: FastifyReply) { try { const data = await request.file() if (!data) { return reply.code(400).send({ message: '请选择要上传的文件' }) } const buffer = await data.toBuffer() const filename = data.filename const mimeType = data.mimetype // 从表单字段中获取描述 const description = (data.fields?.description as any)?.value || '' // 上传文件到OSS const uploadResult = await this.fileService.uploadFile(buffer, filename, mimeType, { folder: 'tweb', maxSize: 50 * 1024 * 1024 // 50MB }) // 创建记录 const record = await this.recordsService.create(uploadResult.url, description) return reply.code(201).send({ message: '文件上传并创建记录成功', data: { record: { id: record.id, 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] } } }) } catch (error) { return reply.code(500).send({ message: '文件上传并创建记录失败', error: error instanceof Error ? error.message : '未知错误' }) } } async create(request: FastifyRequest<{ Body: CreateRecordsBody }>, reply: FastifyReply) { try { const { url, description } = request.body if (!url || !description) { return reply.code(400).send({ message: 'URL和描述不能为空' }) } const record = await this.recordsService.create(url, description) return reply.code(201).send({ record: { id: record.id, url: record.url, description: record.description, createdAt: record.createdAt, updatedAt: record.updatedAt } }) } catch (error) { return reply.code(500).send(error) } } async list(request: FastifyRequest<{ Querystring: ListRecordsQuery }>, reply: FastifyReply) { try { const { page, size, url, description } = request.query const result = await this.recordsService.findAll(page, size, url, description) return reply.send(result) } catch (error) { return reply.code(500).send(error) } } 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' }) } const record = await this.recordsService.findById(id) return reply.send({ record: { id: record.id, url: record.url, description: record.description, createdAt: record.createdAt, updatedAt: record.updatedAt } }) } catch (error) { if (error && typeof error === 'object' && 'name' in error && error.name === 'EntityNotFoundError') { return reply.code(404).send({ message: '记录不存在' }) } return reply.code(500).send(error) } } async update(request: FastifyRequest<{ Body: UpdateRecordsBody }>, reply: FastifyReply) { try { const { id, url, description } = request.body if (!id) { return reply.code(400).send({ message: 'ID不能为空' }) } try { await this.recordsService.findById(id) } catch (error) { return reply.code(404).send({ message: '记录不存在' }) } const updatedRecord = await this.recordsService.updateRecord(id, { url, description }) return reply.send({ record: { id: updatedRecord.id, url: updatedRecord.url, description: updatedRecord.description, createdAt: updatedRecord.createdAt, updatedAt: updatedRecord.updatedAt } }) } catch (error) { return reply.code(500).send(error) } } 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' }) } try { await this.recordsService.findById(id) } catch (error) { return reply.code(404).send({ message: '记录不存在' }) } await this.recordsService.deleteRecord(id) return reply.send({ message: '记录删除成功' }) } catch (error) { return reply.code(500).send(error) } } }