diff --git a/wwwshop/controller/usercontroller/usercontroller.go b/wwwshop/controller/usercontroller/usercontroller.go index 6509894..09d6e07 100644 --- a/wwwshop/controller/usercontroller/usercontroller.go +++ b/wwwshop/controller/usercontroller/usercontroller.go @@ -3,11 +3,11 @@ package usercontroller import ( d "WWWShop/wwwshop/dao/database" u "WWWShop/wwwshop/dao/usermanager" + "bytes" "encoding/json" "fmt" "net/http" - "strings" - "bytes" + "strings" ) type UserController struct{} @@ -21,30 +21,35 @@ func (self UserController) POST(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) err := decoder.Decode(&decoded_user_struct) if err != nil { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) json_response, _ := EncodeJSONResponse(NewResponseError("Unable to decode json")) fmt.Fprintf(w, json_response) - return + return } if decoded_user_struct.Username == "" { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get username")) fmt.Fprintf(w, json_response) - return + return } if decoded_user_struct.PasswordPlaintext == "" { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) json_response, _ := EncodeJSONResponse(NewResponseError("Unable to get password")) fmt.Fprintf(w, json_response) - return + return } user_manager := u.New(d.DB()) user, err := user_manager.AddWithPlainPassword(decoded_user_struct.Username, decoded_user_struct.PasswordPlaintext) if err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) json_response, _ := EncodeJSONResponse(NewResponseError(strings.Join( - []string{"Unable to create user ", err.Error()}, - ""))) - fmt.Fprintf(w, json_response) - return + []string{"Unable to create user ", err.Error()}, + ""))) + fmt.Fprintf(w, json_response) + return } - json_response, err := EncodeJSONResponse(user); + json_response, err := EncodeJSONResponse(user) fmt.Fprintf(w, json_response) }