package db import ( "time" "github.com/jinzhu/gorm" ) type PlayerLevel struct { UserID string `gorm:"primary_key"` LastActive time.Time XP int64 } func GetTopFive() []PlayerLevel { var results []PlayerLevel db.Model(&PlayerLevel{}).Order("xp desc").Limit(5).Find(&results) return results } func GetXP(userID string) int64 { result := PlayerLevel{ UserID: userID, } db.Where(&result).First(&result) return result.XP } func IncrementXP(userID string, value int64, newTime time.Time) { result := db.Model(&PlayerLevel{}). Where(&PlayerLevel{UserID: userID}). UpdateColumn("xp", gorm.Expr("xp + ?", value)). UpdateColumn("last_active", newTime) if result.RowsAffected == 0 { db.Create(&PlayerLevel{ UserID: userID, XP: value, LastActive: newTime, }) } } func GetLastActive(userID string) time.Time { result := PlayerLevel{ UserID: userID, } db.Where(&result).First(&result) return result.LastActive } func DeleteAllLevel() { db.Delete(&PlayerLevel{}) } func SetXP(userID string, lastActive time.Time, xp int64) { db.Create(&PlayerLevel{ UserID: userID, LastActive: lastActive, XP: xp, }) }