Browse Source

Add board query by id support

master
NGnius (Graham) 4 years ago
parent
commit
4fcc9ec149
3 changed files with 20 additions and 3 deletions
  1. +13
    -2
      handlers.go
  2. +6
    -0
      sql_service.go
  3. +1
    -1
      sql_structs.go

+ 13
- 2
handlers.go View File

@@ -33,16 +33,20 @@ func boardHandler(w http.ResponseWriter, r *http.Request) {
}
args := r.URL.Query()
// check args pre-conditions
if !(checkArgExists(args, "board", w) || checkArgExists(args, "name", w)) {
if !(checkArgExists(args, "board", w) || checkArgExists(args, "name", w) || (checkArgExists(args, "id", w) && checkArgInt(args, "id", w, 0))) {
errorResponse(400, "Missing required 'board' URL parameter", w, r)
return
}
board := ""
var boardId int = -1
if checkArgExists(args, "name", w) {
board = args.Get("name")
}
if checkArgExists(args, "board", w) {
board = args.Get("board")
}
if checkArgExists(args, "id", w) {
boardId, _ = strconv.Atoi(args.Get("id"))
}
if !checkArgExists(args, "count", w) || !checkArgInt(args, "count", w, 0) {
//w.WriteHeader(400)
@@ -58,7 +62,14 @@ func boardHandler(w http.ResponseWriter, r *http.Request) {
start, _ := strconv.Atoi(args.Get("start"))
// execute query
result := NewResult("", r.URL.String())
b, err := boardByName(board)
var b *Board
var err error
if (boardId == -1) {
b, err = boardByName(board)
} else {
b, err = boardById(int64(boardId))
}
//b, ok := boards[board]
if err != nil {
fmt.Println(err)


+ 6
- 0
sql_service.go View File

@@ -86,6 +86,7 @@ func sqlInit() error {
"CREATE TABLE IF NOT EXISTS Boards (id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL);",
"CREATE TABLE IF NOT EXISTS Entries (id BIGSERIAL PRIMARY KEY, rank INTEGER NOT NULL DEFAULT -1, score BIGINT NOT NULL, player BIGSERIAL NOT NULL, board BIGSERIAL NOT NULL, time INTEGER NOT NULL, metadata BYTEA NOT NULL DEFAULT E'\\\\xFF', FOREIGN KEY(player) REFERENCES Players(id), FOREIGN KEY(board) REFERENCES Boards(id));",
"CREATE TABLE IF NOT EXISTS Keys (id BIGSERIAL PRIMARY KEY, token TEXT NOT NULL, player BIGSERIAL NOT NULL, time BIGSERIAL NOT NULL, metadata BYTEA NOT NULL DEFAULT E'\\\\xFF', FOREIGN KEY(player) REFERENCES Players(id));",
"SELECT * FROM Boards WHERE id=$1 LIMIT 1;",
},
}
fmt.Printf("SQL Server %s is query type %d\n", sqlServer, queryType)
@@ -121,6 +122,11 @@ func boardByName(name string) (*Board, error) {
return b, db.QueryRow(queryStrings[queryType][0], name).Scan(b.Intake()...)
}

func boardById(id int64) (*Board, error) {
b := &Board{}
return b, db.QueryRow(queryStrings[queryType][23], id).Scan(b.Intake()...)
}

func playerByName(name string) (*Player, error) {
p := &Player{}
return p, db.QueryRow(queryStrings[queryType][1], name).Scan(p.Intake()...)


+ 1
- 1
sql_structs.go View File

@@ -96,7 +96,7 @@ func (b *Board) SomeEntries(start, end int64) ([]*Entry, error) {
}

func (b *Board) Url() string {
return "/board?name=" + b.Name
return "/board?id=" + b.Name
}

// implementation of Jsonable