-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (154 loc) · 4.43 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"nfl_draft/utils"
"os"
"strconv"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
// Player : struct for players
type Player struct {
ID int64 `json:"id"`
Name string `json:"name" sql:"full_name"`
School string `json:"school"`
Position string `json:"position"`
Drafted bool `json:"drafted"`
}
func (p Player) String() string {
return fmt.Sprintf("Player<ID=%d Name=%q>", p.ID, p.Name)
}
func main() {
log.Println("Starting server...")
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
sqlUser := os.Getenv("SQL_USER")
sqlDbName := os.Getenv("DB_NAME")
sqlPW := os.Getenv("SQL_PW")
var connStr string
if sqlPW != "" {
connStr = fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", sqlUser, sqlPW, sqlDbName)
} else {
connStr = fmt.Sprintf("user=%s dbname=%s sslmode=disable", sqlUser, sqlDbName)
}
log.Println("sqlUser: ", sqlUser)
log.Println("sqlDbName: ", sqlDbName)
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
InitStore(&dbStore{db: db})
fs := http.StripPrefix("/files", http.FileServer(http.Dir("./files")))
http.Handle("/files/", fs)
http.HandleFunc("/players/", playerHandler) // TODO: add param to get non-drafted players?? (get rid of /scouting route)
http.HandleFunc("/", index)
http.HandleFunc("/test", test)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func index(w http.ResponseWriter, req *http.Request) {
utils.EnableCors(&w)
utils.RespondWithJSON(w, http.StatusOK, map[string]string{"index": "success"})
}
func test(w http.ResponseWriter, req *http.Request) {
utils.EnableCors(&w)
utils.RespondWithJSON(w, http.StatusOK, map[string]string{"test": "success"})
}
func playerHandler(w http.ResponseWriter, req *http.Request) {
utils.EnableCors(&w)
_, rest := utils.ShiftPath(req.URL.Path) // to take out "players" from the path
head, _ := utils.ShiftPath(rest)
var pID string
if head != "" {
id, err := strconv.Atoi(head)
if err == nil {
pID = strconv.Itoa(id)
head = "id" // hacky solution ...
}
switch head {
case "reset":
num, err := store.Reset()
if err != nil {
msg := "Could not reset the players to be undrafted"
utils.RespondWithError(w, http.StatusInternalServerError, msg)
return
}
utils.RespondWithJSON(w, http.StatusOK, num)
return
case "id":
switch req.Method {
case "GET":
log.Println("getting player with id: ", pID)
p, err := store.Player(id)
if err != nil {
msg := "Could not retrieve player with id: " + pID
utils.RespondWithError(w, http.StatusNotFound, msg)
return
}
utils.RespondWithJSON(w, http.StatusOK, p)
return
case "POST":
log.Println("trying to draft player:", pID)
playerID, err := store.DraftPlayer(id)
switch {
case err == AlreadyDraftedErr:
msg := (AlreadyDraftedErr).Error()
log.Printf("Already drafted err. %s", msg)
utils.RespondWithError(w, http.StatusNotFound, msg)
return
case playerID == 0:
log.Println("did not find player")
msg := "Player with id: " + pID + " does not exist."
utils.RespondWithJSON(w, http.StatusNotFound, msg)
return
case err != nil:
msg := "Could not draft player with id: " + pID
utils.RespondWithError(w, http.StatusNotFound, msg)
return
default:
msg := "Congrats, you have successfully drafted player: " + pID
utils.RespondWithJSON(w, http.StatusOK, msg)
return
}
default:
msg := "Method not allowed"
utils.RespondWithError(w, http.StatusMethodNotAllowed, msg)
return
}
default:
utils.RespondWithError(w, http.StatusNotFound, "Not found")
return
}
}
switch req.Method {
case "GET":
// TODO: implement sorting
// query := url.Query()
// sort := query.Get("sort")
// dir := query.Get("dir")
// fmt.Printf("sort: %s , dir: %s \n", sort, dir)
log.Println("returning all players")
players, err := store.Players()
if err != nil {
msg := "Could not retrieve players"
utils.RespondWithError(w, http.StatusNotFound, msg)
return
}
utils.RespondWithJSON(w, http.StatusOK, players)
return
case "POST":
// TODO - make route to create new players
msg := "Desired method not yet implemented"
utils.RespondWithError(w, http.StatusNotImplemented, msg)
return
default:
msg := "Method not allowed"
utils.RespondWithError(w, http.StatusMethodNotAllowed, msg)
return
}
}