diff options
author | Emile <hanemile@protonmail.com> | 2019-10-19 00:54:25 +0200 |
---|---|---|
committer | Emile <hanemile@protonmail.com> | 2019-10-19 00:54:25 +0200 |
commit | 8d099e0a80d010957d38be4ab0314ad495b938e6 (patch) | |
tree | c185dba30a9cf237c49a58531641b4ad22780b34 | |
parent | 059964e63fb9f06bceb153f903d04551c782bbf0 (diff) |
getAllChallenges function
-rw-r--r-- | src/db.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/db.go b/src/db.go index 976354b..bea1f9c 100644 --- a/src/db.go +++ b/src/db.go @@ -4,6 +4,8 @@ import ( "database/sql" "fmt" "log" + + _ "github.com/lib/pq" ) // setup the Database @@ -30,3 +32,48 @@ func getName(db *sql.DB) string { return names } + +// getAllChallenges gets all the challenges from the server +func getAllChallenges(db *sql.DB) []challenge { + + // build the query + query := fmt.Sprintf("SELECT * FROM challenges") + + // Execute the query + rows, err := db.Query(query) + defer rows.Close() + if err != nil { + log.Printf("[ E ] getAllChallenges query: %v\n\t\t\t query: %s\n", err, query) + return []challenge{} + } + + var challenges []challenge + + // iterate over the returned rows + for rows.Next() { + + var name, description, flag, container, category string + var points int + var static bool + + scanErr := rows.Scan(&name, &description, &flag, &container, &category, &points, &static) + if scanErr != nil { + log.Printf("[ E ] scan error: %v", scanErr) + return []challenge{} + } + + newChallenge := challenge{ + name: name, + description: description, + flag: flag, + container: container, + category: category, + points: points, + static: static, + } + + challenges = append(challenges, newChallenge) + } + + return challenges +} |