about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db.go47
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
+}