/
recent_solutions.go
67 lines (59 loc) · 1.75 KB
/
recent_solutions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package routes
import (
"net/http"
"time"
"github.com/code-golf/code-golf/config"
"github.com/code-golf/code-golf/null"
"github.com/code-golf/code-golf/pager"
"github.com/code-golf/code-golf/session"
)
// GET /recent/solutions/{hole}/{lang}/{scoring}
func recentSolutionsGET(w http.ResponseWriter, r *http.Request) {
type row struct {
Country config.NullCountry
Name string
Hole *config.Hole
Lang *config.Lang
Rank, Strokes, TieCount int
Submitted time.Time
}
data := struct {
HoleID, LangID, Scoring string
LangsShown map[string]bool
Pager *pager.Pager
Rows []row
}{
HoleID: param(r, "hole"),
LangID: param(r, "lang"),
LangsShown: map[string]bool{},
Pager: pager.New(r),
Rows: make([]row, 0, pager.PerPage),
Scoring: param(r, "scoring"),
}
if data.HoleID != "all" && config.HoleByID[data.HoleID] == nil ||
data.LangID != "all" && config.LangByID[data.LangID] == nil ||
data.Scoring != "chars" && data.Scoring != "bytes" {
w.WriteHeader(http.StatusNotFound)
return
}
if err := session.Database(r).Select(
&data.Rows,
` SELECT hole, lang, login name, strokes, rank, submitted, tie_count
FROM rankings
JOIN users ON user_id = id
WHERE (hole = $1 OR $1 IS NULL)
AND (lang = $2 OR $2 IS NULL)
AND scoring = $3
ORDER BY submitted DESC LIMIT $4`,
null.New(data.HoleID, data.HoleID != "all"),
null.New(data.LangID, data.LangID != "all"),
data.Scoring,
pager.PerPage,
); err != nil {
panic(err)
}
for _, row := range data.Rows {
data.LangsShown[row.Lang.ID] = true
}
render(w, r, "recent/solutions", data, "Recent Solutions")
}