Skip to content

Commit

Permalink
makes uses of http.Status... codes instead of
Browse files Browse the repository at this point in the history
using int.
  • Loading branch information
MartinGallauner committed May 10, 2024
1 parent bdac325 commit e331df8
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions internal/handler_add_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ func (cfg *BookclubServer) handlerAddBook(w http.ResponseWriter, r *http.Request
body := AddBookRequest{}
err := decoder.Decode(&body)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error decoding parameters: %s", err))
respondWithError(w, http.StatusBadRequest, fmt.Sprintf("Error decoding parameters: %s", err))
return
}
book, err := cfg.AddBookToCollection(body.ISBN, body.UserId)

if err != nil {
respondWithError(w, 400, "Unable to add the requested book")
respondWithError(w, http.StatusBadRequest, "Unable to add the requested book")
return
}
respondWithJSON(w, 200, book) //TODO: reconsider response body
respondWithJSON(w, http.StatusOK, book) //TODO: reconsider response body
return
}

Expand Down
6 changes: 3 additions & 3 deletions internal/handler_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ func (cfg *BookclubServer) handlerLogin(w http.ResponseWriter, r *http.Request)
if err == gorm.ErrRecordNotFound {
persistedUser, err = cfg.CreateUser(gothUser.Name, gothUser.Email)
if err != nil {
respondWithError(w, 400, "Unable to create new user.")
respondWithError(w, http.StatusBadRequest, "Unable to create new user.")
}
}
jwt, err := cfg.JwtService.CreateToken("bookclub-access", int(persistedUser.ID))
if err != nil {
//TODO: logging
respondWithError(w, 400, "Unable to login user.")
respondWithError(w, http.StatusBadRequest, "Unable to login user.")
}
loginResponse := LoginResponse{Name: persistedUser.Name, Email: persistedUser.Email, Jwt: jwt}
respondWithJSON(w, 200, loginResponse)
respondWithJSON(w, http.StatusOK, loginResponse)
}

type LoginResponse struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/handler_create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ func (cfg *BookclubServer) handlerCreateUser(w http.ResponseWriter, r *http.Requ
request := CreateUserRequest{}
err := decoder.Decode(&request)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error decoding parameters: %s", err))
respondWithError(w, http.StatusBadRequest, fmt.Sprintf("Error decoding parameters: %s", err))
return
}

if request.Name == "" {
respondWithError(w, 400, "Empty username is not accepted.")
respondWithError(w, http.StatusBadRequest, "Empty username is not accepted.")
return
}

user, err := cfg.CreateUser(request.Name, request.Email)

if err != nil {
respondWithError(w, 400, "Unable to create the user")
respondWithError(w, http.StatusBadRequest, "Unable to create the user")
return
}
respondWithJSON(w, 200, user)
respondWithJSON(w, http.StatusOK, user)
return
}

Expand Down
4 changes: 2 additions & 2 deletions internal/handler_get_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ func (cfg *BookclubServer) handlerGetBookByISBN(w http.ResponseWriter, r *http.R
isbn := r.PathValue("isbn")
book, err := cfg.Client.FetchBook(isbn)
if err != nil {
respondWithError(w, 400, "Unable to fetch the requested book")
respondWithError(w, http.StatusBadRequest, "Unable to fetch the requested book")
return
}
respondWithJSON(w, 200, book)
respondWithJSON(w, http.StatusOK, book)
return
}
4 changes: 2 additions & 2 deletions internal/handler_get_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func (cfg *BookclubServer) handlerGetLinks(w http.ResponseWriter, r *http.Reques
}

if err != nil {
respondWithError(w, 400, "Unable to create user link")
respondWithError(w, http.StatusBadRequest, "Unable to create user link")
return
}
respondWithJSON(w, 200, result)
respondWithJSON(w, http.StatusOK, result)
return
}
6 changes: 3 additions & 3 deletions internal/handler_link_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ func (cfg *BookclubServer) handlerCreateLink(w http.ResponseWriter, r *http.Requ
request := LinkRequest{}
err := decoder.Decode(&request)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error decoding parameters: %s", err))
respondWithError(w, http.StatusBadRequest, fmt.Sprintf("Error decoding parameters: %s", err))
return
}
link, err := cfg.LinkUsers(request.SenderId, request.ReceiverId)
linkResponse := mapLinkResponse(link)
if err != nil {
respondWithError(w, 400, "Unable to create user link")
respondWithError(w, http.StatusBadRequest, "Unable to create user link")
return
}
respondWithJSON(w, 200, linkResponse)
respondWithJSON(w, http.StatusOK, linkResponse)
return

}
Expand Down
6 changes: 3 additions & 3 deletions internal/handler_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ func (cfg *BookclubServer) handlerSearch(w http.ResponseWriter, r *http.Request)
body := AddBookRequest{}
err := decoder.Decode(&body)
if err != nil {
respondWithError(w, 400, fmt.Sprintf("Error decoding parameters: %s", err))
respondWithError(w, http.StatusBadRequest, fmt.Sprintf("Error decoding parameters: %s", err))
return
}
//TODO: remove ID from user response
users, err := cfg.SearchBookInNetwork(body.UserId, body.ISBN)
if err != nil {
respondWithError(w, 404, "Book is not available in the users network.")
respondWithError(w, http.StatusNotFound, "Book is not available in the users network.")
}

var responseBody []UserResponse
Expand All @@ -28,7 +28,7 @@ func (cfg *BookclubServer) handlerSearch(w http.ResponseWriter, r *http.Request)

searchResponse := SearchResponse{body.ISBN, responseBody}

respondWithJSON(w, 200, searchResponse)
respondWithJSON(w, http.StatusOK, searchResponse)
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
dat, err := json.Marshal(payload)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(code)
Expand Down

0 comments on commit e331df8

Please sign in to comment.