switched to gin-gonic
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/internal/services"
|
||||
|
||||
// "github.com/gorilla/mux"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
// "strconv"
|
||||
"GoMembership/internal/utils"
|
||||
|
||||
"GoMembership/pkg/logger"
|
||||
)
|
||||
|
||||
@@ -31,15 +28,13 @@ func NewUserController(service services.UserService, emailService *services.Emai
|
||||
return &UserController{service, *emailService, consentService, bankAccountService, membershipService}
|
||||
}
|
||||
|
||||
func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
rh := utils.NewResponseHandler(w)
|
||||
func (uc *UserController) RegisterUser(c *gin.Context) {
|
||||
|
||||
var regData RegistrationData
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(®Data); err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
if err := c.ShouldBindJSON(®Data); err != nil {
|
||||
logger.Error.Printf("Couldn't decode Userdata: %v", err)
|
||||
rh.RespondWithError(http.StatusBadRequest, "Couldn't decode userdata")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Couldn't decode userdata"})
|
||||
return
|
||||
}
|
||||
logger.Info.Printf("registering user: %v", regData.User)
|
||||
@@ -47,9 +42,8 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
// Register User
|
||||
id, token, err := uc.service.RegisterUser(®Data.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")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't register User"})
|
||||
return
|
||||
}
|
||||
regData.User.ID = id
|
||||
@@ -58,7 +52,7 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = uc.bankAccountService.RegisterBankAccount(®Data.BankAccount)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Couldn't register bank account: %v", err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-BankAccount")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't register User-BankAccount"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -81,7 +75,7 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = uc.consentService.RegisterConsent(&consent)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Couldn't register consent: %v", err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-consent")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't register User-consent"})
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -90,48 +84,53 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = uc.membershipService.RegisterMembership(®Data.Membership)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Couldn't register membership: %v", err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Couldn't register User-membership")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Couldn't register User-membership"})
|
||||
return
|
||||
}
|
||||
|
||||
// Send notifications
|
||||
if err := uc.emailService.SendVerificationEmail(®Data.User, &token); err != nil {
|
||||
logger.Error.Printf("Failed to send email verification email to user: %v", err)
|
||||
// rh.RespondWithError(http.StatusServiceUnavailable, "User creation succeeded, but failed to send welcome email to user")
|
||||
// Proceed without returning error since user registration is successful
|
||||
}
|
||||
|
||||
// Notify admin of new user registration
|
||||
if err := uc.emailService.NotifyAdminOfNewUser(®Data.User); 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")
|
||||
// Proceed without returning error since user registration is successful
|
||||
}
|
||||
rh.RespondWithJSON(http.StatusCreated, map[string]interface{}{
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"status": "success",
|
||||
"id": regData.User.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func (uc *UserController) VerifyMailHandler(w http.ResponseWriter, r *http.Request) {
|
||||
rh := utils.NewResponseHandler(w)
|
||||
|
||||
token := r.URL.Query().Get("token")
|
||||
func (uc *UserController) VerifyMailHandler(c *gin.Context) {
|
||||
token := c.Query("token")
|
||||
if token == "" {
|
||||
logger.Error.Println("Missing token to verify mail")
|
||||
rh.RespondWithError(http.StatusNoContent, "Missing token")
|
||||
c.JSON(http.StatusNoContent, gin.H{"error": "Missing token"})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := uc.service.VerifyUser(&token)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Cannot verify user: %v", err)
|
||||
rh.RespondWithError(http.StatusUnauthorized, "Cannot verify user")
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Cannot verify user"})
|
||||
return
|
||||
}
|
||||
|
||||
membership, err := uc.membershipService.FindMembershipByUserID(user.ID)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Cannot get membership of user %v: %v", user.ID, err)
|
||||
rh.RespondWithError(http.StatusInternalServerError, "Cannot get Membership of user")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot get Membership of user"})
|
||||
return
|
||||
}
|
||||
|
||||
uc.emailService.SendWelcomeEmail(user, membership)
|
||||
c.Status(http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
/* func (uc *UserController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user