Skip to content
Snippets Groups Projects
Commit 7acefc61 authored by Hans Kristian Hoel's avatar Hans Kristian Hoel
Browse files

cratet a QueryGet function to prevent duplication in code

parent 591a1812
No related branches found
No related tags found
No related merge requests found
package assignment2
import (
"net/http"
"fmt"
"net/http"
)
func HandlerNil(w http.ResponseWriter, r *http.Request) { //standar default respons
......@@ -10,31 +10,28 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) { //standar default res
http.Error(w, "Invalid request", http.StatusBadRequest)
}
func HandlerCommits (w http.ResponseWriter, r *http.Request) {
func QueryGet(s string, t string, r *http.Request) string {
limit := r.URL.Query().Get("limit") // Reads limit
if limit == "" { // cheks if there exist an limit
limit = "5" // sets limit to 5
test := r.URL.Query().Get(s)
if test == "" {
test = t
}
return test
auth := r.URL.Query().Get("auth") // Reads auth
if auth == "" { // cheks if there exist an auth
auth = "false" // sets auth to false
}
func HandlerCommits(w http.ResponseWriter, r *http.Request) {
limit := QueryGet("limit", "5", r)
auth := QueryGet("auth", "false", r)
}
func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
limit := r.URL.Query().Get("limit") // Reads limit
if limit == "" { // cheks if there exist an limit
limit = "5" // sets limit to 5
}
limit := QueryGet("limit", "5", r)
auth := r.URL.Query().Get("auth") // Reads auth
if auth == "" { // cheks if there exist an auth
auth = "false" // sets auth to false
}
auth := QueryGet("auth", "false", r)
}
......@@ -45,10 +42,7 @@ func HandlerIssues (w http.ResponseWriter, r *http.Request) {
// error
}
auth := r.URL.Query().Get("auth") // Reads auth
if auth == "" { // cheks if there exist an auth
auth = "false" // sets auth to false
}
auth := QueryGet("auth", "false", r)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment