Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code review for justforfunc episode #2

Merged
merged 8 commits into from
Sep 24, 2017

Conversation

campoy
Copy link
Contributor

@campoy campoy commented Sep 24, 2017

thanks for allowing me to review your code!

I'll be publishing a video explaining all of this on Monday on justforfunc.

In the meanwhile happy to answer any questions 😄

@douglasmakey douglasmakey merged commit ee258b6 into douglasmakey:master Sep 24, 2017
@douglasmakey
Copy link
Owner

Wow thanks for you PR, i am very excited for view your video the next Monday.

I really thank, regards !

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response{Data: data, Success: err == nil})
if err != nil {
log.Printf("could not encode response to output: %v", err)
Copy link

@vieiralucas vieiralucas Sep 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't status become 500 if it fails to encode?


c, err := h.storage.Save(url)
if err != nil {
return nil, http.StatusBadRequest, fmt.Errorf("Could not store in database: %v", err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If database fails, it's not users fault.
Shouldn't this be sending 500?

@tobiaskohlbau
Copy link

func responseHandler(h func(io.Writer, *http.Request) (interface{}, int, error)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		data, status, err := h(w, r)
		if err != nil {
			data = err.Error()
		}
		w.WriteHeader(status)
		w.Header().Set("Content-Type", "application/json")
		err = json.NewEncoder(w).Encode(response{Data: data, Success: err == nil})
		if err != nil {
			log.Printf("could not encode response to output: %v", err)
		}
	}
}

w.Header().Set("Content-Type", "application/json") should be called before w.WriteHeader(status) as stated within docs:

...
// Changing the header map after a call to WriteHeader (or
// Write) has no effect unless the modified headers are
// trailers.
...

Otherwise the content-type never gets populated.

return nil, err
}

_, err = p.db.Exec("update shortener set visited=$1, count=$2 where uid=$3", true, item.Count+1, id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the update, the visited status nor the count are getting reflected into the item pointer. Could be better use a UPDATE RETURNING in a single queryRow and Scan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants