about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-20 00:31:13 +0200
committerEmile <hanemile@protonmail.com>2019-10-20 00:31:13 +0200
commit4f4317f59150588482779ab3e6f78cb4269bcd67 (patch)
tree07b8c7e2d4b67ce672f21dfe6c2dc5697dfd58c8 /src
parent027a5ee0fc896f5bbbe10bb2505a5942232ab995 (diff)
consistet naming for the db functions
Diffstat (limited to 'src')
-rw-r--r--src/db.go6
-rw-r--r--src/http.go14
-rw-r--r--src/structs.go16
3 files changed, 23 insertions, 13 deletions
diff --git a/src/db.go b/src/db.go
index da58951..c26e054 100644
--- a/src/db.go
+++ b/src/db.go
@@ -20,7 +20,7 @@ func setupDatabase() *sql.DB {
 }
 
 // getNames gets the name of all the challenges
-func getName(db *sql.DB) string {
+func dbGetName(db *sql.DB) string {
 
 	// get the current name of the challenges
 	query := fmt.Sprintf("SELECT name FROM challenges WHERE points=200")
@@ -34,7 +34,7 @@ func getName(db *sql.DB) string {
 }
 
 // getAllChallenges gets all the challenges from the server
-func getAllChallenges(db *sql.DB) []Challenge {
+func dbGetAllChallenges(db *sql.DB) []Challenge {
 
 	// build the query
 	query := fmt.Sprintf("SELECT * FROM challenges")
@@ -97,7 +97,7 @@ func dbNewChallenge(challenge Challenge) (string, error) {
 }
 
 // editChallengeUUID edited the challenge with the given uuid using the values in the updatedChallenge
-func editChallengeUUID(uuid string, updatedChallenge Challenge) error {
+func dbEditChallengeUUID(uuid string, updatedChallenge Challenge) error {
 	query := fmt.Sprintf("UPDATE challenges SET name = '%s', description = '%s', flag = '%s', container = '%s', category = '%s', points = %d, static = %t WHERE uuid::text = '%s'", updatedChallenge.Name, updatedChallenge.Description, updatedChallenge.Flag, updatedChallenge.Container, updatedChallenge.Category, updatedChallenge.Points, updatedChallenge.Static, updatedChallenge.UUID)
 
 	_, _ = db.Exec(query)
diff --git a/src/http.go b/src/http.go
index 8cbcf01..fe38a8e 100644
--- a/src/http.go
+++ b/src/http.go
@@ -30,6 +30,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("/api/getChallenges", getChallenges).Methods("GET")
 
 	return http.Server{
 		Addr:    fmt.Sprintf("0.0.0.0:%d", *port),
@@ -93,7 +94,7 @@ func createPostHandler(w http.ResponseWriter, r *http.Request) {
 // viewGetHandler returns a list of all challenges in the database
 func viewGetHandler(w http.ResponseWriter, r *http.Request) {
 	// get all challenges from the db
-	challs := getAllChallenges(db)
+	challs := dbGetAllChallenges(db)
 
 	// define a challenges struct storing the challenges.
 	// This struct can be used in a template
@@ -187,7 +188,7 @@ func editPostHandler(w http.ResponseWriter, r *http.Request) {
 	}
 
 	// update the challenge in the database
-	editChallengeUUID(r.Form.Get("challengeUUID"), editedChallenge)
+	dbEditChallengeUUID(r.Form.Get("challengeUUID"), editedChallenge)
 	log.Println("done editing challenge!")
 
 	http.Redirect(w, r, "/edit", http.StatusSeeOther)
@@ -205,3 +206,12 @@ func readFileToResponse(w http.ResponseWriter, path string) {
 		w.Write([]byte(contents))
 	}
 }
+
+// getChallenges returns all challenges
+func getChallenges(w http.ResponseWriter, r *http.Request) {
+	// Steps:
+	// - get all challenges from the database
+	// - marshal the challenges to json
+	// - return the json
+
+}
diff --git a/src/structs.go b/src/structs.go
index 84fe6d9..9db5ab7 100644
--- a/src/structs.go
+++ b/src/structs.go
@@ -2,14 +2,14 @@ package main
 
 // Challenge defines a challenge as in the original companion.json format
 type Challenge struct {
-	UUID        string
-	Name        string
-	Description string
-	Flag        string
-	Container   string
-	Category    string
-	Points      int
-	Static      bool
+	UUID        string `json:"uuid"`
+	Name        string `json:"name"`
+	Description string `json:"description"`
+	Flag        string `json:"flag"`
+	Container   string `json:"container"`
+	Category    string `json:"category"`
+	Points      int    `json:"points"`
+	Static      bool   `json:"static"`
 }
 
 // Challenges defines a list of challenges.