From 95736488e956397b9395a58704bb3b617f9600dd Mon Sep 17 00:00:00 2001 From: Emile Date: Sat, 19 Oct 2019 21:19:46 +0200 Subject: edit challenge endpoints --- src/http.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 3 deletions(-) (limited to 'src') 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 -- cgit 1.4.1