/
shot.go
65 lines (56 loc) · 1.93 KB
/
shot.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
package models
import (
"errors"
"github.com/alex1sz/shotcharter-go-api/db"
)
type Shot struct {
ID string `db:"id" json:"id"`
Player Player `db:"player" json:"player,omitempty"`
Game Game `db:"game" json:"game,omitempty"`
Team Team `db:"team" json:"team,omitempty"`
PtValue uint8 `db:"pt_value" json:"pt_value"`
Made bool `db:"made" json:"made"`
XAxis uint64 `db:"x_axis" json:"x_axis"`
YAxis uint64 `db:"y_axis" json:"y_axis"`
CreatedAt string `db:"created_at" json:"created_at"`
UpdatedAt string `db:"updated_at" json:"updated_at"`
}
type omit *struct{}
type PublicShot struct {
*Shot
Game omit `json:"game,omitempty"`
Team omit `json:"team,omitempty"`
}
func (shot *Shot) Create() error {
if !shot.IsValid() {
return errors.New("Shot is invalid")
}
return db.Db.QueryRowx("INSERT INTO shots (player_id, game_id, team_id, pt_value, made, x_axis, y_axis) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id", shot.Player.ID, shot.Game.ID, shot.Team.ID, shot.PtValue, shot.Made, shot.XAxis, shot.YAxis).Scan(&shot.ID)
}
func (shot *Shot) Update() (err error) {
var result string
err = db.Db.QueryRowx(`UPDATE shots SET
player_id = ($2),
team_id = ($3),
pt_value = ($4),
made = ($5),
x_axis = ($6),
y_axis = ($7)
WHERE id = ($1) RETURNING id`, shot.ID, shot.Player.ID, shot.Team.ID, shot.PtValue, shot.Made, shot.XAxis, shot.YAxis).Scan(&result)
if result != shot.ID && err == nil {
return errors.New("Shot update failed: result not equal to shot.ID")
}
return
}
func (shot Shot) IsValid() (playerIsOnTeam bool) {
playerIsOnTeam, err := RowExists(`
SELECT 1 FROM players
WHERE EXISTS(
SELECT 1 FROM games
WHERE players.team_id = games.home_team_id OR players.team_id = games.away_team_id AND games.id = $3)
AND id=$1 AND team_id=$2`, shot.Player.ID, shot.Team.ID, shot.Game.ID)
if err != nil || !playerIsOnTeam {
return false
}
return true
}