-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
61 lines (47 loc) · 1.45 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
var tasks = []Task{}
// GetTasks fetches all the tasks that are availble.
func GetTasks(w http.ResponseWriter, r *http.Request) {
fmt.Println("In GetTasks !!")
writeResponse(w, Tasks)
}
// GetTaskByID fetch the task by ID.
func GetTaskByID(w http.ResponseWriter, r *http.Request) {
fmt.Println("In GetTaskByID page")
writeResponse(w, TaskbyID(mux.Vars(r)["id"]))
}
// AddTask is to add taks.
func AddTask(w http.ResponseWriter, r *http.Request) {
fmt.Println("In AddTask page")
var t Task
json.NewDecoder(r.Body).Decode(&t)
Tasks = append(Tasks, t)
writeResponse(w, Tasks[len(Tasks)-1])
}
// UpdateTask updates the task.
func UpdateTask(w http.ResponseWriter, r *http.Request) {
fmt.Println("In UpdateTask Page")
var t Task
json.NewDecoder(r.Body).Decode(&t)
if Equals(Task{}, TaskbyID((mux.Vars(r)["id"]))) {
writeResponse(w, "we could not find the ID you want to update !!")
}
q := r.URL.Query()
writeResponse(w, ModelUpdateTask((mux.Vars(r)["id"]), q.Get("name"), q.Get("desc")))
}
// SayHello is starting of the page.
func SayHello(w http.ResponseWriter, r *http.Request) {
fmt.Println("In SayHello Page !!")
writeResponse(w, "If you see this , server is running is fine !")
}
// this is util function.
func writeResponse(w http.ResponseWriter, result interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}