@@ -4,30 +4,34 @@ import (
44 "encoding/json"
55 "net/http"
66
7- gopher "github.com/friendsofgo/gopherapi/pkg"
7+ "github.com/friendsofgo/gopherapi/pkg/adding"
8+ "github.com/friendsofgo/gopherapi/pkg/fetching"
89
910 "github.com/gorilla/mux"
1011)
1112
1213type api struct {
13- router http.Handler
14- repository gopher.GopherRepository
14+ router http.Handler
15+ fetching fetching.Service
16+ adding adding.Service
1517}
1618
1719// Server representation of gopher server
1820type Server interface {
1921 Router () http.Handler
2022 FetchGophers (w http.ResponseWriter , r * http.Request )
2123 FetchGopher (w http.ResponseWriter , r * http.Request )
24+ AddGopher (w http.ResponseWriter , r * http.Request )
2225}
2326
2427// New initialize the server
25- func New (repo gopher. GopherRepository ) Server {
26- a := & api {repository : repo }
28+ func New (fS fetching. Service , aS adding. Service ) Server {
29+ a := & api {fetching : fS , adding : aS }
2730
2831 r := mux .NewRouter ()
2932 r .HandleFunc ("/gophers" , a .FetchGophers ).Methods (http .MethodGet )
3033 r .HandleFunc ("/gophers/{ID:[a-zA-Z0-9_]+}" , a .FetchGopher ).Methods (http .MethodGet )
34+ r .HandleFunc ("/gophers" , a .AddGopher ).Methods (http .MethodPost )
3135
3236 a .router = r
3337 return a
@@ -39,7 +43,7 @@ func (a *api) Router() http.Handler {
3943
4044// FetchGophers return a list of all gophers
4145func (a * api ) FetchGophers (w http.ResponseWriter , r * http.Request ) {
42- gophers , _ := a .repository .FetchGophers ()
46+ gophers , _ := a .fetching .FetchGophers ()
4347
4448 w .Header ().Set ("Content-Type" , "application/json" )
4549 json .NewEncoder (w ).Encode (gophers )
@@ -48,7 +52,7 @@ func (a *api) FetchGophers(w http.ResponseWriter, r *http.Request) {
4852// FetchGopher return a gopher by ID
4953func (a * api ) FetchGopher (w http.ResponseWriter , r * http.Request ) {
5054 vars := mux .Vars (r )
51- gopher , err := a .repository .FetchGopherByID (vars ["ID" ])
55+ gopher , err := a .fetching .FetchGopherByID (vars ["ID" ])
5256 w .Header ().Set ("Content-Type" , "application/json" )
5357 if err != nil {
5458 w .WriteHeader (http .StatusNotFound ) // We use not found for simplicity
@@ -58,3 +62,32 @@ func (a *api) FetchGopher(w http.ResponseWriter, r *http.Request) {
5862
5963 json .NewEncoder (w ).Encode (gopher )
6064}
65+
66+ type addGopherRequest struct {
67+ ID string `json:"ID"`
68+ Name string `json:"name"`
69+ Image string `json:"image"`
70+ Age int `json:"age"`
71+ }
72+
73+ // AddGopher save a gopher
74+ func (a * api ) AddGopher (w http.ResponseWriter , r * http.Request ) {
75+ decoder := json .NewDecoder (r .Body )
76+
77+ var g addGopherRequest
78+ err := decoder .Decode (& g )
79+
80+ if err != nil {
81+ w .WriteHeader (http .StatusInternalServerError )
82+ json .NewEncoder (w ).Encode ("Error unmarshalling request body" )
83+ return
84+ }
85+
86+ if err := a .adding .AddGopher (g .ID , g .Name , g .Image , g .Age ); err != nil {
87+ w .WriteHeader (http .StatusInternalServerError )
88+ json .NewEncoder (w ).Encode ("Can't create a gopher" )
89+ return
90+ }
91+
92+ w .WriteHeader (http .StatusCreated )
93+ }
0 commit comments