-
-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathuser.go
More file actions
79 lines (71 loc) · 1.98 KB
/
Copy pathuser.go
File metadata and controls
79 lines (71 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package database
import (
"github.com/gotify/server/v2/model"
"gorm.io/gorm"
)
// GetUserByName returns the user by the given name or nil.
func (d *GormDatabase) GetUserByName(name string) (*model.User, error) {
user := new(model.User)
err := d.DB.Where("name = ?", name).Find(user).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if user.Name == name {
return user, err
}
return nil, err
}
// GetUserByID returns the user by the given id or nil.
func (d *GormDatabase) GetUserByID(id uint) (*model.User, error) {
user := new(model.User)
err := d.DB.Find(user, id).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if user.ID == id {
return user, err
}
return nil, err
}
// CountUser returns the user count which satisfies the given condition.
func (d *GormDatabase) CountUser(condition ...interface{}) (int64, error) {
c := int64(-1)
handle := d.DB.Model(new(model.User))
if len(condition) == 1 {
handle = handle.Where(condition[0])
} else if len(condition) > 1 {
handle = handle.Where(condition[0], condition[1:]...)
}
err := handle.Count(&c).Error
return c, err
}
// GetUsers returns all users.
func (d *GormDatabase) GetUsers() ([]*model.User, error) {
var users []*model.User
err := d.DB.Find(&users).Error
return users, err
}
// DeleteUserByID deletes a user by its id.
func (d *GormDatabase) DeleteUserByID(id uint) error {
apps, _ := d.GetApplicationsByUser(id)
for _, app := range apps {
d.DeleteApplicationByID(app.ID)
}
clients, _ := d.GetClientsByUser(id)
for _, client := range clients {
d.DeleteClientByID(client.ID)
}
pluginConfs, _ := d.GetPluginConfByUser(id)
for _, conf := range pluginConfs {
d.DeletePluginConfByID(conf.ID)
}
return d.DB.Where("id = ?", id).Delete(&model.User{}).Error
}
// UpdateUser updates a user.
func (d *GormDatabase) UpdateUser(user *model.User) error {
return d.DB.Save(user).Error
}
// CreateUser creates a user.
func (d *GormDatabase) CreateUser(user *model.User) error {
return d.DB.Create(user).Error
}