-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (47 loc) · 1.45 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
package main
import(
"fmt"
"log"
"encoding/json"
"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"`
}
func getMovies(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}
func deleteMovies(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 movies []Movie
func main(){
r := mux.NewRouter()
maulana := Director{Firstname: "Maulana", Lastname: "Ihsan"}
movies = append(movies, Movie{ID: "1", Isbn: "24251", Title: "I still love her", Director: &maulana})
movies = append(movies, Movie{ID: "2", Isbn: "23245", Title: "But i give her space and i will let her go", Director: &maulana})
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("POST")
r.HandleFunc("/movies/{id}", deleteMovie).Methods("DELETE")
fmt.Printf("Starting at Port 8080\n")
log.Fatal(http.ListenAndServe(":8080", r))
}