-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.go
53 lines (48 loc) · 1.06 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"gorm.io/gorm"
"log"
"time"
)
type Sample struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
Team int
Points uint64
}
func initTables(db *gorm.DB) error {
err := db.AutoMigrate(&Sample{})
if err != nil {
return err
}
return nil
}
func getMaxPointsPerTeam(db *gorm.DB) map[int]uint64 {
rows, err := db.Raw("SELECT team, MAX(points) FROM samples GROUP BY team").Rows()
if err != nil {
panic(err) // TODO: Change this?
}
teamPoints := make(map[int]uint64)
var team int
var points uint64
for rows.Next() {
err = rows.Scan(&team, &points)
if err != nil {
panic(err) // TODO: Change this?
}
teamPoints[team] = points
}
return teamPoints
}
func updateMaxPointsPerTeam(db *gorm.DB, teamPoints map[int]uint64) {
var samples []*Sample
for team, points := range teamPoints {
samples = append(samples, &Sample{Team: team, Points: points})
}
result := db.Create(samples)
if result.Error != nil {
panic(result.Error)
}
rowsAffected := result.RowsAffected
log.Printf("Rows affected: %d\n", rowsAffected)
}