goipam/db/groups.go

92 lines
2.0 KiB
Go
Raw Normal View History

2024-02-22 10:18:59 +01:00
package db
import "log"
func CreateGroup(name string, email string, displayName string) error {
user := User{
Name: name,
Email: email,
DisplayName: displayName,
}
result := conn.Create(&user)
if result.Error != nil {
log.Printf("Could not add group %s: %v", name, result.Error)
return result.Error
} else {
log.Printf("Created group %s", name)
return nil
}
}
func GetGroupByName(name string) (Group, error) {
var group Group
result := conn.Where("name = ?", name).First(&group)
if result.Error != nil {
log.Printf("Could find group %s: %v", name, result.Error)
return group, result.Error
} else {
log.Printf("retrieved group %s", name)
return group, nil
}
}
func GetGroupByID(id int) (Group, error) {
var group Group
result := conn.Where("id = ?", id).First(&group)
if result.Error != nil {
log.Printf("Could find group %d: %v", id, result.Error)
return group, result.Error
} else {
log.Printf("retrieved group %d", id)
return group, nil
}
}
func GetAllGroups() ([]Group, error) {
var group []Group
result := conn.Find(&group)
if result.Error != nil {
log.Printf("Could find roles: %v", result.Error)
return group, result.Error
} else {
return group, nil
}
}
func SetGroupName(group Group, name string) error {
group.Name = name
result := conn.Save(&group)
if result.Error != nil {
log.Printf("Could find group %s: %v", name, result.Error)
return result.Error
} else {
log.Printf("retrieved group %s", name)
return nil
}
}
func SetGroupDisplayName(group Group, name string) error {
group.DisplayName = name
result := conn.Save(&group)
if result.Error != nil {
log.Printf("Could find group %s: %v", name, result.Error)
return result.Error
} else {
log.Printf("retrieved group %s", name)
return nil
}
}
func DeleteGroup(group Group) error {
result := conn.First(&group).Delete(&group)
if result.Error != nil {
log.Printf("Could find group %d: %v", group.ID, result.Error)
return result.Error
} else {
log.Printf("deleted group %d", group.ID)
return nil
}
}