about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-19 21:19:46 +0200
committerEmile <hanemile@protonmail.com>2019-10-19 21:19:46 +0200
commit95736488e956397b9395a58704bb3b617f9600dd (patch)
tree25a682d69348bb7414a9015f395df99dfe31b8f4
parent71b82eca41b89437dbed7a80b3bf620cd6c0a074 (diff)
edit challenge endpoints
-rw-r--r--src/http.go82
1 files changed, 79 insertions, 3 deletions
diff --git a/src/http.go b/src/http.go
index cf66dce..7be863d 100644
--- a/src/http.go
+++ b/src/http.go
@@ -56,7 +56,7 @@ func createPostHandler(w http.ResponseWriter, r *http.Request) {
 
 	points, err := strconv.ParseInt(r.Form.Get("challengePoints"), 10, 64)
 	if err != nil {
-		log.Println("Could not parse points: %v", err)
+		log.Printf("Could not parse points: %v", err)
 		return
 	}
 
@@ -116,13 +116,89 @@ func viewGetHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 func editGetHandler(w http.ResponseWriter, r *http.Request) {
-	log.Println("edit GET")
-	readFileToResponse(w, "/edit.html")
+	var uuid string
+
+	if r.URL.Query()["uuid"] == nil {
+		log.Println("no uuid given")
+
+		// redirect the user to the view page for selecting what challenge to edit
+		viewGetHandler(w, r)
+		return
+	}
+	uuid = r.URL.Query()["uuid"][0]
+
+	log.Printf("fetching challenge with the uuid %s", uuid)
+
+	//chall := dbGetChallengeByUUID(uuid)
+	//
+	// temporary test challenge used while the dbGetChallengeByUUID function
+	// has not been implemented:
+	chall := Challenge{
+		UUID:        uuid,
+		Name:        "random challenge name",
+		Description: "some bad description",
+		Flag:        "FLAG",
+		Container:   "container-randomchallenge",
+		Category:    "guess",
+		Points:      100,
+		Static:      false,
+	}
+
+	// define a new template to render the challenges in
+	t := template.New("")
+	t, err := t.ParseFiles("./hosted/edit_uuid.html")
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	// execute the template using the challenges struct
+	t.ExecuteTemplate(w, "edit_uuid", chall)
+	return
 }
 
 func editPostHandler(w http.ResponseWriter, r *http.Request) {
 	log.Println("edit POST")
+	// parse the Post Request form
+	err := r.ParseForm()
+	if err != nil {
+		log.Println("could not parse the http post form!")
+		return
+	}
+
+	// parse the challenge points
+	points, err := strconv.ParseInt(r.PostFormValue("challengePoints"), 10, 64)
+	if err != nil {
+		log.Printf("Could not parse points: %v (%#v)", err, r.Form.Get("challengePoints"))
+		return
+	}
+
+	// parse the static value
+	var static bool
+	if r.Form.Get("challengeStatic") == "true" {
+		static = true
+	} else if r.Form.Get("challengeStatic") == "false" {
+		static = false
+	} else {
+		log.Println("[edit POST] Could not parse static: %v", r.Form.Get("challengeStatic"))
+		return
+	}
+
+	// define the new edited challenge
+	editedChallenge := Challenge{
+		UUID:        r.Form.Get("challengeUUID"),
+		Name:        r.Form.Get("challengeName"),
+		Description: r.Form.Get("challengeDescription"),
+		Flag:        r.Form.Get("challengeFlag"),
+		Container:   r.Form.Get("challengeContainer"),
+		Category:    r.Form.Get("challengeCategory"),
+		Points:      int(points),
+		Static:      static,
+	}
 
+	// update the challenge in the database
+	editChallengeUUID(r.Form.Get("challengeUUID"), editedChallenge)
+	log.Println("done editing challenge!")
 }
 
 // Helper function to host files off of "hosted/" directory