From 8d099e0a80d010957d38be4ab0314ad495b938e6 Mon Sep 17 00:00:00 2001 From: Emile Date: Sat, 19 Oct 2019 00:54:25 +0200 Subject: getAllChallenges function --- src/db.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src') 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 +} -- cgit 1.4.1