Skip to content

Commit 3c8614d

Browse files
committed
Added Switch statements to our code
1 parent 0c3b5ef commit 3c8614d

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed

src/flows/switch/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
type httpRequest struct {
4+
Method string
5+
}
6+
7+
func main() {
8+
r := httpRequest{Method: "GET"}
9+
10+
// NOTE: Go has implicit break statements among the cases by default
11+
// If we want to case to fall through to the next, use explicit "fallthrough" keyword
12+
switch r.Method {
13+
case "GET":
14+
println("GET request")
15+
case "POST":
16+
println("POST request")
17+
case "PUT":
18+
println("PUT request")
19+
case "DELETE":
20+
println("DELETE request")
21+
default:
22+
println("Unhandled method")
23+
}
24+
}

src/webservice/controllers/front.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controllers
22

33
import (
4+
"encoding/json"
5+
"io"
46
"net/http"
57
)
68

@@ -11,3 +13,8 @@ func RegisterControllers() {
1113
http.Handle("/users", *uc)
1214
http.Handle("/users/", *uc)
1315
}
16+
17+
func encodeResponseAsJSON(data interface{}, w io.Writer) {
18+
enc := json.NewEncoder(w)
19+
enc.Encode(data)
20+
}

src/webservice/controllers/user.go

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,127 @@
11
package controllers
22

33
import (
4+
"demo/webservice/models"
5+
"encoding/json"
46
"net/http"
57
"regexp"
8+
"strconv"
69
)
710

811
type userController struct {
912
userIDPattern *regexp.Regexp
1013
}
1114

1215
func (uc userController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
13-
w.Write([]byte("Hello from the User Controller!"))
16+
if r.URL.Path == "/users" || r.URL.Path == "/users/" {
17+
switch r.Method {
18+
case http.MethodGet:
19+
uc.getAll(w)
20+
case http.MethodPost:
21+
uc.post(w, r)
22+
default:
23+
w.WriteHeader(http.StatusNotImplemented)
24+
}
25+
} else {
26+
matches := uc.userIDPattern.FindStringSubmatch(r.URL.Path)
27+
if len(matches) == 0 {
28+
w.WriteHeader(http.StatusBadRequest)
29+
w.Write([]byte("Malformed URL"))
30+
return
31+
}
32+
33+
id, err := strconv.Atoi(matches[1])
34+
if err != nil {
35+
w.WriteHeader(http.StatusBadRequest)
36+
w.Write([]byte("Malformed User ID"))
37+
return
38+
}
39+
40+
switch r.Method {
41+
case http.MethodGet:
42+
uc.get(id, w)
43+
case http.MethodPut:
44+
uc.put(id, w, r)
45+
case http.MethodDelete:
46+
uc.delete(id, w)
47+
default:
48+
w.WriteHeader(http.StatusNotImplemented)
49+
}
50+
}
51+
}
52+
53+
func (uc *userController) getAll(w http.ResponseWriter) {
54+
encodeResponseAsJSON(models.GetUsers(), w)
55+
}
56+
57+
func (uc *userController) get(id int, w http.ResponseWriter) {
58+
u, err := models.GetUserByID(id)
59+
if err != nil {
60+
w.WriteHeader(http.StatusInternalServerError)
61+
w.Write([]byte(err.Error()))
62+
return
63+
}
64+
encodeResponseAsJSON(u, w)
65+
}
66+
67+
func (uc *userController) post(w http.ResponseWriter, r *http.Request) {
68+
u, err := uc.parseRequest(r)
69+
if err != nil {
70+
w.WriteHeader(http.StatusInternalServerError)
71+
w.Write([]byte("Could not parse User object"))
72+
return
73+
}
74+
75+
u, err = models.AddUser(u)
76+
if err != nil {
77+
w.WriteHeader(http.StatusInternalServerError)
78+
w.Write([]byte(err.Error()))
79+
return
80+
}
81+
encodeResponseAsJSON(u, w)
82+
}
83+
84+
func (uc *userController) put(id int, w http.ResponseWriter, r *http.Request) {
85+
u, err := uc.parseRequest(r)
86+
if err != nil {
87+
w.WriteHeader(http.StatusInternalServerError)
88+
w.Write([]byte("Could not parse User object"))
89+
return
90+
}
91+
92+
if u.ID != id {
93+
w.WriteHeader(http.StatusBadRequest)
94+
w.Write([]byte("ID of submitted user must match ID in URL"))
95+
return
96+
}
97+
98+
u, err = models.UpdateUser(u)
99+
if err != nil {
100+
w.WriteHeader(http.StatusInternalServerError)
101+
w.Write([]byte(err.Error()))
102+
return
103+
}
104+
encodeResponseAsJSON(u, w)
105+
}
106+
107+
func (uc *userController) delete(id int, w http.ResponseWriter) {
108+
err := models.RemoveUserByID(id)
109+
if err != nil {
110+
w.WriteHeader(http.StatusInternalServerError)
111+
w.Write([]byte(err.Error()))
112+
return
113+
}
114+
w.WriteHeader(http.StatusOK)
115+
}
116+
117+
func (uc *userController) parseRequest(r *http.Request) (models.User, error) {
118+
dec := json.NewDecoder(r.Body)
119+
var u models.User
120+
err := dec.Decode(&u)
121+
if err != nil {
122+
return models.User{}, err
123+
}
124+
return u, nil
14125
}
15126

16127
// Constructor Function - Go's way of creating constructor as a function

src/webservice/models/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type User struct {
1313
}
1414

1515
var (
16-
users []*User
16+
users = []*User{}
1717
nextID = 1
1818
)
1919

0 commit comments

Comments
 (0)