forked from harshitmohan2996/jsonsearchtask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonsearchtask.go
129 lines (112 loc) · 3.1 KB
/
jsonsearchtask.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
//hello
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"log"
"net/http"
"net/url"
"github.com/gorilla/mux"
)
type manager struct {
ID string `json:"ID"`
Fname string `json:"Fname"`
Lname string `json:"Lname"`
}
type allManager []manager
var managers = allManager{
{
ID: "1",
Fname: "Harshit",
Lname: "Mohan",
},
{
ID: "2",
Fname: "Unnati",
Lname: "Kala",
},
{
ID: "3",
Fname: "Shivangi",
Lname: "Varshney",
},
{
ID: "4",
Fname: "Harshit",
Lname: "Mohaan",
},
}
func homeLink(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome home!")
}
func createEvent(w http.ResponseWriter, r *http.Request) {
var newEvent manager
// Convert r.Body into a readable formart
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Kindly enter data with the manager id and full name")
}
json.Unmarshal(reqBody, &newEvent)
// Add the newly created manager to the array of managers
managers = append(managers, newEvent)
// Return the 201 created status code
w.WriteHeader(http.StatusCreated)
// Return the newly created manager
json.NewEncoder(w).Encode(newEvent)
}
func getOneEvent(w http.ResponseWriter, r *http.Request) {
// Get the ID from the url
eventFname := mux.Vars(r)["Fname"]
// Get the details from an existing manager
// Use the blank identifier to avoid creating a value that will not be used
for _, singleEvent := range managers {
if singleEvent.Fname == eventFname {
json.NewEncoder(w).Encode(singleEvent)
}
}
fmt.Println(reflect.TypeOf(singleEvent))
}
func getAllManager(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(managers)
}
/*func updateEvent(w http.ResponseWriter, r *http.Request) {
// Get the ID from the url
eventID := mux.Vars(r)["id"]
var updatedEvent manager
// Convert r.Body into a readable formart
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Kindly enter data with the manager title and description only in order to update")
}
json.Unmarshal(reqBody, &updatedEvent)
for i, singleEvent := range managers {
if singleEvent.ID == eventID {
singleEvent.Title = updatedEvent.Title
singleEvent.Description = updatedEvent.Description
managers = append(managers[:i], singleEvent)
json.NewEncoder(w).Encode(singleEvent)
}
}
}*/
/*func deleteEvent(w http.ResponseWriter, r *http.Request) {
// Get the ID from the url
eventID := mux.Vars(r)["id"]
// Get the details from an existing manager
// Use the blank identifier to avoid creating a value that will not be used
for i, singleEvent := range managers {
if singleEvent.ID == eventID {
managers = append(managers[:i], managers[i+1:]...)
fmt.Fprintf(w, "The manager with ID %v has been deleted successfully", eventID)
}
}
}*/
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", homeLink)
router.HandleFunc("/createmanager", createEvent).Methods("POST")
router.HandleFunc("/managers", getAllManager).Methods("GET")
router.HandleFunc("/managers/{Fname}", getOneEvent).Methods("GET")
log.Fatal(http.ListenAndServe(":8043", router))
}