about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-19 19:36:27 +0200
committerEmile <hanemile@protonmail.com>2019-10-19 19:36:27 +0200
commit76774b0ffa860fa47061b49a3ca61ece444d051d (patch)
treec5a84757cd83d0457c7c5a3de6ef603d535e6a4d /src
parent54474f5bf3e0525adbf9694a51bab6e2ba3e6e5b (diff)
using uuids
Diffstat (limited to 'src')
-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
 }