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

test db migrations, game edit form #2

Merged
merged 1 commit into from
Feb 14, 2020
Merged
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
13 changes: 0 additions & 13 deletions db/game_events.sql

This file was deleted.

37 changes: 37 additions & 0 deletions db/test/edits.sqlite.V6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
DROP VIEW IF EXISTS last_games;

CREATE VIEW last_games AS
SELECT
`g`.`id` AS `id`,
`g`.`game_date` AS `game_date`,
`p1`.`name` AS `team_1_d`,
`p1`.`id` AS `team_1_d_id`,
`p2`.`name` AS `team_1_o`,
`p2`.`id` AS `team_1_o_id`,
`p3`.`name` AS `team_2_d`,
`p3`.`id` AS `team_2_d_id`,
`p4`.`name` AS `team_2_o`,
`p4`.`id` AS `team_2_o_id`,
`g`.`team_1_half` AS `team_1_half`,
`g`.`team_2_half` AS `team_2_half`,
`g`.`team_1_final` AS `team_1_final`,
`g`.`team_2_final` AS `team_2_final`
FROM
(
(
(
(
`games` `g`
LEFT JOIN `players` `p1`
ON `p1`.`id` = `g`.`team_1_p1`
)
LEFT JOIN `players` `p2`
ON `p2`.`id` = `g`.`team_1_p2`
)
LEFT JOIN `players` `p3`
ON `p3`.`id` = `g`.`team_2_p1`
)
LEFT JOIN `players` `p4`
ON `p4`.`id` = `g`.`team_2_p2`
)
ORDER BY `g`.`game_date` DESC;
8 changes: 4 additions & 4 deletions db/event_types.sql → db/test/event_types.sqlite.V3.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CREATE TABLE event_types (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name CHAR(64) NOT NULL
CREATE TABLE `event_types` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` CHAR(64) NOT NULL
);

INSERT INTO event_types (id, name) VALUES
INSERT INTO `event_types` (`id`, `name`) VALUES
(1, 'bread'),
(2, 'reverse bread'),
(3, 'toast'),
Expand Down
13 changes: 13 additions & 0 deletions db/test/game_events.sqlite.V4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE `game_events` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`game` INTEGER NOT NULL,
`order_num` INTEGER NOT NULL,
`type` INTEGER NOT NULL,
`player_by` INTEGER NOT NULL,
`player_on` INTEGER NOT NULL,
`rating` INTEGER,
FOREIGN KEY(game) REFERENCES games(id),
FOREIGN KEY(type) REFERENCES event_types(id),
FOREIGN KEY(player_by) REFERENCES players(id),
FOREIGN KEY(player_on) REFERENCES players(id)
);
26 changes: 13 additions & 13 deletions db/games.sql → db/test/games.sqlite.V2.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS [games] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT,
[game_date] DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
[input_date] DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
[team_1_p1] INT NOT NULL,
[team_1_p2] INT NOT NULL,
[team_2_p1] INT NOT NULL,
[team_2_p2] INT NOT NULL,
[team_1_half] INT NOT NULL,
[team_1_final] INT NOT NULL,
[team_2_half] INT NOT NULL,
[team_2_final] INT NOT NULL,
[input_by] INT,
CREATE TABLE IF NOT EXISTS `games` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`game_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`input_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`team_1_p1` INT NOT NULL,
`team_1_p2` INT NOT NULL,
`team_2_p1` INT NOT NULL,
`team_2_p2` INT NOT NULL,
`team_1_half` INT NOT NULL,
`team_1_final` INT NOT NULL,
`team_2_half` INT NOT NULL,
`team_2_final` INT NOT NULL,
`input_by` INT,
FOREIGN KEY(team_1_p1) REFERENCES players(id),
FOREIGN KEY(team_1_p2) REFERENCES players(id),
FOREIGN KEY(team_2_p1) REFERENCES players(id),
Expand Down
14 changes: 7 additions & 7 deletions db/players.sql → db/test/players.sqlite.V1.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CREATE TABLE IF NOT EXISTS [players] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT,
[name] CHAR(64) NOT NULL,
[display_name] CHAR(16),
[email] CHAR(128),
[favorite_shot] CHAR(64)
CREATE TABLE IF NOT EXISTS `players` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` CHAR(64) NOT NULL,
`display_name` CHAR(16),
`email` CHAR(128),
`favorite_shot` CHAR(64)
);

INSERT INTO players (id, name, display_name, email, favorite_shot) VALUES
INSERT INTO `players` (`id`, `name`, `display_name`, `email`, `favorite_shot`) VALUES
(1, 'Evan White', 'king', 'evanwht1@gmail.com', 'pull'),
(2, 'Thomas Mckenna', 'number 2', '', 'push pass'),
(3, 'Zach Volz', 'zachonius', '', 'pull spin pass'),
Expand Down
Binary file renamed db/test.db → db/test/test.db
Binary file not shown.
File renamed without changes.
32 changes: 18 additions & 14 deletions gopages/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ package gopages

// Player from the db
type Player struct {
ID int
Name string
NickName string
ID int `json:"id"`
Name string `json:"name"`
NickName string `json:"display_name"`
}

type gameDBData struct {
ID int `json:"id"`
Date string `json:"game_date"`
T1pd string `json:"team_1_Defense"`
T1po string `json:"team_1_Offense"`
T2pd string `json:"team_2_Defense"`
T2po string `json:"team_2_Offense"`
T1half int `json:"team_1_half"`
T2half int `json:"team_2_half"`
T1final int `json:"team_1_final"`
T2final int `json:"team_2_final"`
ID int `json:"id"`
Date string `json:"game_date"`
T1pd string `json:"team_1_d"`
T1pdID string `json:"team_1_d_id"`
T1po string `json:"team_1_o"`
T1poID string `json:"team_1_o_id"`
T2pd string `json:"team_2_d"`
T2pdID string `json:"team_2_d_id"`
T2po string `json:"team_2_o"`
T2poID string `json:"team_2_o_id"`
T1half int `json:"team_1_half"`
T2half int `json:"team_2_half"`
T1final int `json:"team_1_final"`
T2final int `json:"team_2_final"`
}

type gameData struct {
gameDBData
Team1Class string
Team2Class string
}
}
2 changes: 1 addition & 1 deletion gopages/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CreatePlayerOptions(db *sqlx.DB) template.HTML {
func playersToOptions(players []Player) []string {
var options []string
for _, player := range players {
options = append(options, util.HTMLOption(strconv.Itoa(player.ID), player.Name))
options = append(options, util.HTMLOption(strconv.Itoa(player.ID), player.Name + "(" + player.NickName + ")"))
}
return options
}
Expand Down
71 changes: 32 additions & 39 deletions gopages/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"github.com/evanwht1/phoosball/util"
"github.com/jmoiron/sqlx"
"html/template"
Expand Down Expand Up @@ -32,7 +31,7 @@ func buildRow(gd gameDBData) (string, error) {
func getGames(db *sqlx.DB) *gamesInfo {
var tableRows []string
games := []gameDBData{}
err := db.Select(&games, "select id, DATE(game_date) as game_date, team_1_Defense, team_1_Offense, team_2_Defense, team_2_Offense, team_1_half, team_2_half, team_1_final, team_2_final from last_games;")
err := db.Select(&games, "select id, DATE(game_date) as game_date, team_1_d, team_1_d_id, team_1_o, team_1_o_id, team_2_d, team_2_d_id, team_2_o, team_2_o_id, team_1_half, team_2_half, team_1_final, team_2_final from last_games;")
if err != nil {
log.Fatal(err)
} else if len(games[0].Date) == 0 {
Expand Down Expand Up @@ -69,52 +68,46 @@ func RenderGamesPage(db *sqlx.DB, w http.ResponseWriter, r *http.Request) (templ
return template.HTML(buff.String()), nil
}

type gameEditData struct {
ID int
Date string
T1pd int
T1po int
T2pd int
T2po int
T1half int
T2half int
T1final int
T2final int
}

// SaveGameEdit : saves a PUT request to alter a games data
func SaveGameEdit(env *util.Env, w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
var t gameEditData
decoder := json.NewDecoder(r.Body)
var t gameData
err := decoder.Decode(&t)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
tx, err := env.DB.Begin()
var fail bool
if err != nil {
log.Fatal(err)
fail = true
updateGame := `UPDATE games SET
team_1_p1 = ?, team_1_p2 = ?, team_2_p1 = ?, team_2_p2 = ?,
team_1_half = ?, team_2_half = ?, team_1_final = ?, team_2_final = ?
WHERE id = ?;`
var res sql.Result
if t.T1final > t.T2final {
res, err = env.DB.Exec(updateGame, t.T1pd, t.T1po, t.T2pd, t.T2po, t.T1half, t.T2half, t.T1final, t.T2final, t.ID)
} else {
stmt, err := tx.Prepare(`UPDATE games SET
team_1_p1 = ?, team_1_p2 = ?, team_2_p1 = ?, team_2_p2 = ?,
team_1_half = ?, team_2_half = ?, team_1_final = ?, team_2_final = ?
WHERE id = ?;`)
if err != nil {
log.Println(err)
fail = true
} else {
var res sql.Result
if t.T1final > t.T2final {
res, err = stmt.Exec(t.T1pd, t.T1po, t.T2pd, t.T2po, t.T1half, t.T2half, t.T1final, t.T2final, t.ID)
} else {
res, err = stmt.Exec(t.T2pd, t.T2po, t.T1pd, t.T1po, t.T2half, t.T1half, t.T2final, t.T1final, t.ID)
}
if err != nil {
log.Println(err)
fail = true
}
rowCnt, err := res.RowsAffected()
if err != nil || rowCnt != 1 {
fail = true
}
}
if fail {
tx.Rollback()
http.Error(w, "Error", http.StatusInternalServerError)
} else {
tx.Commit()
fmt.Fprint(w, "Saved")
}
stmt.Close()
res, err = env.DB.Exec(updateGame, t.T2pd, t.T2po, t.T1pd, t.T1po, t.T2half, t.T1half, t.T2final, t.T1final, t.ID)
}
if err != nil {
log.Print(err)
http.Error(w, "SQL failed", http.StatusInternalServerError)
}
rowCnt, err := res.RowsAffected()
if err != nil || rowCnt != 1 {
http.Error(w, "Nothing updated", http.StatusInternalServerError)
}
}
} else {
Expand Down
28 changes: 10 additions & 18 deletions gopages/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,23 @@ import (
"bytes"
"github.com/jmoiron/sqlx"
"html/template"
"net/http"
"log"
"io/ioutil"
"log"
"net/http"

"github.com/evanwht1/phoosball/util"
)

// GetAllPlayers : gets all selectable players from the db
func GetAllPlayers(db *sqlx.DB) []Player {
var players []Player
rows, err := db.Query("select id, name, display_name from players;")
players := []Player{}
err := db.Select(&players, "select id, name, display_name from players;")
if err != nil {
log.Fatal(err)
} else {
defer rows.Close()
for rows.Next() {
var p Player
err := rows.Scan(&p.ID, &p.Name, &p.NickName)
if err != nil {
log.Fatal(err)
} else {
players = append(players, p)
}
for _, player := range players {
players = append(players, player)
}
rows.Close()
}
return players
}
Expand Down Expand Up @@ -70,7 +62,7 @@ func RenderPlayerPage(db *sqlx.DB, w http.ResponseWriter, r *http.Request) (temp
log.Println(err)
fail = true
} else {
res, err := stmt.Exec(r.PostFormValue("firstName") + " " + r.PostFormValue("lastName"), r.PostFormValue("nickName"), r.PostFormValue("email"))
res, err := stmt.Exec(r.PostFormValue("firstName")+" "+r.PostFormValue("lastName"), r.PostFormValue("nickName"), r.PostFormValue("email"))
if err != nil {
log.Println(err)
fail = true
Expand All @@ -97,13 +89,13 @@ func RenderPlayerPage(db *sqlx.DB, w http.ResponseWriter, r *http.Request) (temp
b, err := ioutil.ReadFile("webpage/player_input/fail_alert.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
AlertMessage = template.HTML(fallBackAlert)
AlertMessage = template.HTML(fallBackAlert)
}
AlertMessage = template.HTML(string(b))
AlertMessage = template.HTML(string(b))
}
b, err := ioutil.ReadFile("webpage/player_input/success_alert.html")
if err != nil {
AlertMessage = template.HTML(fallBackAlert)
AlertMessage = template.HTML(fallBackAlert)
}
AlertMessage = template.HTML(string(b))
}
Expand Down
Loading