Skip to content

Commit ef5f52f

Browse files
committed
Fix track names with dots breaking MongoDB queries
1 parent 972f7b5 commit ef5f52f

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

server/database/judge.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,27 @@ func UpdateAfterSeen(db *mongo.Database, ctx context.Context, judge *models.Judg
181181
return errors.New("error updating judge: " + err.Error())
182182
}
183183

184-
incData := gin.H{}
185184
if judge.Track != "" {
186-
incData["track_seen."+judge.Track] = 1
185+
project, findErr := FindProject(db, ctx, &seenProject.ProjectId)
186+
if findErr != nil {
187+
return errors.New("error finding project: " + findErr.Error())
188+
}
189+
if project.TrackSeen == nil {
190+
project.TrackSeen = make(map[string]int64)
191+
}
192+
project.TrackSeen[judge.Track] += 1
193+
_, err = db.Collection("projects").UpdateOne(
194+
ctx,
195+
gin.H{"_id": seenProject.ProjectId},
196+
gin.H{"$set": gin.H{"track_seen": project.TrackSeen, "last_activity": util.Now()}},
197+
)
187198
} else {
188-
incData["seen"] = 1
199+
_, err = db.Collection("projects").UpdateOne(
200+
ctx,
201+
gin.H{"_id": seenProject.ProjectId},
202+
gin.H{"$inc": gin.H{"seen": 1}, "$set": gin.H{"last_activity": util.Now()}},
203+
)
189204
}
190-
191-
// Update the project's seen count
192-
_, err = db.Collection("projects").UpdateOne(
193-
ctx,
194-
gin.H{"_id": seenProject.ProjectId},
195-
gin.H{"$inc": incData, "$set": gin.H{"last_activity": util.Now()}},
196-
)
197205
if err != nil {
198206
return errors.New("error updating project: " + err.Error())
199207
}

server/database/options.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ func UpdateQRCode(db *mongo.Database, ctx context.Context, qrCode string) error
174174

175175
// UpdateTrackQRCode updates the QR code for a track in the database
176176
func UpdateTrackQRCode(db *mongo.Database, ctx context.Context, track string, qrCode string) error {
177-
key := "track_qr_codes." + track
178-
_, err := db.Collection("options").UpdateOne(ctx, gin.H{}, gin.H{"$set": gin.H{key: qrCode}})
177+
options, err := GetOptions(db, ctx)
178+
if err != nil {
179+
return err
180+
}
181+
options.TrackQRCodes[track] = qrCode
182+
_, err = db.Collection("options").UpdateOne(ctx, gin.H{}, gin.H{"$set": gin.H{"track_qr_codes": options.TrackQRCodes}})
179183
return err
180184
}
181185

server/database/project.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,22 @@ func UpdateProjectStars(db *mongo.Database, ctx context.Context, projId primitiv
319319

320320
// UpdateProjectTrackStars updates the starred count of a project in a specific track, incrementing or decrementing it
321321
func UpdateProjectTrackStars(db *mongo.Database, ctx context.Context, projId primitive.ObjectID, track string, increment bool) error {
322-
change := -1
323-
if increment {
324-
change = 1
322+
project, err := FindProject(db, ctx, &projId)
323+
if err != nil {
324+
return err
325325
}
326-
track_str := "track_stars." + track
327-
328-
_, err := db.Collection("projects").UpdateOne(ctx, gin.H{"_id": projId}, gin.H{"$inc": gin.H{track_str: change}})
326+
if project == nil {
327+
return errors.New("project not found")
328+
}
329+
change := int64(1)
330+
if !increment {
331+
change = -1
332+
}
333+
if project.TrackStars == nil {
334+
project.TrackStars = make(map[string]int64)
335+
}
336+
project.TrackStars[track] += change
337+
_, err = db.Collection("projects").UpdateOne(ctx, gin.H{"_id": projId}, gin.H{"$set": gin.H{"track_stars": project.TrackStars}})
329338
return err
330339
}
331340

0 commit comments

Comments
 (0)