Selaa lähdekoodia

预测记录重复问题修复

jax 4 kuukautta sitten
vanhempi
commit
5b4e69fa49
3 muutettua tiedostoa jossa 48 lisäystä ja 5 poistoa
  1. 24 2
      src/app/api/prediction/route.js
  2. 23 3
      src/app/api/user/route.js
  3. 1 0
      src/app/models/Prediction.js

+ 24 - 2
src/app/api/prediction/route.js

@@ -219,6 +219,12 @@ export const POST = withAuth(async (request) => {
         match: matchId,
       });
 
+      const filter = {
+        user: userId,
+        match: matchId,
+      };
+
+
       if (existingPrediction) {
         // 更新预测
         if (type === "football" && football) {
@@ -254,7 +260,15 @@ export const POST = withAuth(async (request) => {
           }
         }
 
-        const updatedPrediction = await existingPrediction.save();
+        // const updatedPrediction = await existingPrediction.save();
+        const updatedPrediction = await Prediction.findOneAndUpdate(
+          filter,
+          existingPrediction,
+          {
+            new: true,       // 返回更新后的数据
+            upsert: false,   // 不存在则不创建新记录
+          }
+        );
         createdPredictions.push(updatedPrediction);
       } else {
         // 创建新预测
@@ -284,7 +298,15 @@ export const POST = withAuth(async (request) => {
           }),
         });
 
-        const savedPrediction = await newPrediction.save();
+        // const savedPrediction = await newPrediction.save();
+        const savedPrediction = await Prediction.findOneAndUpdate(
+          filter,
+          newPrediction,
+          {
+            new: true,          // 返回更新后的数据
+            upsert: true,       // 如果不存在则创建新记录
+            setDefaultsOnInsert: true, // 在插入新记录时,应用模式中的默认值
+          });
         createdPredictions.push(savedPrediction);
       }
     }

+ 23 - 3
src/app/api/user/route.js

@@ -29,7 +29,7 @@ export const GET = withAuth(async (request) => {
     }
 
     const searchQuery = {};
-
+    let sortParams = {};
     for (const [key, value] of searchParams.entries()) {
       if (["current", "pageSize"].includes(key)) continue;
 
@@ -54,13 +54,26 @@ export const GET = withAuth(async (request) => {
         case "id":
           searchQuery._id = value;
           break;
+        case "sort":
+          if (value && value != '{}') {
+            const sortObj = JSON.parse(value);
+            const sortField = Object.keys(sortObj)[0]; // 获取字段名,例如 'points'
+            if (sortObj[sortField] === "ascend") {
+              sortParams[sortField] = 1; // 升序
+            } else {
+              sortParams[sortField] = -1; // 降序
+            }
+          }
+          break;
       }
     }
 
 
     const skip = (current - 1) * pageSize;
     const totalUsers = await User.countDocuments(searchQuery);
-    const usersData = await User.aggregate([
+
+    // 构建聚合管道
+    const pipeline = [
       { $match: searchQuery },
       { $project: { password: 0 } },
       {
@@ -73,7 +86,14 @@ export const GET = withAuth(async (request) => {
       { $sort: { sortOrder: 1, createdAt: -1 } },
       { $skip: skip },
       { $limit: pageSize },
-    ]);
+    ];
+
+
+    if (Object.keys(sortParams).length > 0) {
+      pipeline.splice(3, 1, { $sort: { ...sortParams } });
+    }
+
+    const usersData = await User.aggregate(pipeline);
 
     const response = NextResponse.json({
       success: true,

+ 1 - 0
src/app/models/Prediction.js

@@ -90,6 +90,7 @@ const PredictionSchema = new mongoose.Schema(
 
 PredictionSchema.index({ "user": 1 }); // user 索引
 PredictionSchema.index({ "match": 1 }); // match 索引
+PredictionSchema.index({ user: 1, match: 1 }, { unique: true });
 
 export default mongoose.models.Prediction ||
   mongoose.model("Prediction", PredictionSchema);