From ef6bee998228bf02bd2f351ee62e9c582def695b Mon Sep 17 00:00:00 2001 From: DictumMortuum Date: Fri, 5 Apr 2024 20:08:23 +0300 Subject: [PATCH] [boardgames] added boardgames function to identify all options for a specific boardgame count --- cmd/servus-boardgames/db.go | 6 +++ cmd/servus-boardgames/main.go | 9 ++++- cmd/servus-boardgames/options.go | 63 ++++++++++++++++++++++++++++++++ pkg/middleware/http.go | 12 ++++++ pkg/model/map.go | 4 ++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 cmd/servus-boardgames/options.go diff --git a/cmd/servus-boardgames/db.go b/cmd/servus-boardgames/db.go index 4018a16..78986ae 100644 --- a/cmd/servus-boardgames/db.go +++ b/cmd/servus-boardgames/db.go @@ -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 { @@ -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, diff --git a/cmd/servus-boardgames/main.go b/cmd/servus-boardgames/main.go index 10b58d7..bbf4e9f 100644 --- a/cmd/servus-boardgames/main.go +++ b/cmd/servus-boardgames/main.go @@ -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) } @@ -57,5 +57,12 @@ func main() { middleware.Result, ) + g.GET( + "/options/:num", + middleware.Num, + adapter.A(GetPopularGamesForNum), + middleware.Result, + ) + r.Run(":10002") } diff --git a/cmd/servus-boardgames/options.go b/cmd/servus-boardgames/options.go new file mode 100644 index 0000000..a0c5272 --- /dev/null +++ b/cmd/servus-boardgames/options.go @@ -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 +} diff --git a/pkg/middleware/http.go b/pkg/middleware/http.go index 9dfc34d..5c3d2ae 100644 --- a/pkg/middleware/http.go +++ b/pkg/middleware/http.go @@ -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 { diff --git a/pkg/model/map.go b/pkg/model/map.go index 063591e..e0cea83 100644 --- a/pkg/model/map.go +++ b/pkg/model/map.go @@ -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)