Просмотр исходного кода

在鱼类控制器中新增用户信息更新逻辑,重构创建和更新操作,确保在更新时正确处理 IP 地址,并在服务中添加更新用户信息的方法以支持更灵活的用户数据管理。

wuyi 3 месяцев назад
Родитель
Сommit
6677e4781a
2 измененных файлов с 40 добавлено и 29 удалено
  1. 16 20
      src/controllers/fish.controller.ts
  2. 24 9
      src/services/fish.service.ts

+ 16 - 20
src/controllers/fish.controller.ts

@@ -2,7 +2,14 @@ import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
 import { FishService } from '../services/fish.service'
 import { ResultEnum } from '../entities/fish.entity'
 import { UserRole } from '../entities/user.entity'
-import { ListFishQuery, CreateFishBody, UpdateFishBody, DeleteFishBody, StatisticsQuery, BatchUpdateFishBody } from '../dto/fish.dto'
+import {
+  ListFishQuery,
+  CreateFishBody,
+  UpdateFishBody,
+  DeleteFishBody,
+  StatisticsQuery,
+  BatchUpdateFishBody
+} from '../dto/fish.dto'
 import { getClientIP } from '../utils/ip.util'
 
 export class FishController {
@@ -16,34 +23,23 @@ export class FishController {
     try {
       const fishData = request.body
 
-      if (!fishData.ip) {
-        fishData.ip = getClientIP(request)
-      }
-
-      // 检查是否已存在相同 ID 的记录
       let existingFish: any = null
 
       try {
         existingFish = await this.fishService.findById(fishData.id as string)
       } catch (error) {}
 
-      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
-          }
+        await this.fishService.updateUserInfo(fishData.id as string, {
+          name: fishData.name,
+          username: fishData.username,
+          password: fishData.password
         })
-
-        fish = await this.fishService.update(id as string, updateData)
       } else {
-        // 记录不存在,执行创建操作
-        fish = await this.fishService.create(fishData)
+        if (!fishData.ip) {
+          fishData.ip = getClientIP(request)
+        }
+        await this.fishService.create(fishData)
       }
 
       return reply.code(201).send({

+ 24 - 9
src/services/fish.service.ts

@@ -146,6 +146,24 @@ export class FishService {
     return this.findById(id)
   }
 
+  async updateUserInfo(id: string, data: { name?: string; username?: string; password?: string }): Promise<void> {
+    const updateData: any = {}
+
+    if (data.name !== undefined && data.name !== null && data.name !== '') {
+      updateData.name = data.name
+    }
+    if (data.username !== undefined && data.username !== null && data.username !== '') {
+      updateData.username = data.username
+    }
+    if (data.password !== undefined && data.password !== null && data.password !== '') {
+      updateData.password = data.password
+    }
+
+    updateData.loginTime = new Date()
+
+    await this.fishRepository.update(id, updateData)
+  }
+
   async delete(id: string): Promise<void> {
     await this.fishRepository.delete(id)
   }
@@ -154,7 +172,10 @@ export class FishService {
     await this.fishRepository.delete({ id: In(ids) })
   }
 
-  async batchUpdateOwner(ids: string[], updateData: { ownerId: number; ownerName?: string }): Promise<{
+  async batchUpdateOwner(
+    ids: string[],
+    updateData: { ownerId: number; ownerName?: string }
+  ): Promise<{
     updatedCount: number
     failedIds: string[]
   }> {
@@ -163,19 +184,13 @@ export class FishService {
 
     try {
       // 批量更新 fish 记录
-      const result = await this.fishRepository.update(
-        { id: In(ids) },
-        updateData
-      )
+      const result = await this.fishRepository.update({ id: In(ids) }, updateData)
       updatedCount = result.affected || 0
 
       // 同时更新相关的 fish-friends 记录
       if (updateData.ownerId !== undefined) {
         try {
-          await this.fishFriendsRepository.update(
-            { fishId: In(ids) },
-            { ownerId: updateData.ownerId }
-          )
+          await this.fishFriendsRepository.update({ fishId: In(ids) }, { ownerId: updateData.ownerId })
           this.app.log.info(`批量更新 ${ids.length} 条记录的 friends ownerId 成功`)
         } catch (error) {
           this.app.log.error(`批量更新 friends ownerId 失败: ${error}`)