diff --git a/CHANGELOG.md b/CHANGELOG.md index c9c3a1d..b7af281 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Improved Tournament Generation - Support for `Max Rounds` in `X01` - Added `players` to match presets +- Default options when configuring matches - Lots of new badges #### Changed diff --git a/cmd/serve.go b/cmd/serve.go index 1c95559..0bfaeb9 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -94,6 +94,8 @@ var serveCmd = &cobra.Command{ router.HandleFunc("/preset/{id}", controllers.UpdatePreset).Methods("PUT") router.HandleFunc("/preset/{id}", controllers.DeletePreset).Methods("DELETE") + router.HandleFunc("/option/default", controllers.GetDefaultOptions).Methods("GET") + router.HandleFunc("/statistics/global", controllers.GetGlobalStatistics).Methods("GET") router.HandleFunc("/statistics/global/fnc", controllers.GetGlobalStatisticsFnc).Methods("GET") router.HandleFunc("/statistics/office/{from}/{to}", controllers.GetOfficeStatistics).Methods("GET") diff --git a/controllers/option_controller.go b/controllers/option_controller.go new file mode 100644 index 0000000..6edcee8 --- /dev/null +++ b/controllers/option_controller.go @@ -0,0 +1,21 @@ +package controllers + +import ( + "encoding/json" + "log" + "net/http" + + "github.com/kcapp/api/data" +) + +// GetDefaultOptions will return the default options +func GetDefaultOptions(w http.ResponseWriter, r *http.Request) { + SetHeaders(w) + offices, err := data.GetDefaultOptions() + if err != nil { + log.Println("Unable to get default options", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + json.NewEncoder(w).Encode(offices) +} diff --git a/data/option.go b/data/option.go new file mode 100644 index 0000000..e3ab27f --- /dev/null +++ b/data/option.go @@ -0,0 +1,36 @@ +package data + +import ( + "database/sql" + + "github.com/kcapp/api/models" +) + +// GetDefaultOptions will return default options +func GetDefaultOptions() (*models.DefaultOptions, error) { + opts := new(models.DefaultOptions) + opts.MatchType = new(models.MatchType) + opts.MatchMode = new(models.MatchMode) + opts.OutshotType = new(models.OutshotType) + + err := models.DB.QueryRow(` + SELECT + mt.id, mt.name, mt.description, + mm.id, mm.name, mm.short_name, mm.wins_required, mm.legs_required, + starting_score, max_rounds, ot.id, ot.name, ot.short_name + FROM match_default md + LEFT JOIN match_type mt ON mt.id = md.match_type_id + LEFT JOIN match_mode mm ON mm.id = md.match_mode_id + LEFT JOIN outshot_type ot ON ot.id = md.outshot_type_id + LIMIT 1`). + Scan(&opts.MatchType.ID, &opts.MatchType.Name, &opts.MatchType.Description, &opts.MatchMode.ID, &opts.MatchMode.Name, + &opts.MatchMode.ShortName, &opts.MatchMode.WinsRequired, &opts.MatchMode.LegsRequired, &opts.StartingScore, &opts.MaxRounds, + &opts.OutshotType.ID, &opts.OutshotType.Name, &opts.OutshotType.ShortName) + if err != nil { + if err == sql.ErrNoRows { + return opts, nil + } + return nil, err + } + return opts, nil +} diff --git a/models/option.go b/models/option.go new file mode 100644 index 0000000..b74a299 --- /dev/null +++ b/models/option.go @@ -0,0 +1,12 @@ +package models + +import "github.com/guregu/null" + +// DefaultOptions struct used for storing default options +type DefaultOptions struct { + MatchType *MatchType `json:"match_type"` + MatchMode *MatchMode `json:"match_mode"` + StartingScore int `json:"starting_score"` + MaxRounds null.Int `json:"max_rounds,omitempty"` + OutshotType *OutshotType `json:"outshot_type,omitempty"` +}