From 9bd8dcd0c878885864f5fdc29be284d76906b45b Mon Sep 17 00:00:00 2001 From: Emile Date: Sun, 20 Oct 2019 01:32:39 +0200 Subject: editSelect endpoint this endpoint is similar to the view endpoint, but contains an edit button next to the challenges --- src/http.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/http.go') diff --git a/src/http.go b/src/http.go index 4df6944..079ecba 100644 --- a/src/http.go +++ b/src/http.go @@ -32,6 +32,7 @@ func setupHTTPServer() http.Server { r.HandleFunc("/view", viewGetHandler).Methods("GET") r.HandleFunc("/edit", editGetHandler).Methods("GET") r.HandleFunc("/edit", editPostHandler).Methods("POST") + r.HandleFunc("/editSelect", editSelectGetHandler).Methods("GET") r.HandleFunc("/api/getChallenges", getChallenges).Methods("GET") return http.Server{ @@ -119,14 +120,35 @@ func viewGetHandler(w http.ResponseWriter, r *http.Request) { t.ExecuteTemplate(w, "view", challenges) } +func editSelectGetHandler(w http.ResponseWriter, r *http.Request) { + // get all challenges from the db + challs := dbGetAllChallenges() + + // define a challenges struct storing the challenges. + // This struct can be used in a template + challenges := Challenges{} + + for _, chal := range challs { + challenges.Challenge = append(challenges.Challenge, chal) + } + + // define a new template to render the challenges in + t := template.New("") + t, err := t.ParseFiles("./hosted/edit.html") + if err != nil { + log.Println(err) + return + } + + // execure the template using the challenges struct + t.ExecuteTemplate(w, "edit", challenges) +} + func editGetHandler(w http.ResponseWriter, r *http.Request) { 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) + log.Println("editnoparam") return } uuid = r.URL.Query()["uuid"][0] -- cgit 1.4.1