Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The big refactor #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 10 additions & 16 deletions api/common/base.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package common

import (
"database/sql"
"github.com/onestay/MarathonTools-API/marathon"
"net/http"

"github.com/go-redis/redis"
"gopkg.in/mgo.v2"

"github.com/onestay/MarathonTools-API/api/models"
"github.com/onestay/MarathonTools-API/ws"
)

// Controller is the base struct for any controller. It's used to manage state and other things.
type Controller struct {
WS *ws.Hub
MGS *mgo.Session
Col *mgo.Collection
RunIndex int
CurrentRun *models.Run
NextRun *models.Run
PrevRun *models.Run
UpNext *models.Run
RedisClient *redis.Client
TimerState TimerState
TimerTime float64
HTTPClient http.Client
Marathon marathon.Marathon
// SocialUpdatesChan is used to communicate with the socialController on Twitter and twitch updates
SocialUpdatesChan chan int
CL *Checklist
Settings *SettingsProvider
db *sql.DB
}

type httpResponse struct {
Expand Down Expand Up @@ -55,26 +51,24 @@ const (
)

// NewController returns a new base controller
func NewController(hub *ws.Hub, mgs *mgo.Session, crIndex int, rc *redis.Client) *Controller {
var runs []models.Run
err := mgs.DB("marathon").C("runs").Find(nil).All(&runs)
func NewController(hub *ws.Hub, mgs *mgo.Session, crIndex int, rc *redis.Client) (*Controller, error) {
db, err := sql.Open("sqlite3", "../../db/test.sqlite")
if err != nil {
return nil
return nil, err
}

c := &Controller{
WS: hub,
MGS: mgs,
RunIndex: crIndex,
Col: mgs.DB("marathon").C("runs"),
RedisClient: rc,
TimerState: 2,
TimerTime: 0,
HTTPClient: http.Client{},
SocialUpdatesChan: make(chan int, 1),
db: db,
}
c.CL = NewChecklist(c)
c.Settings = InitSettings(c)
c.UpdateActiveRuns()
c.UpdateUpNext()
return c
return c, nil
}
10 changes: 0 additions & 10 deletions api/models/marathon.go

This file was deleted.

42 changes: 42 additions & 0 deletions api/models/player.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package models

import "database/sql"

type PlayerInfo struct {
Id int64 `json:"id,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Country string `json:"country,omitempty"`
TwitterName string `json:"twitterName,omitempty"`
TwitchName string `json:"twitchName,omitempty"`
YoutubeName string `json:"youtubeName,omitempty"`
}

func AddPlayer(player PlayerInfo, db *sql.DB) (int64, error) {
stmt, err := db.Prepare("INSERT INTO players(display_name, country, twitter_name, twitch_name, youtube_name) VALUES (?, ?, ?, ?, ?)")
if err != nil {
return 0, err
}
defer stmt.Close()

result, err := stmt.Exec(player.DisplayName, player.Country, player.TwitterName, player.TwitchName, player.YoutubeName)
if err != nil {
return 0, err
}

id, err := result.LastInsertId()
if err != nil {
return 0, err
}

return id, nil
}

func GetPlayerById(id int64, db *sql.DB) (*PlayerInfo, error) {
var player PlayerInfo
err := db.QueryRow("SELECT * FROM players WHERE id=?", id).Scan(&player.Id, &player.DisplayName, &player.Country, &player.TwitterName, &player.TwitchName, &player.YoutubeName)
if err != nil {
return nil, err
}

return &player, nil
}
45 changes: 45 additions & 0 deletions api/models/player_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package models

import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"testing"
)

func TestPlayer(t *testing.T) {
db, err := sql.Open("sqlite3", "../../db/test.sqlite")
if err != nil {
t.Fail()
}

for i := 0; i < 10; i++ {
p := PlayerInfo{
Id: 0,
DisplayName: fmt.Sprintf("onestay%d", i),
Country: "de",
TwitterName: "@onest4y",
TwitchName: "onestay",
YoutubeName: "",
}

_, err = AddPlayer(p, db)
if err != nil {
panic(err)
}
}

player, err := GetPlayerById(1, db)
if err != nil {
panic(err)
}

if player.DisplayName != "onestay0" {
t.Errorf("Expected DisplayName to be %s but got %s", "onestay0", player.DisplayName)
}

_, err = GetPlayerById(10000, db)
if err == nil {
t.Errorf("Expected GetPlayerById with id 10000 to fail")
}
}
Loading