-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
104 lines (83 loc) · 2.49 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
package main
import (
"database/sql"
"embed"
"log"
_ "modernc.org/sqlite"
"github.com/labstack/echo/v4"
api "github.com/tarow/skat-counter/internal/api"
"github.com/tarow/skat-counter/internal/skat"
)
//go:embed static/*
var staticAssets embed.FS
func main() {
db, err := sql.Open("sqlite", "skat.sqlite")
createTables(db)
if err != nil {
log.Fatal(err)
}
defer db.Close()
e := echo.New()
handler := api.NewHandler(skat.NewService(db))
registerRoutes(e, handler)
e.Logger.Fatal(e.Start(":8080"))
}
type Form struct {
Playernames []string `form:"playername"`
}
func registerRoutes(e *echo.Echo, handler api.Handler) {
e.StaticFS("/static", echo.MustSubFS(staticAssets, "static"))
e.GET("/", handler.GetIndex)
e.GET("/games", handler.GetGameList)
e.POST("/games", handler.CreateGame)
e.GET("/games/create", handler.GetCreateGameForm)
e.GET("/games/:id", handler.GetGameDetails)
e.GET("/games/:id/edit", handler.GetEditGameForm)
e.PUT("/games/:id/edit", handler.EditGame)
e.DELETE("/games/:id", handler.DeleteGame)
e.POST("/games/:id/rounds", handler.AddRound)
e.GET("/games/:gameid/rounds/:roundid", handler.GetEditRoundForm)
e.PUT("/games/:gameid/rounds/:roundid", handler.EditRound)
e.DELETE("/games/:gameid/rounds/:roundid", handler.DeleteRound)
}
func createTables(db *sql.DB) error {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS player (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS game (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
created_at TIMESTAMP NOT NULL,
stake REAL NOT NULL,
online BOOLEAN NOT NULL
);
CREATE TABLE IF NOT EXISTS game_player (
game_id INTEGER NOT NULL,
player_id INTEGER NOT NULL,
rank INTEGER NOT NULL,
FOREIGN KEY(game_id) REFERENCES games(id),
FOREIGN KEY(player_id) REFERENCES players(id)
);
CREATE TABLE IF NOT EXISTS round (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
game_id INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL,
dealer INTEGER,
declarer INTEGER NOT NULL,
won BOOLEAN NOT NULL,
value INTEGER NOT NULL,
FOREIGN KEY(game_id) REFERENCES games(id),
FOREIGN KEY(dealer) REFERENCES players(id),
FOREIGN KEY(declarer) REFERENCES players(id)
);
CREATE TABLE IF NOT EXISTS round_opponent (
round_id INTEGER NOT NULL,
player_id INTEGER NOT NULL,
PRIMARY KEY (round_id, player_id),
FOREIGN KEY(round_id) REFERENCES rounds(id),
FOREIGN KEY(player_id) REFERENCES players(id)
);
`)
return err
}