about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-19 17:27:21 +0200
committerEmile <hanemile@protonmail.com>2019-10-19 17:27:21 +0200
commit8add7a7c1d72986d68e8aa1c388347e202ad0dcf (patch)
treec98b23d82d0a53961fc8b1d874b6d70268695081
parent2aa3476ef53b5eadfb89013660103ac3e6d69def (diff)
view all challenges in the database
-rw-r--r--hosted/view.html39
-rw-r--r--src/http.go33
2 files changed, 58 insertions, 14 deletions
diff --git a/hosted/view.html b/hosted/view.html
index ac8b411..1f72e9e 100644
--- a/hosted/view.html
+++ b/hosted/view.html
@@ -1,3 +1,4 @@
+{{define "view"}}
 <!DOCTYPE html>
 <html>
     <head>
@@ -35,17 +36,33 @@
             </div>
         </nav>
         <main class="container" role="main">
-            <div class="jumbotron">
-                <h1 class="display-4">challenges</h1>
-                <h3>You can find a list of the challenges below</h3>
-                <p>You can create, view and edit challenges there</p>
-            </div>
-
-            <div class="jumbotron">
-                <h2 class="display-4">challenges</h2>
-                <h3>You can find a list of the challenges below</h3>
-                <p>You can create, view and edit challenges there</p>
-            </div>
+			<table class="table">
+			  <thead>
+				<tr>
+				  <th scope="col">Name</th>
+				  <th scope="col">Description</th>
+				  <th scope="col">Flag</th>
+				  <th scope="col">Container</th>
+				  <th scope="col">Category</th>
+				  <th scope="col">Points</th>
+				  <th scope="col">Static</th>
+				</tr>
+			  </thead>
+			  <tbody>
+				{{range .Challenge}}
+				<tr>
+				  <th scope="row">{{.Name}}</th>
+				  <td>{{.Description}}</td>
+				  <td>{{.Flag}}</td>
+				  <td>{{.Container}}</td>
+				  <td>{{.Category}}</td>
+				  <td>{{.Points}}</td>
+				  <td>{{.Static}}</td>
+				</tr>
+				{{end}}
+			  </tbody>
+			</table>
         </main>
     </body>
 </html>
+{{end}}
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")
 }