44 lines
895 B
Go
44 lines
895 B
Go
package users
|
|
|
|
import (
|
|
"clortho/lib/db"
|
|
"errors"
|
|
)
|
|
|
|
// GetUserByID retrieves a user by ID
|
|
func GetUserByID(id uint) (*db.User, error) {
|
|
var user db.User
|
|
result := db.Connection.First(&user, id)
|
|
if result.RowsAffected == 0 {
|
|
return nil, errors.New("user not found")
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// UpdateUser updates a user's information
|
|
func UpdateUser(id uint, displayName string, admin bool) (*db.User, error) {
|
|
user, err := GetUserByID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Update the user fields
|
|
user.DisplayName = &displayName
|
|
user.Admin = admin
|
|
|
|
result := db.Connection.Save(user)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
// DeleteUser deletes a user by ID
|
|
func DeleteUser(id uint) error {
|
|
result := db.Connection.Delete(&db.User{}, id)
|
|
if result.RowsAffected == 0 {
|
|
return errors.New("user not found")
|
|
}
|
|
return result.Error
|
|
} |