Skip to content

Commit

Permalink
Merge pull request #26 from umamiMike/main
Browse files Browse the repository at this point in the history
updates for functioning build in domain-hex
  • Loading branch information
katzien authored Oct 5, 2020
2 parents 1d1d00d + b3f917f commit 2b09b28
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
Binary file added domain-hex/cmd/sample-data/sample-data
Binary file not shown.
12 changes: 6 additions & 6 deletions domain-hex/cmd/sample-data/sample_reviews.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import "github.com/katzien/go-structure-examples/domain-hex/pkg/reviewing"

var DefaultReviews = []reviewing.Review{
{BeerID: 1, FirstName: "Joe", LastName: "Tribiani", Score: 5, Text: "This is good but this is not pizza!"},
{BeerID: 2, FirstName: "Chandler", LastName: "Bing", Score: 1, Text: "I would SO NOT drink this ever again."},
{BeerID: 1, FirstName: "Ross", LastName: "Geller", Score: 4, Text: "Drank while on a break, was pretty good!"},
{BeerID: 2, FirstName: "Phoebe", LastName: "Buffay", Score: 2, Text: "Wasn't that great, so I gave it to my smelly cat."},
{BeerID: 1, FirstName: "Monica", LastName: "Geller", Score: 5, Text: "AMAZING! Like Chandler's jokes!"},
{BeerID: 2, FirstName: "Rachel", LastName: "Green", Score: 5, Text: "So yummy, just like my beef and custard trifle."},
{BeerID: "1", FirstName: "Joe", LastName: "Tribiani", Score: 5, Text: "This is good but this is not pizza!"},
{BeerID: "2", FirstName: "Chandler", LastName: "Bing", Score: 1, Text: "I would SO NOT drink this ever again."},
{BeerID: "1", FirstName: "Ross", LastName: "Geller", Score: 4, Text: "Drank while on a break, was pretty good!"},
{BeerID: "2", FirstName: "Phoebe", LastName: "Buffay", Score: 2, Text: "Wasn't that great, so I gave it to my smelly cat."},
{BeerID: "1", FirstName: "Monica", LastName: "Geller", Score: 5, Text: "AMAZING! Like Chandler's jokes!"},
{BeerID: "2", FirstName: "Rachel", LastName: "Green", Score: 5, Text: "So yummy, just like my beef and custard trifle."},
}
26 changes: 4 additions & 22 deletions domain-hex/pkg/http/rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func addBeer(s adding.Service) func(w http.ResponseWriter, r *http.Request, _ ht
return
}

_ := s.AddBeer(newBeer)
s.AddBeer(newBeer)
// error handling omitted for simplicity

w.Header().Set("Content-Type", "application/json")
Expand All @@ -48,20 +48,14 @@ func addBeer(s adding.Service) func(w http.ResponseWriter, r *http.Request, _ ht
// addBeerReview returns a handler for POST /beers/:id/reviews requests
func addBeerReview(s reviewing.Service) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ID, err := strconv.Atoi(p.ByName("id"))
if err != nil {
http.Error(w, fmt.Sprintf("%s is not a valid Beer ID, it must be a number.", p.ByName("id")), http.StatusBadRequest)
return
}

var newReview reviewing.Review
decoder := json.NewDecoder(r.Body)

if err := decoder.Decode(&newReview); err != nil {
http.Error(w, "Failed to parse review", http.StatusBadRequest)
}

newReview.BeerID = ID
newReview.BeerID = p.ByName("id")

s.AddBeerReview(newReview)

Expand All @@ -82,13 +76,7 @@ func getBeers(s listing.Service) func(w http.ResponseWriter, r *http.Request, _
// getBeer returns a handler for GET /beers/:id requests
func getBeer(s listing.Service) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ID, err := strconv.Atoi(p.ByName("id"))
if err != nil {
http.Error(w, fmt.Sprintf("%s is not a valid beer ID, it must be a number.", p.ByName("id")), http.StatusBadRequest)
return
}

beer, err := s.GetBeer(ID)
beer, err := s.GetBeer(p.ByName("id"))
if err == listing.ErrNotFound {
http.Error(w, "The beer you requested does not exist.", http.StatusNotFound)
return
Expand All @@ -102,13 +90,7 @@ func getBeer(s listing.Service) func(w http.ResponseWriter, r *http.Request, p h
// getBeerReviews returns a handler for GET /beers/:id/reviews requests
func getBeerReviews(s listing.Service) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ID, err := strconv.Atoi(p.ByName("id"))
if err != nil {
http.Error(w, fmt.Sprintf("%s is not a valid beer ID, it must be a number.", p.ByName("id")), http.StatusBadRequest)
return
}

reviews := s.GetBeerReviews(ID)
reviews := s.GetBeerReviews(p.ByName("id"))

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(reviews)
Expand Down

0 comments on commit 2b09b28

Please sign in to comment.