-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable default options for matches
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"` | ||
} |