add: Safe func to user model

This commit is contained in:
$(pass /github/name)
2024-09-07 10:36:21 +02:00
parent f99ff57275
commit c3944cb4aa
2 changed files with 45 additions and 7 deletions

View File

@@ -24,8 +24,7 @@ type UserController struct {
} }
type RegistrationData struct { type RegistrationData struct {
User models.User `json:"user"` User models.User `json:"user"`
Password string `json:"password"`
} }
func (uc *UserController) CurrentUserHandler(c *gin.Context) { func (uc *UserController) CurrentUserHandler(c *gin.Context) {
@@ -34,8 +33,6 @@ func (uc *UserController) CurrentUserHandler(c *gin.Context) {
logger.Error.Printf("Error getting user_id from header") logger.Error.Printf("Error getting user_id from header")
} }
userID := userIDString.(float64) userID := userIDString.(float64)
logger.Error.Printf("UserIDINt64: %v", userID)
logger.Error.Printf("c.Get Value: %v", userIDString)
user, err := uc.Service.GetUserByID(int64(userID)) user, err := uc.Service.GetUserByID(int64(userID))
if err != nil { if err != nil {
logger.Error.Printf("Error retrieving valid user: %v", err) logger.Error.Printf("Error retrieving valid user: %v", err)
@@ -43,7 +40,7 @@ func (uc *UserController) CurrentUserHandler(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, user) c.JSON(http.StatusOK, user.Safe())
} }
func (uc *UserController) LoginUser(c *gin.Context) { func (uc *UserController) LoginUser(c *gin.Context) {
@@ -126,7 +123,6 @@ func (uc *UserController) RegisterUser(c *gin.Context) {
// logger.Info.Printf("REGISTERING user: %#v", regData.User) // logger.Info.Printf("REGISTERING user: %#v", regData.User)
regData.User.RoleID = constants.Roles.Member regData.User.RoleID = constants.Roles.Member
regData.User.Password = regData.Password
// Register User // Register User
id, token, err := uc.Service.RegisterUser(&regData.User) id, token, err := uc.Service.RegisterUser(&regData.User)

View File

@@ -16,7 +16,7 @@ type User struct {
Phone string `json:"phone" validate:"omitempty,omitnil"` Phone string `json:"phone" validate:"omitempty,omitnil"`
Notes *string `json:"notes"` Notes *string `json:"notes"`
FirstName string `gorm:"not null" json:"first_name" validate:"required"` FirstName string `gorm:"not null" json:"first_name" validate:"required"`
Password string `json:"-" required_unless=RoleID 0` Password string `json:"password" validate:"required_unless=RoleID 0"`
Email string `gorm:"unique;not null" json:"email" validate:"required,email"` Email string `gorm:"unique;not null" json:"email" validate:"required,email"`
LastName string `gorm:"not null" json:"last_name" validate:"required"` LastName string `gorm:"not null" json:"last_name" validate:"required"`
ProfilePicture string `json:"profile_picture" validate:"omitempty,omitnil,image"` ProfilePicture string `json:"profile_picture" validate:"omitempty,omitnil,image"`
@@ -50,3 +50,45 @@ func (u *User) PasswordMatches(plaintextPassword string) (bool, error) {
return match, nil return match, nil
} }
func (u *User) Safe() map[string]interface{} {
return map[string]interface{}{
"email": u.Email,
"first_name": u.FirstName,
"last_name": u.LastName,
"phone": u.Phone,
"notes": u.Notes,
"address": u.Address,
"zip_code": u.ZipCode,
"city": u.City,
"status": u.Status,
"id": u.ID,
"role_id": u.RoleID,
"date_of_birth": u.DateOfBirth,
"membership": map[string]interface{}{
"id": u.Membership.ID,
"start_date": u.Membership.StartDate,
"end_date": u.Membership.EndDate,
"status": u.Membership.Status,
"subscription_model": map[string]interface{}{
"id": u.Membership.SubscriptionModel.ID,
"name": u.Membership.SubscriptionModel.Name,
"details": u.Membership.SubscriptionModel.Details,
"conditions": u.Membership.SubscriptionModel.Conditions,
"monthly_fee": u.Membership.SubscriptionModel.MonthlyFee,
"hourly_rate": u.Membership.SubscriptionModel.HourlyRate,
"included_per_year": u.Membership.SubscriptionModel.IncludedPerYear,
"included_per_month": u.Membership.SubscriptionModel.IncludedPerMonth,
},
},
"bank_account": map[string]interface{}{
"id": u.BankAccount.ID,
"mandate_date_signed": u.BankAccount.MandateDateSigned,
"bank": u.BankAccount.Bank,
"account_holder_name": u.BankAccount.AccountHolderName,
"iban": u.BankAccount.IBAN,
"bic": u.BankAccount.BIC,
"mandate_reference": u.BankAccount.MandateReference,
},
}
}