Skip to content

Commit 02c1554

Browse files
committed
Modifying service
1 parent fb6f85d commit 02c1554

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

cmd/gopherapi/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"log"
77
"net/http"
88

9+
"github.com/friendsofgo/gopherapi/pkg/modifying"
10+
911
"github.com/friendsofgo/gopherapi/pkg/adding"
1012
"github.com/friendsofgo/gopherapi/pkg/fetching"
1113

@@ -27,10 +29,11 @@ func main() {
2729
}
2830

2931
repo := inmem.NewRepository(gophers)
30-
fetching := fetching.NewService(repo)
31-
adding := adding.NewService(repo)
32+
fS := fetching.NewService(repo)
33+
aS := adding.NewService(repo)
34+
mS := modifying.NewService(repo)
3235

33-
s := server.New(fetching, adding)
36+
s := server.New(fS, aS, mS)
3437

3538
fmt.Println("The gopher server is on tap now: http://localhost:8080")
3639
log.Fatal(http.ListenAndServe(":8080", s.Router()))

pkg/modifying/service.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package modifying
2+
3+
import (
4+
gopher "github.com/friendsofgo/gopherapi/pkg"
5+
)
6+
7+
// Service provides modifying operations.
8+
type Service interface {
9+
ModifyGopher(ID, name, image string, age int) error
10+
}
11+
12+
type service struct {
13+
repository gopher.Repository
14+
}
15+
16+
// NewService creates a modifying service with the necessary dependencies
17+
func NewService(repository gopher.Repository) Service {
18+
return &service{repository}
19+
}
20+
21+
// ModifyGopher modify a gopher data
22+
func (s *service) ModifyGopher(ID, name, image string, age int) error {
23+
g := gopher.New(ID, name, image, age)
24+
return s.repository.UpdateGopher(ID, *g)
25+
}

pkg/server/api.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66

77
"github.com/friendsofgo/gopherapi/pkg/adding"
88
"github.com/friendsofgo/gopherapi/pkg/fetching"
9+
"github.com/friendsofgo/gopherapi/pkg/modifying"
910

1011
"github.com/gorilla/mux"
1112
)
1213

1314
type api struct {
14-
router http.Handler
15-
fetching fetching.Service
16-
adding adding.Service
15+
router http.Handler
16+
fetching fetching.Service
17+
adding adding.Service
18+
modifying modifying.Service
1719
}
1820

1921
// Server representation of gopher server
@@ -22,16 +24,18 @@ type Server interface {
2224
FetchGophers(w http.ResponseWriter, r *http.Request)
2325
FetchGopher(w http.ResponseWriter, r *http.Request)
2426
AddGopher(w http.ResponseWriter, r *http.Request)
27+
ModifyGopher(w http.ResponseWriter, r *http.Request)
2528
}
2629

2730
// New initialize the server
28-
func New(fS fetching.Service, aS adding.Service) Server {
29-
a := &api{fetching: fS, adding: aS}
31+
func New(fS fetching.Service, aS adding.Service, mS modifying.Service) Server {
32+
a := &api{fetching: fS, adding: aS, modifying: mS}
3033

3134
r := mux.NewRouter()
3235
r.HandleFunc("/gophers", a.FetchGophers).Methods(http.MethodGet)
3336
r.HandleFunc("/gophers/{ID:[a-zA-Z0-9_]+}", a.FetchGopher).Methods(http.MethodGet)
3437
r.HandleFunc("/gophers", a.AddGopher).Methods(http.MethodPost)
38+
r.HandleFunc("/gophers/{ID:[a-zA-Z0-9_]+}", a.ModifyGopher).Methods(http.MethodPut)
3539

3640
a.router = r
3741
return a
@@ -77,6 +81,7 @@ func (a *api) AddGopher(w http.ResponseWriter, r *http.Request) {
7781
var g addGopherRequest
7882
err := decoder.Decode(&g)
7983

84+
w.Header().Set("Content-Type", "application/json")
8085
if err != nil {
8186
w.WriteHeader(http.StatusInternalServerError)
8287
json.NewEncoder(w).Encode("Error unmarshalling request body")
@@ -91,3 +96,32 @@ func (a *api) AddGopher(w http.ResponseWriter, r *http.Request) {
9196

9297
w.WriteHeader(http.StatusCreated)
9398
}
99+
100+
type modifyGopherRequest struct {
101+
Name string `json:"name"`
102+
Image string `json:"image"`
103+
Age int `json:"age"`
104+
}
105+
106+
// ModifyGopher modify gopher data
107+
func (a *api) ModifyGopher(w http.ResponseWriter, r *http.Request) {
108+
decoder := json.NewDecoder(r.Body)
109+
110+
var g addGopherRequest
111+
err := decoder.Decode(&g)
112+
113+
w.Header().Set("Content-Type", "application/json")
114+
if err != nil {
115+
w.WriteHeader(http.StatusInternalServerError)
116+
json.NewEncoder(w).Encode("Error unmarshalling request body")
117+
return
118+
}
119+
vars := mux.Vars(r)
120+
if err := a.modifying.ModifyGopher(vars["ID"], g.Name, g.Image, g.Age); err != nil {
121+
w.WriteHeader(http.StatusInternalServerError)
122+
json.NewEncoder(w).Encode("Can't modify a gopher")
123+
return
124+
}
125+
126+
w.WriteHeader(http.StatusNoContent)
127+
}

pkg/server/api_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/friendsofgo/gopherapi/pkg/adding"
1313
"github.com/friendsofgo/gopherapi/pkg/fetching"
14+
"github.com/friendsofgo/gopherapi/pkg/modifying"
1415

1516
sample "github.com/friendsofgo/gopherapi/cmd/sample-data"
1617
gopher "github.com/friendsofgo/gopherapi/pkg"
@@ -140,5 +141,7 @@ func buildServer() Server {
140141
repo := inmem.NewRepository(sample.Gophers)
141142
fetching := fetching.NewService(repo)
142143
adding := adding.NewService(repo)
143-
return New(fetching, adding)
144+
modifying := modifying.NewService(repo)
145+
146+
return New(fetching, adding, modifying)
144147
}

0 commit comments

Comments
 (0)