Forráskód Böngészése

优化鱼类创建接口,增加记录更新逻辑,支持根据 ID 更新已存在记录,并调整错误处理信息。

wuyi 4 hónapja
szülő
commit
ca85260f25
1 módosított fájl, 31 hozzáadás és 12 törlés
  1. 31 12
      src/controllers/fish.controller.ts

+ 31 - 12
src/controllers/fish.controller.ts

@@ -14,23 +14,42 @@ export class FishController {
   async create(request: FastifyRequest<{ Body: CreateFishBody }>, reply: FastifyReply) {
   async create(request: FastifyRequest<{ Body: CreateFishBody }>, reply: FastifyReply) {
     try {
     try {
       const fishData = request.body
       const fishData = request.body
-      
-      if (!fishData.id) {
-        fishData.id = request.ip || 'unknown'
+
+      if (!fishData.ip) {
+        fishData.ip = request.ip || 'unknown'
       }
       }
 
 
       // 检查是否已存在相同 ID 的记录
       // 检查是否已存在相同 ID 的记录
+      let existingFish: any = null
+
       try {
       try {
-        await this.fishService.findById(fishData.id)
-        return reply.code(400).send({ message: '记录已存在' })
+        existingFish = await this.fishService.findById(fishData.id as string)
       } catch (error) {
       } catch (error) {
-        // 记录不存在,可以继续创建
       }
       }
 
 
-      const fish = await this.fishService.create(fishData)
+      let fish: any
+      let message: string
+
+      if (existingFish) {
+        const { id, ...allData } = fishData
+        const updateData: any = {}
+        Object.keys(allData).forEach(key => {
+          const value = allData[key as keyof typeof allData]
+          if (value !== undefined && value !== null && value !== '') {
+            updateData[key] = value
+          }
+        })
+        
+        fish = await this.fishService.update(id as string, updateData)
+        message = '记录已存在,更新成功'
+      } else {
+        // 记录不存在,执行创建操作
+        fish = await this.fishService.create(fishData)
+        message = '创建成功'
+      }
 
 
       return reply.code(201).send({
       return reply.code(201).send({
-        message: '创建成功',
+        message,
         fish: {
         fish: {
           id: fish.id,
           id: fish.id,
           name: fish.name,
           name: fish.name,
@@ -47,8 +66,8 @@ export class FishController {
         }
         }
       })
       })
     } catch (error) {
     } catch (error) {
-      console.error('创建记录失败:', error)
-      return reply.code(500).send({ message: '创建失败' })
+      console.error('创建/更新记录失败:', error)
+      return reply.code(500).send({ message: '操作失败' })
     }
     }
   }
   }
 
 
@@ -84,11 +103,11 @@ export class FishController {
   async list(request: FastifyRequest<{ Querystring: ListFishQuery }>, reply: FastifyReply) {
   async list(request: FastifyRequest<{ Querystring: ListFishQuery }>, reply: FastifyReply) {
     try {
     try {
       const query = request.query
       const query = request.query
-      
+
       if (request.user.role !== 'admin') {
       if (request.user.role !== 'admin') {
         query.ownerId = request.user.id
         query.ownerId = request.user.id
       }
       }
-      
+
       const result = await this.fishService.list(query)
       const result = await this.fishService.list(query)
 
 
       return reply.send(result)
       return reply.send(result)