diff --git a/main.go b/main.go index 2a82cd8..c9c9ed4 100644 --- a/main.go +++ b/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)) } diff --git a/main_test.go b/main_test.go index 1f6c894..d6e0ee0 100644 --- a/main_test.go +++ b/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{ diff --git a/server.go b/server.go index 380b741..01758e2 100644 --- a/server.go +++ b/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) } diff --git a/store/memory.go b/store/memory.go index d277a20..09f0cee 100644 --- a/store/memory.go +++ b/store/memory.go @@ -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 } diff --git a/store/store.go b/store/store.go index 46c31c9..cc2b8f1 100644 --- a/store/store.go +++ b/store/store.go @@ -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 }