generated from Fs02/go-todo-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
50 lines (40 loc) · 841 Bytes
/
score.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
package handler
import (
"net/http"
"github.com/ddanurwenda/relecho/scores"
"github.com/go-chi/chi"
"github.com/go-rel/rel"
)
// Score for score endpoints.
type Score struct {
*chi.Mux
repository rel.Repository
}
// Index handle GET /
func (s Score) Index(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
result scores.Score
)
s.repository.Find(ctx, &result)
render(w, result, 200)
}
// Points handle Get /points
func (s Score) Points(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
result []scores.Point
)
s.repository.FindAll(ctx, &result)
render(w, result, 200)
}
// NewScore handler.
func NewScore(repository rel.Repository) Score {
h := Score{
Mux: chi.NewMux(),
repository: repository,
}
h.Get("/", h.Index)
h.Get("/points", h.Points)
return h
}