Migrate models
This commit is contained in:
parent
b09c8fae85
commit
a9f8a4474f
5 changed files with 28 additions and 50 deletions
4
main.go
4
main.go
|
@ -13,8 +13,8 @@ const port = "3000"
|
||||||
func main() {
|
func main() {
|
||||||
logger.SetLevel(logger.DebugLevel)
|
logger.SetLevel(logger.DebugLevel)
|
||||||
|
|
||||||
server := NewPlayerServer(store.NewMemoryPlayerStore())
|
server := NewTestHubCollectorServer(store.NewMemoryTestHubCollectorStore())
|
||||||
|
|
||||||
logger.Infof("The API server is listening to port %s...", port)
|
logger.Infof("TestHub Collector server is listening to port %s...", port)
|
||||||
log.Fatal(http.ListenAndServe(":"+port, server))
|
log.Fatal(http.ListenAndServe(":"+port, server))
|
||||||
}
|
}
|
||||||
|
|
11
main_test.go
11
main_test.go
|
@ -26,11 +26,6 @@ type StubQuestionsStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *StubQuestionsStore) CreateQuestion(question *models.Question) *models.Question {
|
func (store *StubQuestionsStore) CreateQuestion(question *models.Question) *models.Question {
|
||||||
id := store.lastQuestionID++
|
|
||||||
|
|
||||||
for _, answer := range question.Answers {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +42,10 @@ func TestRunner(t *testing.T) {
|
||||||
|
|
||||||
func (t *testSuite) TestGETQuestions() {
|
func (t *testSuite) TestGETQuestions() {
|
||||||
expectedResult := []*models.Question{
|
expectedResult := []*models.Question{
|
||||||
&{Name: "Cleo", Wins: 20},
|
&{
|
||||||
&{Name: "Chris", Wins: 10},
|
Text: "Domanda 1",
|
||||||
|
Answers: []*models.Answers{&{Text: "Option 1"},&{Text: "Option 2"}, &{Text: "Option 3"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
store := &StubPlayerStore{
|
store := &StubPlayerStore{
|
||||||
|
|
33
server.go
33
server.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -11,46 +10,38 @@ import (
|
||||||
|
|
||||||
const jsonContentType = "application/json"
|
const jsonContentType = "application/json"
|
||||||
|
|
||||||
type PlayerServer struct {
|
type TestHubCollectorServer struct {
|
||||||
store store.PlayerStore
|
store store.TestHubCollectorStore
|
||||||
http.Handler
|
http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlayerServer(store store.PlayerStore) *PlayerServer {
|
func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollectorServer {
|
||||||
ps := new(PlayerServer)
|
ps := new(TestHubCollectorServer)
|
||||||
ps.store = store
|
ps.store = store
|
||||||
|
|
||||||
router := http.NewServeMux()
|
router := http.NewServeMux()
|
||||||
|
|
||||||
router.Handle("/players/", http.HandlerFunc(ps.playersHandler))
|
router.Handle("/questions", http.HandlerFunc(ps.questionsHandler))
|
||||||
router.Handle("/league", http.HandlerFunc(ps.leagueHandler))
|
// router.Handle("/league", http.HandlerFunc(ps.leagueHandler))
|
||||||
|
|
||||||
ps.Handler = router
|
ps.Handler = router
|
||||||
|
|
||||||
return ps
|
return ps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
|
func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("content-type", jsonContentType)
|
player := strings.TrimPrefix(r.URL.Path, "/questions")
|
||||||
json.NewEncoder(w).Encode(ps.store.GetLeague())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ps *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
player := strings.TrimPrefix(r.URL.Path, "/players/")
|
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
score := ps.store.GetPlayerScore(player)
|
questions := ps.store.GetQuestions()
|
||||||
if score == 0 {
|
fmt.Fprint(w, questions)
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
}
|
|
||||||
fmt.Fprint(w, score)
|
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
ps.processWin(w, player)
|
ps.createQuestion(w, player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *PlayerServer) processWin(w http.ResponseWriter, player string) {
|
func (ps *TestHubCollectorServer) processWin(w http.ResponseWriter, player string) {
|
||||||
ps.store.RecordWin(player)
|
ps.store.RecordWin(player)
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,40 +6,30 @@ import (
|
||||||
"git.andreafazzi.eu/andrea/testhub/models"
|
"git.andreafazzi.eu/andrea/testhub/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MemoryPlayerStore struct {
|
type MemoryTestHubCollectorStore struct {
|
||||||
store map[string]int
|
// store map[string]int
|
||||||
|
|
||||||
// A mutex is used to synchronize read/write access to the map
|
// A mutex is used to synchronize read/write access to the map
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMemoryPlayerStore() *MemoryPlayerStore {
|
func NewMemoryTestHubCollectorStore() *MemoryTestHubCollectorStore {
|
||||||
s := new(MemoryPlayerStore)
|
s := new(MemoryTestHubCollectorStore)
|
||||||
s.store = make(map[string]int, 0)
|
// s.store = make(map[string]int, 0)
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MemoryPlayerStore) GetPlayerScore(player string) int {
|
func (s *MemoryTestHubCollectorStore) GetQuestions() []*models.Question {
|
||||||
s.lock.RLock()
|
s.lock.RLock()
|
||||||
defer s.lock.RUnlock()
|
defer s.lock.RUnlock()
|
||||||
|
|
||||||
return s.store[player]
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MemoryPlayerStore) RecordWin(player string) {
|
func (s *MemoryTestHubCollectorStore) CreateQuestion(question *models.Question) *models.Question {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
s.store[player]++
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
func (s *MemoryPlayerStore) GetLeague() []models.Player {
|
|
||||||
var result []models.Player
|
|
||||||
|
|
||||||
for player, wins := range s.store {
|
|
||||||
result = append(result, models.Player{Name: player, Wins: wins})
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"git.andreafazzi.eu/andrea/testhub/models"
|
"git.andreafazzi.eu/andrea/testhub/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type QuestionsStore interface {
|
type TestHubCollectorStore interface {
|
||||||
GetQuestions() []*models.Question
|
GetQuestions() []*models.Question
|
||||||
CreateQuestion(question *models.Question) *models.Question
|
CreateQuestion(question *models.Question) *models.Question
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue