-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (94 loc) · 3.27 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
// Movies ka struct
type Movie struct {
ID string `json:"id"`
Title string `json:"title"`
Year string `json:"year"`
Director *Director `json:"director"`
}
// Director sahab ka struct
type Director struct {
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Age int `json:"age"`
}
var movies []Movie
// Func 1
func getMovies(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}
// Func 2
func getMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for _, item := range movies {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Movie{})
}
// Func 3
func createMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var movie Movie
_ = json.NewDecoder(r.Body).Decode(&movie)
movies = append(movies, movie)
json.NewEncoder(w).Encode(movie)
}
// Func 4
func updateMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range movies {
if item.ID == params["id"] {
movies = append(movies[:index], movies[index+1:]...)
var movie Movie
_ = json.NewDecoder(r.Body).Decode(&movie)
movie.ID = params["id"]
movies = append(movies, movie)
json.NewEncoder(w).Encode(movie)
return
}
}
json.NewEncoder(w).Encode(movies)
}
// Func 5
func deleteMovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range movies {
if item.ID == params["id"] {
movies = append(movies[:index], movies[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(movies)
}
func main() {
r := mux.NewRouter()
// making Struct Objects coz database nhi mila abhi tak
movies = append(movies, Movie{ID: "1", Title: "The Shawshank Redemption", Year: "1994", Director: &Director{FirstName: "Frank", LastName: "Darabont", Age: 61}})
movies = append(movies, Movie{ID: "2", Title: "The Godfather", Year: "1972", Director: &Director{FirstName: "Francis", LastName: "Ford Coppola", Age: 81}})
movies = append(movies , Movie{ID: "3", Title: "The Dark Knight", Year: "2008", Director: &Director{FirstName: "Christopher", LastName: "Nolan", Age: 50}})
movies = append(movies, Movie{ID: "4" , Title : "Swadesh" , Year : "2004" , Director : &Director{FirstName : "Ashutosh" , LastName : "Gowariker" , Age : 56}})
movies = append(movies , Movie{ID : "5" , Title : "The Pursuit of Happyness" , Year : "2006" , Director : &Director{FirstName : "Gabriele" , LastName : "Muccino" , Age : 53}})
// Handling Function for better understanding
r.HandleFunc("/movies", getMovies).Methods("GET")
r.HandleFunc("/movies/{id}", getMovie).Methods("GET")
r.HandleFunc("/movies", createMovie).Methods("POST")
r.HandleFunc("/movies/{id}", updateMovie).Methods("PUT")
r.HandleFunc("/movies/{id}", deleteMovie).Methods("DELETE")
// Server chala rha hu
fmt.Println("Server on port 8000")
log.Fatal(http.ListenAndServe(":8000", r))
}