From 8add7a7c1d72986d68e8aa1c388347e202ad0dcf Mon Sep 17 00:00:00 2001
From: Emile <hanemile@protonmail.com>
Date: Sat, 19 Oct 2019 17:27:21 +0200
Subject: view all challenges in the database

---
 src/http.go | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/http.go b/src/http.go
index 6dd9485..88326fc 100644
--- a/src/http.go
+++ b/src/http.go
@@ -3,6 +3,7 @@ package main
 import (
 	"flag"
 	"fmt"
+	"html/template"
 	"io/ioutil"
 	"log"
 	"net/http"
@@ -51,11 +52,37 @@ func createPostHandler(w http.ResponseWriter, r *http.Request) {
 
 func viewGetHandler(w http.ResponseWriter, r *http.Request) {
 	// get all challenges from the db
-	challenges := getAllChallenges(db)
-	for _, chal := range challenges {
-		fmt.Println(chal.name)
+	challs := getAllChallenges(db)
+
+	log.Println("got all challenges")
+
+	// 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)
 	}
 
+	log.Println("Done inserting all the challengesi nto the challenges struct")
+
+	fmt.Printf("challenges: %#v\n", challenges)
+
+	log.Println("Generating a new template used to render the challenges")
+
+	t := template.New("")
+	t, err := t.ParseFiles("./hosted/view.html")
+	if err != nil {
+		log.Println(err)
+		return
+	}
+
+	log.Println("Done generating and parsing the template")
+
+	t.ExecuteTemplate(w, "view", challenges)
+
+	log.Println("Done executing the template")
+
 	// read the view.html file and write its content to the response writer
 	//readFileToResponse(w, "/view.html")
 }
-- 
cgit 1.4.1