-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (93 loc) · 3.02 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
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type Movie struct {
ID string `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Director *Director `json:"director"`
}
type Director struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
var movies []Movie
func getallmovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}
func getonemovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for _, value := range movies {
if value.ID == params["id"] {
json.NewEncoder(w).Encode(value)
return
}
}
json.NewEncoder(w).Encode("No movies available for given id.")
}
func deleteonemovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, value := range movies {
if value.ID == params["id"] {
movies = append(movies[:index], movies[index+1:]...)
json.NewEncoder(w).Encode(movies)
return
}
}
json.NewEncoder(w).Encode("Please provide valid id you want to delete")
}
func updatemovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, value := range movies {
if value.ID == params["id"] {
movies = append(movies[:index], movies[index+1:]...)
break
}
}
//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(movies)
}
func createmovie(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var movie Movie
if r.Body == nil {
json.NewEncoder(w).Encode("Body is Empty cannot create movie")
return
}
json.NewDecoder(r.Body).Decode(&movie)
movie.ID = strconv.Itoa(rand.Intn(50000))
movies = append(movies, movie)
json.NewEncoder(w).Encode(movies)
}
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<h1> Welcome to the page"))
}
func main() {
r := mux.NewRouter()
movies = append(movies, Movie{ID: "12", Isbn: "1233", Title: "javascript", Director: &Director{Firstname: "ujjwal", Lastname: "silwal"}})
movies = append(movies, Movie{ID: "13", Isbn: "1244", Title: "rust", Director: &Director{Firstname: "anish", Lastname: "manandhar"}})
r.HandleFunc("/", home).Methods("GET")
r.HandleFunc("/movies", getallmovie).Methods("GET")
r.HandleFunc("/movies/{id}", deleteonemovie).Methods("DELETE")
r.HandleFunc("/movies", createmovie).Methods("POST")
r.HandleFunc("/movies/{id}", updatemovie).Methods("PUT")
r.HandleFunc("/movies/{id}", getonemovie).Methods("GET")
//http.ListenAndServe(":8000", nil)
fmt.Println("Listening to the port")
log.Fatal(http.ListenAndServe(":2000", r))
}