From c3944cb4aa52105fb767ca64102648879fab4485 Mon Sep 17 00:00:00 2001 From: "$(pass /github/name)" <$(pass /github/email)> Date: Sat, 7 Sep 2024 10:36:21 +0200 Subject: [PATCH] add: Safe func to user model --- internal/controllers/user_controller.go | 8 ++--- internal/models/user.go | 44 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/internal/controllers/user_controller.go b/internal/controllers/user_controller.go index 97c20b5..a79e1b1 100644 --- a/internal/controllers/user_controller.go +++ b/internal/controllers/user_controller.go @@ -24,8 +24,7 @@ type UserController struct { } type RegistrationData struct { - User models.User `json:"user"` - Password string `json:"password"` + User models.User `json:"user"` } 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") } userID := userIDString.(float64) - logger.Error.Printf("UserIDINt64: %v", userID) - logger.Error.Printf("c.Get Value: %v", userIDString) user, err := uc.Service.GetUserByID(int64(userID)) if err != nil { logger.Error.Printf("Error retrieving valid user: %v", err) @@ -43,7 +40,7 @@ func (uc *UserController) CurrentUserHandler(c *gin.Context) { return } - c.JSON(http.StatusOK, user) + c.JSON(http.StatusOK, user.Safe()) } 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) regData.User.RoleID = constants.Roles.Member - regData.User.Password = regData.Password // Register User id, token, err := uc.Service.RegisterUser(®Data.User) diff --git a/internal/models/user.go b/internal/models/user.go index f82ed91..d593110 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -16,7 +16,7 @@ type User struct { Phone string `json:"phone" validate:"omitempty,omitnil"` Notes *string `json:"notes"` 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"` LastName string `gorm:"not null" json:"last_name" validate:"required"` ProfilePicture string `json:"profile_picture" validate:"omitempty,omitnil,image"` @@ -50,3 +50,45 @@ func (u *User) PasswordMatches(plaintextPassword string) (bool, error) { 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, + }, + } +}