Skip to content

User APIs - #5

Open
hamid-JTG wants to merge 29 commits into
middlewarefrom
userAPIs
Open

User APIs#5
hamid-JTG wants to merge 29 commits into
middlewarefrom
userAPIs

Conversation

@hamid-JTG

Copy link
Copy Markdown
Owner

No description provided.

@hamid-JTG
hamid-JTG changed the base branch from main to middleware March 5, 2025 12:54
Comment thread controllers/users.go Outdated
Comment thread controllers/users.go Outdated
response, err := service.ListUsers()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve users"})
c.JSON(http.StatusInternalServerError, err.Error())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also return from here otherwise below code will run too check at all places

Comment thread controllers/users.go Outdated

func GetUser(service *services.Service) gin.HandlerFunc {
return func(c *gin.Context) {
userID, exists := c.Get("user_id")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a constant for this

Comment thread controllers/users.go Outdated
return func(c *gin.Context) {
userID, exists := c.Get("user_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": constants.ErrUnauthorized})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verify if this status code is correct

Comment thread services/users.go Outdated

func (s *Service) GetUserByID(userID uint) (*serializers.GetUserResponse, error) {
var user models.User
if err := s.DB.Where("id = ? AND deleted_at IS NULL", userID).First(&user).Error; err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if you can use preload to find office and residential details

Comment thread services/users.go Outdated
Comment on lines +38 to +49
if err := s.DB.Find(&users).Error; err != nil {
return nil, err
}

response := serializers.SerializeListUser(users)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select only required field and scan the data instead of find to get the desired result

Comment thread services/users.go Outdated
if err := tx.First(&user, "id = ?", userID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
tx.Rollback()
return nil, errors.New(constants.ErrUserNotFound)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return status code too it will be 404 in that case

Comment thread services/users.go Outdated

if err := tx.First(&user, "id = ?", userID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
tx.Rollback()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this above if so it is not required below

Comment thread services/users.go Outdated
Comment on lines +66 to +89
if err := tx.Model(&user).Update("deleted_at", time.Now()).Error; err != nil {
tx.Rollback()
return nil, err
}

var officeDetails []models.OfficeDetail
if err := tx.Where("user_id = ?", userID).Find(&officeDetails).Error; err != nil {
tx.Rollback()
return nil, err
}

for _, office := range officeDetails {
if err := tx.Model(&office).Update("deleted_at", time.Now()).Error; err != nil {
tx.Rollback()
return nil, err
}
}

var residentialDetails []models.ResidentialDetail
if err := tx.Where("user_id = ?", userID).Find(&residentialDetails).Error; err != nil {
tx.Rollback()
return nil, err
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using update you can use delete also check cascade delete to automatically delete other child record if parent is deleted

Comment thread services/users.go Outdated
Comment on lines +151 to +175
switch req.MaritalStatus {
case "single":
maritalStatus = 1
case "married":
maritalStatus = 2
default:
err = errors.New("invalid marital status value")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move this to model as it is also used in create API

Comment thread services/users.go Outdated
func (s *Service) UpdatePasswordByID(userID uint, req serializers.PasswordRequest) *errors.APIError {
var user models.User
if err := s.DB.Where("id = ?", userID).First(&user).Error; err != nil {
return errors.NewAPIError(constants.ErrUserNotFound, http.StatusNotFound)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is possible that there can be db error too due to db not available so handle user not found seperately and raise 500 for other cases

Comment thread services/users.go
Comment on lines +78 to +99
if err := tx.Preload("OfficeDetails").Preload("ResidentialDetails").First(&user, "id = ?", userID).Error; err != nil {
if err == gorm.ErrRecordNotFound {
tx.Rollback()
return nil, errors.NewAPIError(constants.ErrUserNotFound, http.StatusNotFound)
}
tx.Rollback()
return nil, errors.NewAPIError(constants.ErrFailedToRetrieveUser, http.StatusInternalServerError)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err := tx.Preload("OfficeDetails").Preload("ResidentialDetails").First(&user, "id = ?", userID).Error; err != nil {
if err == gorm.ErrRecordNotFound {
tx.Rollback()
return nil, errors.NewAPIError(constants.ErrUserNotFound, http.StatusNotFound)
}
tx.Rollback()
return nil, errors.NewAPIError(constants.ErrFailedToRetrieveUser, http.StatusInternalServerError)
}
if err := tx.Preload("OfficeDetails").Preload("ResidentialDetails").First(&user, "id = ?", userID).Error; err != nil {
tx.Rollback()
if err == gorm.ErrRecordNotFound {
return nil, errors.NewAPIError(constants.ErrUserNotFound, http.StatusNotFound)
}
return nil, errors.NewAPIError(constants.ErrInternalServerError, http.StatusInternalServerError)
}

Also update error message to ErrInternalServerError in case of 500

Comment thread services/users.go Outdated
Comment thread services/users.go Outdated
Comment on lines +137 to +151
case "male":
gender = 1
case "female":
gender = 2
case "other":
gender = 3
default:
err = errors.NewAPIError(constants.ErrInvalidGenderValue, http.StatusUnprocessableEntity)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this you can create a map in model and check whether the key exists in map. Same for marital status too

Comment thread services/users.go

func (s *Service) UpdateUserByID(userID uint, req *serializers.UpdateUserRequest) (*serializers.GetUserResponse, *errors.APIError) {
tx := s.DB.Begin()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add panic handler

Comment thread services/users.go Outdated
return nil, errors.NewAPIError(err.Error(), http.StatusInternalServerError)
}

return follower, nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the doc properly you have to written the list of user not just ids same for below too

Comment thread services/users.go Outdated

for _, followerID := range followerIDs {

if !s.checkIfUserExists(followerID) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is possible that there can be other err instead of user not found

Comment thread services/users.go Outdated
Comment on lines +216 to +214
if err == nil {
continue
}

if err != gorm.ErrRecordNotFound {
tx.Rollback()
return errors.NewAPIError(constants.ErrFailedToCheckFollowing, http.StatusInternalServerError)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err == nil {
continue
}
if err != gorm.ErrRecordNotFound {
tx.Rollback()
return errors.NewAPIError(constants.ErrFailedToCheckFollowing, http.StatusInternalServerError)
}
if err != nil && err != gorm.ErrRecordNotFound {
tx.Rollback()
return errors.NewAPIError(constants.ErrFailedToCheckFollowing, http.StatusInternalServerError)
} else if err == nil {
continue
}

Comment thread services/auth.go Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants