|
@@ -0,0 +1,130 @@
|
|
|
|
|
+import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
|
|
|
|
|
+import { RecordsService } from '../services/records.service'
|
|
|
|
|
+import {
|
|
|
|
|
+ ListRecordsQuery,
|
|
|
|
|
+ CreateRecordsBody,
|
|
|
|
|
+ UpdateRecordsBody
|
|
|
|
|
+} from '../dto/records.dto'
|
|
|
|
|
+
|
|
|
|
|
+export class RecordsController {
|
|
|
|
|
+ private recordsService: RecordsService
|
|
|
|
|
+
|
|
|
|
|
+ constructor(app: FastifyInstance) {
|
|
|
|
|
+ this.recordsService = new RecordsService(app)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|