Skip to content

Commit

Permalink
feat: user repository can update multiple columns
Browse files Browse the repository at this point in the history
  • Loading branch information
xappyyy committed Dec 25, 2023
1 parent 7d331fe commit 7b5a63a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/server/repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (repo *UserRepository) Checkin(checkin *domain.Checkin) error {
return nil
}

// Update column of user in database with column_name and value
// Update a column of user in database with column_name and value
func (repo *UserRepository) UpdateColumn(user *domain.User, column_name string, value interface{}) error {
result := repo.db.Model(user).Clauses(clause.Returning{}).Update(column_name, value)
if result.Error != nil {
Expand All @@ -43,6 +43,15 @@ func (repo *UserRepository) UpdateColumn(user *domain.User, column_name string,
return nil
}

// Update some columns of user in database
func (repo *UserRepository) UpdateMultipleColumns(user *domain.User, columns map[string]interface{}) error {
result := repo.db.Model(user).Clauses(clause.Returning{}).Updates(columns)
if result.Error != nil {
return result.Error
}
return nil
}

// Get user by id
func (repo *UserRepository) GetById(user *domain.User, id string) error {
result := repo.db.First(user, "user_id = ?", id)
Expand Down
1 change: 1 addition & 0 deletions src/usecase/user_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type UserRepository interface {
CreateUser(*domain.User) error
Checkin(*domain.Checkin) error
UpdateColumn(*domain.User, string, interface{}) error
UpdateMultipleColumns(*domain.User, map[string]interface{}) error
GetById(*domain.User, string) error
GetWithCheckins(*domain.User, string) error
}
Expand Down

0 comments on commit 7b5a63a

Please sign in to comment.