added more general response handler

This commit is contained in:
$(pass /github/name)
2024-07-07 15:49:28 +02:00
parent ea29ec22bf
commit 90edd26ae0
4 changed files with 70 additions and 16 deletions

View File

@@ -7,6 +7,7 @@ import (
// "github.com/gorilla/mux"
"net/http"
// "strconv"
"GoMembership/internal/utils"
"GoMembership/pkg/logger"
)
@@ -20,29 +21,38 @@ func NewUserController(service services.UserService, emailService *services.Emai
}
func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
logger.Info.Println("registering user")
rh := utils.NewResponseHandler(w)
var user models.User
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
// http.Error(w, err.Error(), http.StatusBadRequest)
logger.Error.Printf("Couldn't decode Userdata: %v", err)
rh.RespondWithError(http.StatusBadRequest, "Couldn't decode userdata")
return
}
if err := uc.service.RegisterUser(&user); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
id, err := uc.service.RegisterUser(&user)
if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error.Printf("Couldn't register User: %v", err)
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User")
return
}
// Send welcome email to the user
if err := uc.emailService.SendWelcomeEmail(user); err != nil {
logger.Error.Printf("Failed to send welcome email to user: %v", err)
// rh.RespondWithError(http.StatusServiceUnavailable, "User creation succeeded, but failed to send welcome email to user")
}
// Notify admin of new user registration
if err := uc.emailService.NotifyAdminOfNewUser(user.Email); err != nil {
logger.Error.Printf("Failed to notify admin of new user registration: %v", err)
// rh.RespondWithError(http.StatusServiceUnavailable, "User creation succeeded, but failed to notify admin of new user registration")
}
w.WriteHeader(http.StatusCreated)
rh.RespondWithJSON(http.StatusCreated, map[string]interface{}{
"status": "success",
"id": id,
})
}
/* func (uc *UserController) LoginUser(w http.ResponseWriter, r *http.Request) {