-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (115 loc) · 2.96 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strconv"
)
// Movie is a struct
type Movie struct {
ID int `json:"id"`
Name string `json:"name"`
FilmMaker string `json:"filmMaker"`
Rating float32 `json:"rating"`
}
func findProduct(mid int) (int, error) {
for i, p := range movies {
if p.ID == mid {
return i, nil
}
}
return -1, fmt.Errorf("movie not Found")
}
func returnResponse(w http.ResponseWriter) {
err := json.NewEncoder(w).Encode(movies)
if err != nil {
fmt.Println("There seems to be an error : ", err)
return
}
}
func returnError(w http.ResponseWriter, s string) {
err := json.NewEncoder(w).Encode(s)
if err != nil {
fmt.Println("There seems to be an error : ", err)
return
}
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
fmt.Println("A GET request.")
returnResponse(w) // Returns a json of all the Movies in the database
}
if r.Method == http.MethodPost {
fmt.Println("A POST request.")
var p Movie
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
returnError(w, "Couldn't decode your request body.")
fmt.Println("There seems to be an error : ", err)
return
}
p.ID = len(movies) + 1
movies = append(movies, p)
returnResponse(w)
}
if r.Method == http.MethodPut {
fmt.Println("A PUT request.")
reg := regexp.MustCompile(`/([0-9]+)`)
g := reg.FindAllStringSubmatch(r.URL.Path, -1)
strMid := g[0][1]
mid, err := strconv.Atoi(strMid)
if err != nil {
returnError(w, "Couldn't decode the URI.")
fmt.Println("Not a valid URI.")
return
}
i, err := findProduct(mid)
if err != nil {
returnError(w, "Couldn't find the Movie id.")
fmt.Println("There seems to be an error : ", err)
return
}
var p Movie
err = json.NewDecoder(r.Body).Decode(&p)
if err != nil {
returnError(w, "Couldn't decode your request body.")
fmt.Println("There seems to be an error : ", err)
}
p.ID = mid
movies[i] = p
returnResponse(w)
}
if r.Method == http.MethodDelete {
fmt.Println("A DELETE request.")
reg := regexp.MustCompile(`/([0-9]+)`)
g := reg.FindAllStringSubmatch(r.URL.Path, -1)
strMid := g[0][1]
mid, err := strconv.Atoi(strMid)
if err != nil {
returnError(w, "Couldn't decode the URI.")
fmt.Println("Not a valid URI.")
return
}
i, err := findProduct(mid)
fmt.Println("i", i)
fmt.Println(movies[i])
if err != nil {
returnError(w, "Couldn't find the Movie id.")
fmt.Println("There seems to be an error : ", err)
return
}
movies = append(movies[:i], movies[i+1:]...)
returnResponse(w)
}
}
func main() {
http.HandleFunc("/", mainHandler)
http.ListenAndServe(":8080", nil)
}
var movies = []Movie{
Movie{1, "Chungking Express", "Wong Kar Wai", 9.0},
Movie{ID: 2, Name: "Yi Yi", FilmMaker: "Edward Yang", Rating: 9.0},
Movie{ID: 3, Name: "Memories of Murder", FilmMaker: "Bong Joon Ho", Rating: 8.9},
}