-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud_api.go
104 lines (86 loc) · 3.04 KB
/
crud_api.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
package main
import (
"encoding/json" // encode and decode json
"fmt"
"log" // log errors
"math/rand"
"net/http" // create server in golang
"strconv"
"github.com/gorilla/mux" // create router in golang
)
type Movie struct {
ID string `json:"id"`
Rating int `json:"rating"`
Title string `json:"title"`
Director *Director `json:"director"`
}
type Director struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
var movies []Movie
func main(){
movies = append(movies, Movie{ID: "1", Rating: 3, Title: "The Matrix", Director: &Director{FirstName: "joe", LastName: "smith"}})
movies = append(movies, Movie{ID: "4", Rating: 7, Title: "The MAtrix Reloaded", Director: &Director{FirstName: "lmfao", LastName: "lmao"}})
r := mux.NewRouter()
r.HandleFunc("/movies", getMovies).Methods("GET")
r.HandleFunc("/movies/{ID}", getMovie).Methods("GET")
r.HandleFunc("/movies/{ID}", updateMovie).Methods("PUT")
r.HandleFunc("/movies/{ID}", deleteMovie).Methods("DELETE")
r.HandleFunc("/movies", createMovie).Methods("POST")
fmt.Printf("Server is running on port 8000")
log.Fatal(http.ListenAndServe(":8000", r))
}
func getMovies(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(movies)
}
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 getMovie(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
params:= mux.Vars(r)
for _, item := range movies{ // blank identifier is used to ignore the index
if item.ID == params["ID"] {
json.NewEncoder(w).Encode(item)
return
}
}
}
func createMovie(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
// params := mux.Vars(r)
//object := Movie{ID: params["ID"], Rating: params["Rating"], Title: params["Title"], Director: &Director{FirstName: params["Director_fname"], LastName: params["Director_lname"]}}
var movie Movie
_ = json.NewDecoder(r.Body).Decode(&movie)
movie.ID = strconv.Itoa(rand.Intn(1000000))
movies = append(movies, movie)
json.NewEncoder(w).Encode(movie)
}
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 params["ID"] == item.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(movies)
}
json.NewEncoder(w).Encode(movies)
}
//object := Movie{ID: params["ID"], Rating: params["Rating"], Title: params["Title"], Director: &Director{FirstName: params["Director_fname"], LastName: params["Director_lname"]}}
//movies = append(movies, object)
//create string list
}