From 4f9891dddc2f891def1e046b1380174c0ccb63a5 Mon Sep 17 00:00:00 2001 From: SeraphJACK Date: Sat, 21 Sep 2024 11:43:44 +0800 Subject: [PATCH] feat(stat): check time before writing to db --- services/stat.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/stat.go b/services/stat.go index 12364ba..e33b603 100644 --- a/services/stat.go +++ b/services/stat.go @@ -1,6 +1,7 @@ package services import ( + "fmt" "net/http" "strconv" "time" @@ -39,10 +40,14 @@ func EndpointStat(ng *gin.Engine) { err := db.Transaction(func(tx *gorm.DB) error { for _, v := range form { for _, t := range v.At { + timeParsed := time.Unix(t, 0) + if timeParsed.After(time.Now().Add(time.Hour)) { + return fmt.Errorf("play time is significantly larger than current time: %v", timeParsed) + } record := model.PlayRecord{ UserID: user.ID, Track: v.Track, - At: time.Unix(t, 0), + At: timeParsed, } if err := tx.Save(&record).Error; err != nil { return err