Skip to content

Commit

Permalink
[boardgames] added boardgames function to identify all options for a …
Browse files Browse the repository at this point in the history
…specific boardgame count
  • Loading branch information
DictumMortuum committed Apr 5, 2024
1 parent e1194d9 commit ef6bee9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/servus-boardgames/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type Boardgame struct {
Families models.JsonArray `json:"families,omitempty"`
Weight models.JsonNullFloat64 `json:"weight,omitempty"`
Average models.JsonNullString `json:"average,omitempty"`
MinPlayers models.JsonNullInt64 `json:"min_players,omitempty"`
MaxPlayers models.JsonNullInt64 `json:"max_players,omitempty"`
LastPlayed time.Time `json:"last_played,omitempty"`
}

func GetPlayedGames(req *model.Map, res *model.Map) error {
Expand Down Expand Up @@ -57,6 +60,9 @@ func GetPlayedGames(req *model.Map, res *model.Map) error {
json_extract(g.bgg_data, '$.links.boardgamesubdomain') subdomains,
json_extract(g.bgg_data, '$.polls.boardgameweight.averageweight') weight,
json_extract(g.bgg_data, '$.stats.average') average,
g.min_players,
g.max_players,
max(p.date) last_played,
count(*) count
from
tboardgames g,
Expand Down
9 changes: 8 additions & 1 deletion cmd/servus-boardgames/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func Version(c *gin.Context) {
rs := map[string]any{
"version": "v0.0.6",
"version": "v0.0.7",
}
c.AbortWithStatusJSON(200, rs)
}
Expand Down Expand Up @@ -57,5 +57,12 @@ func main() {
middleware.Result,
)

g.GET(
"/options/:num",
middleware.Num,
adapter.A(GetPopularGamesForNum),
middleware.Result,
)

r.Run(":10002")
}
63 changes: 63 additions & 0 deletions cmd/servus-boardgames/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"

"github.com/DictumMortuum/servus-extapi/pkg/db"
"github.com/DictumMortuum/servus-extapi/pkg/model"
)

func GetPopularGamesForNum(req *model.Map, res *model.Map) error {
DB, err := req.GetDB()
if err != nil {
return err
}

num, err := req.GetInt64("num")
if err != nil {
return err
}

RDB, err := req.GetRedis()
if err != nil {
return err
}

rs := []Boardgame{}
err = db.CachedSelect(DB, RDB, fmt.Sprintf("GetPopularGamesForNum%d", num), &rs, fmt.Sprintf(`
select
g.id,
g.name,
g.rank,
g.square200,
json_extract(g.bgg_data, '$.links.boardgamemechanic') mechanics,
json_extract(g.bgg_data, '$.links.boardgamedesigner') designers,
json_extract(g.bgg_data, '$.links.boardgamecategory') categories,
json_extract(g.bgg_data, '$.links.boardgamefamily') families,
json_extract(g.bgg_data, '$.links.boardgamesubdomain') subdomains,
json_extract(g.bgg_data, '$.polls.boardgameweight.averageweight') weight,
json_extract(g.bgg_data, '$.stats.average') average,
g.min_players,
g.max_players,
max(p.date) last_played,
count(*) count
from
tboardgames g,
tboardgameplays p
where
g.id = p.boardgame_id and
g.min_players <= %d and
g.max_players >= %d %s
group by
1
order by
last_played, weight desc, count desc
`, num, num, db.YearConstraint(req, "and")))
if err != nil {
return err
}

res.Set("options", rs)

return nil
}
12 changes: 12 additions & 0 deletions pkg/middleware/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func Url(c *gin.Context) {
m.Set("url", c.Request.URL.String())
}

func Num(c *gin.Context) {
id := c.Param("num")

m, err := model.ToMap(c, "req")
if err != nil {
c.Error(err)
return
}

m.Set("num", id)
}

func Body(c *gin.Context) {
body, err := io.ReadAll(c.Request.Body)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/model/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (m *Map) Close() error {
return nil
}

func (m *Map) Unset(key string) {
delete(m.Internal, key)
}

func (m *Map) Set(key string, val any) {
if m.Internal == nil {
m.Internal = make(map[string]any)
Expand Down

0 comments on commit ef6bee9

Please sign in to comment.