Skip to content

Commit

Permalink
feat: use sqlite as data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarow committed Dec 26, 2023
1 parent c21b774 commit 437127c
Show file tree
Hide file tree
Showing 28 changed files with 1,227 additions and 126 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ config.yaml

# Compiled binaries
bin/
tmp/
tmp/

## DB files
*.sqlite
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"sqltools.useNodeRuntime": true,
"sqltools.connections": [
{
"previewLimit": 50,
"driver": "SQLite",
"database": "skat.sqlite",
"name": "skat"
}
]
}
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ TAGS := latest

all: clean install gen tidy build

run:
run: gen
go run main.go

dev:
air

build:
gen:
templ generate
jet -source=sqlite -dsn="./skat.sqlite" -schema=games -path=./internal/skat/gen

build: gen
go build -o bin/$(BINARY_NAME) main.go

clean:
Expand Down
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,38 @@ go 1.21.5

require (
github.com/a-h/templ v0.2.476
github.com/google/uuid v1.5.0
github.com/go-jet/jet/v2 v2.10.1
github.com/labstack/echo/v4 v4.11.4
modernc.org/sqlite v1.28.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.29.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
)
245 changes: 245 additions & 0 deletions go.sum

Large diffs are not rendered by default.

101 changes: 78 additions & 23 deletions internal/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/a-h/templ"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/tarow/skat-counter/internal/skat"
"github.com/tarow/skat-counter/internal/skat/gen/model"
template "github.com/tarow/skat-counter/templates"
"github.com/tarow/skat-counter/templates/components"
)

type Handler struct {
Expand All @@ -24,7 +26,12 @@ func NewHandler(service skat.Service) Handler {
}

func (h Handler) GetIndex(c echo.Context) error {
index := template.GameOverview(h.service.List())
games, err := h.service.List()
if err != nil {
c.Logger().Error(err)
return err
}
index := template.GameOverview(games)
return render(c, http.StatusOK, index)
}

Expand All @@ -33,71 +40,117 @@ func (h Handler) GetGameDetails(c echo.Context) error {

parsedId, err := strconv.Atoi(gameId)
if err != nil {
c.Logger().Error(err)
return err
}

game := h.service.Find(parsedId)
game, err := h.service.Find(int32(parsedId))
if err != nil {
c.Logger().Error(err)
return err
}
fmt.Printf("%+v", game)

gameDetails := template.GameDetails(*game)

return render(c, http.StatusOK, gameDetails)
}

func (h Handler) CreateGame(c echo.Context) error {
createGameForm := struct {
Players []string `form:"player"`
Online bool `form:"online"`
Stake float32 `form:"stake"`
}{}

err := c.Bind(&createGameForm)
if err != nil {
c.Logger().Error(err)
return err
}

players := []model.Player{}
for _, p := range createGameForm.Players {
players = append(players, model.Player{Name: p})
}
g := skat.Game{}
c.Bind(&g)
fmt.Println("received game", g)
g = h.service.Create(g)
fmt.Println("created game", g)
g.Players = players

g.Online = true
g.Stake = 1.5
g.CreatedAt = time.Now()

g, err = h.service.Create(g)
if err != nil {
return err
}

details := template.GameDetails(g)

c.Response().Header().Set("Access-Control-Expose-Headers", "*")
//c.Response().Header().Set("Access-Control-Allow-Origin", "*")
//c.Response().Header().Set("Access-Control-Allow-Headers", "*")
c.Response().Header().Set("HX-Push-Url", fmt.Sprintf("/games/%v", g.Id))
c.Response().Header().Set("HX-Push-Url", fmt.Sprintf("/games/%v", g.ID))

return render(c, http.StatusCreated, details)
}

func (h Handler) DeleteGame(c echo.Context) error {
gameId := c.Param("id")

parsedId, err := strconv.Atoi(gameId)
if err != nil {
return err
}

err = h.service.Delete(int32(parsedId))
if err != nil {
c.Logger().Error(err)
return err
}

return nil
}

func (h Handler) AddRound(c echo.Context) error {
gameId := c.Param("id")
parsedId, err := strconv.Atoi(gameId)
if err != nil {
c.Logger().Error(err)
return err
}

game := h.service.Find(parsedId)
game, err := h.service.Find(int32(parsedId))
if err != nil {
c.Logger().Error(err)
return err
}
fmt.Printf("rounds before: %+v", len(game.Rounds))

var params map[string]string = make(map[string]string)
c.Bind(&params)

round := skat.Round{}
round.GameID = game.ID
round.CreatedAt = time.Now()
for _, player := range game.Players {
role, exists := params[player]
role, exists := params[player.Name]
if !exists {
continue
}

switch role {
case "declarer":
round.Declarer = player
round.Declarer = player.ID
case "opponent":
round.Opponents = append(round.Opponents, player)
round.Opponents = append(round.Opponents, player.ID)
case "dealer":
round.Dealer = player
round.Dealer = &player.ID
}
}

wonStr, exists := params["won"]
if exists {
won, err := strconv.ParseBool(wonStr)
if err != nil {
c.Logger().Error(err)
return err
}
round.Won = won
Expand All @@ -107,27 +160,29 @@ func (h Handler) AddRound(c echo.Context) error {
if exists {
gameValue, err := strconv.Atoi(gameValueStr)
if err != nil {
c.Logger().Error(err)
return err
}
round.Value = gameValue
round.Value = int32(gameValue)
}

round, err = h.service.AddRound(game.ID, round)
if err != nil {
log.Error(err)
return err
}

game.Rounds = append(game.Rounds, round)
fmt.Printf("rounds after: %+v", len(game.Rounds))
gameDetails := template.GameDetails(*game)
return render(c, http.StatusCreated, gameDetails)
}

func (h Handler) GetCreateGameForm(c echo.Context) error {
form := components.CreateGameForm()
return render(c, http.StatusOK, form)
}

func render(ctx echo.Context, status int, t templ.Component) error {
ctx.Response().Writer.WriteHeader(status)

err := t.Render(context.Background(), ctx.Response().Writer)
if err != nil {
ctx.Logger().Error(err)
return ctx.String(http.StatusInternalServerError, "failed to render response template")
}

Expand Down
19 changes: 19 additions & 0 deletions internal/skat/gen/model/game.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/skat/gen/model/game_player.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/skat/gen/model/player.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/skat/gen/model/round.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/skat/gen/model/round_opponent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 437127c

Please sign in to comment.