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() {
|
||||
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))
|
||||
}
|
||||
|
|
11
main_test.go
11
main_test.go
|
@ -26,11 +26,6 @@ type StubQuestionsStore struct {
|
|||
}
|
||||
|
||||
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() {
|
||||
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{
|
||||
|
|
33
server.go
33
server.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -11,46 +10,38 @@ import (
|
|||
|
||||
const jsonContentType = "application/json"
|
||||
|
||||
type PlayerServer struct {
|
||||
store store.PlayerStore
|
||||
type TestHubCollectorServer struct {
|
||||
store store.TestHubCollectorStore
|
||||
http.Handler
|
||||
}
|
||||
|
||||
func NewPlayerServer(store store.PlayerStore) *PlayerServer {
|
||||
ps := new(PlayerServer)
|
||||
func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollectorServer {
|
||||
ps := new(TestHubCollectorServer)
|
||||
ps.store = store
|
||||
|
||||
router := http.NewServeMux()
|
||||
|
||||
router.Handle("/players/", http.HandlerFunc(ps.playersHandler))
|
||||
router.Handle("/league", http.HandlerFunc(ps.leagueHandler))
|
||||
router.Handle("/questions", http.HandlerFunc(ps.questionsHandler))
|
||||
// router.Handle("/league", http.HandlerFunc(ps.leagueHandler))
|
||||
|
||||
ps.Handler = router
|
||||
|
||||
return ps
|
||||
}
|
||||
|
||||
func (ps *PlayerServer) leagueHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("content-type", jsonContentType)
|
||||
json.NewEncoder(w).Encode(ps.store.GetLeague())
|
||||
}
|
||||
|
||||
func (ps *PlayerServer) playersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
player := strings.TrimPrefix(r.URL.Path, "/players/")
|
||||
func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
player := strings.TrimPrefix(r.URL.Path, "/questions")
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
score := ps.store.GetPlayerScore(player)
|
||||
if score == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
fmt.Fprint(w, score)
|
||||
questions := ps.store.GetQuestions()
|
||||
fmt.Fprint(w, questions)
|
||||
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)
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
|
|
@ -6,40 +6,30 @@ import (
|
|||
"git.andreafazzi.eu/andrea/testhub/models"
|
||||
)
|
||||
|
||||
type MemoryPlayerStore struct {
|
||||
store map[string]int
|
||||
type MemoryTestHubCollectorStore struct {
|
||||
// store map[string]int
|
||||
|
||||
// A mutex is used to synchronize read/write access to the map
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryPlayerStore() *MemoryPlayerStore {
|
||||
s := new(MemoryPlayerStore)
|
||||
s.store = make(map[string]int, 0)
|
||||
func NewMemoryTestHubCollectorStore() *MemoryTestHubCollectorStore {
|
||||
s := new(MemoryTestHubCollectorStore)
|
||||
// s.store = make(map[string]int, 0)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *MemoryPlayerStore) GetPlayerScore(player string) int {
|
||||
func (s *MemoryTestHubCollectorStore) GetQuestions() []*models.Question {
|
||||
s.lock.RLock()
|
||||
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()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
s.store[player]++
|
||||
}
|
||||
|
||||
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
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"git.andreafazzi.eu/andrea/testhub/models"
|
||||
)
|
||||
|
||||
type QuestionsStore interface {
|
||||
type TestHubCollectorStore interface {
|
||||
GetQuestions() []*models.Question
|
||||
CreateQuestion(question *models.Question) *models.Question
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue