-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
158 lines (124 loc) · 3.39 KB
/
store.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"database/sql"
"errors"
"fmt"
"log"
)
var AlreadyDraftedErr = errors.New("the player you selected was already drafted. pick again")
// Store interface contains all methods available for players
type Store interface {
DraftPlayer(id int) (int64, error)
Players() ([]*Player, error)
Player(id int) (*Player, error)
Reset() (int64, error)
Scout() ([]Player, error)
}
type dbStore struct {
db *sql.DB
}
// CreatePlayer is a function that creates a player and returns an error if there is one
func (store *dbStore) DraftPlayer(id int) (int64, error) {
// FIXME - sending back 0 for errors??
player, err := store.Player(id)
if err != nil {
return 0, err
}
fmt.Println("player is drafted: ", player.Drafted)
if player.Drafted == true {
err := AlreadyDraftedErr //errors.New("the player was already drafted. pick again")
fmt.Println("player already drafted error: ", err)
return 0, err
}
lastInsertID := 0
sqlStatement := `
UPDATE players
SET drafted = true
WHERE id = $1
RETURNING id`
e := store.db.QueryRow(sqlStatement, id).Scan(&lastInsertID)
switch {
case e == sql.ErrNoRows:
return 0, e
case e != nil:
fmt.Println(e)
return 0, e
default:
fmt.Printf("Player is %d\n ", lastInsertID)
return int64(lastInsertID), nil
}
}
// Player is a function that returns player for an id (if player exists)
// https://golang.org/src/database/sql/example_test.go
func (store *dbStore) Player(id int) (*Player, error) {
player := &Player{}
err := store.db.QueryRow("SELECT * FROM players WHERE id = $1", id).Scan(&player.ID, &player.Name, &player.School, &player.Position, &player.Drafted)
switch {
case err == sql.ErrNoRows:
fmt.Printf("No player with that ID.")
return nil, err
case err != nil:
fmt.Println(err)
return nil, err
default:
fmt.Printf("Player is %s\n", player)
return player, nil
}
}
// Players is a function that returns all players
func (store *dbStore) Players() ([]*Player, error) {
rows, err := store.db.Query("SELECT * FROM players")
if err != nil {
fmt.Println("Failed to run query", err)
return nil, err
}
defer rows.Close()
players := []*Player{}
for rows.Next() {
player := &Player{}
if err := rows.Scan(&player.ID, &player.Name, &player.School, &player.Position, &player.Drafted); err != nil {
log.Fatal(err)
}
players = append(players, player)
}
return players, nil
}
// Reset is a function that resets all drafted players to be undrafted
func (store *dbStore) Reset() (int64, error) {
sqlStatement := `
UPDATE players
SET drafted = false
WHERE drafted = true`
result, err := store.db.Exec(sqlStatement)
if err != nil {
return 0, err
}
rows, err := result.RowsAffected()
if err != nil {
return 0, err
}
return rows, nil
}
// Scout is a function that returns all players that have not been drafted
func (store *dbStore) Scout() ([]Player, error) {
rows, err := store.db.Query("SELECT * FROM players WHERE drafted = false")
if err != nil {
fmt.Println("Failed to run query", err)
return nil, err
}
defer rows.Close()
players := []Player{}
for rows.Next() {
player := &Player{}
if err := rows.Scan(&player.ID, &player.Name, &player.School, &player.Position, &player.Drafted); err != nil {
log.Fatal(err)
}
players = append(players, *player)
}
return players, nil
}
var store Store
// InitStore function to create new instance of the store
func InitStore(s Store) {
store = s
}