User APIs - #5
Conversation
| response, err := service.ListUsers() | ||
| if err != nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve users"}) | ||
| c.JSON(http.StatusInternalServerError, err.Error()) |
There was a problem hiding this comment.
Also return from here otherwise below code will run too check at all places
|
|
||
| func GetUser(service *services.Service) gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| userID, exists := c.Get("user_id") |
| return func(c *gin.Context) { | ||
| userID, exists := c.Get("user_id") | ||
| if !exists { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": constants.ErrUnauthorized}) |
There was a problem hiding this comment.
verify if this status code is correct
|
|
||
| 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 { |
There was a problem hiding this comment.
check if you can use preload to find office and residential details
| if err := s.DB.Find(&users).Error; err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| response := serializers.SerializeListUser(users) |
There was a problem hiding this comment.
select only required field and scan the data instead of find to get the desired result
| if err := tx.First(&user, "id = ?", userID).Error; err != nil { | ||
| if errors.Is(err, gorm.ErrRecordNotFound) { | ||
| tx.Rollback() | ||
| return nil, errors.New(constants.ErrUserNotFound) |
There was a problem hiding this comment.
return status code too it will be 404 in that case
|
|
||
| if err := tx.First(&user, "id = ?", userID).Error; err != nil { | ||
| if errors.Is(err, gorm.ErrRecordNotFound) { | ||
| tx.Rollback() |
There was a problem hiding this comment.
move this above if so it is not required below
| 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 | ||
| } | ||
|
|
There was a problem hiding this comment.
why using update you can use delete also check cascade delete to automatically delete other child record if parent is deleted
| switch req.MaritalStatus { | ||
| case "single": | ||
| maritalStatus = 1 | ||
| case "married": | ||
| maritalStatus = 2 | ||
| default: | ||
| err = errors.New("invalid marital status value") | ||
| } |
There was a problem hiding this comment.
you can move this to model as it is also used in create API
03d6f59 to
622f40d
Compare
| 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) |
There was a problem hiding this comment.
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
| 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) | ||
| } |
There was a problem hiding this comment.
| 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
| case "male": | ||
| gender = 1 | ||
| case "female": | ||
| gender = 2 | ||
| case "other": | ||
| gender = 3 | ||
| default: | ||
| err = errors.NewAPIError(constants.ErrInvalidGenderValue, http.StatusUnprocessableEntity) |
There was a problem hiding this comment.
for this you can create a map in model and check whether the key exists in map. Same for marital status too
|
|
||
| func (s *Service) UpdateUserByID(userID uint, req *serializers.UpdateUserRequest) (*serializers.GetUserResponse, *errors.APIError) { | ||
| tx := s.DB.Begin() | ||
|
|
| return nil, errors.NewAPIError(err.Error(), http.StatusInternalServerError) | ||
| } | ||
|
|
||
| return follower, nil |
There was a problem hiding this comment.
check the doc properly you have to written the list of user not just ids same for below too
|
|
||
| for _, followerID := range followerIDs { | ||
|
|
||
| if !s.checkIfUserExists(followerID) { |
There was a problem hiding this comment.
it is possible that there can be other err instead of user not found
| if err == nil { | ||
| continue | ||
| } | ||
|
|
||
| if err != gorm.ErrRecordNotFound { | ||
| tx.Rollback() | ||
| return errors.NewAPIError(constants.ErrFailedToCheckFollowing, http.StatusInternalServerError) | ||
| } |
There was a problem hiding this comment.
| 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 | |
| } |
No description provided.