about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/db.go16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/db.go b/src/db.go
index cb65979..6961aad 100644
--- a/src/db.go
+++ b/src/db.go
@@ -52,17 +52,18 @@ func getAllChallenges(db *sql.DB) []Challenge {
 	// iterate over the returned rows
 	for rows.Next() {
 
-		var name, description, flag, container, category string
+		var uuid, name, description, flag, container, category string
 		var points int
 		var static bool
 
-		scanErr := rows.Scan(&name, &description, &flag, &container, &category, &points, &static)
+		scanErr := rows.Scan(&uuid, &name, &description, &flag, &container, &category, &points, &static)
 		if scanErr != nil {
 			log.Printf("[ E ] scan error: %v", scanErr)
 			return []Challenge{}
 		}
 
 		newChallenge := Challenge{
+			UUID:        uuid,
 			Name:        name,
 			Description: description,
 			Flag:        flag,
@@ -79,17 +80,18 @@ func getAllChallenges(db *sql.DB) []Challenge {
 }
 
 // dbNewChallenge inserts the given challenge into the database
-func dbNewChallenge(challenge Challenge) error {
+func dbNewChallenge(challenge Challenge) (string, error) {
 
 	// build the query to be executed
-	query := fmt.Sprintf("INSERT INTO challenges(name, description, flag, container, category, points, static) VALUES ($1, $2, $3, $4, $5, $6, $7)")
+	query := fmt.Sprintf("INSERT INTO challenges(name, description, flag, container, category, points, static) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING uuid")
 
 	// execute the query with the challenge values given
-	_, err := db.Exec(query, challenge.Name, challenge.Description, challenge.Flag, challenge.Container, challenge.Category, challenge.Points, challenge.Static)
+	var uuid string
+	err := db.QueryRow(query, challenge.Name, challenge.Description, challenge.Flag, challenge.Container, challenge.Category, challenge.Points, challenge.Static).Scan(&uuid)
 
 	// handle errors
 	if err != nil {
-		return err
+		return "", err
 	}
-	return nil
+	return uuid, nil
 }