forked from gotify/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
57 lines (50 loc) · 1.48 KB
/
client.go
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
package database
import (
"github.com/gotify/server/model"
"github.com/jinzhu/gorm"
)
// GetClientByID returns the client for the given id or nil.
func (d *GormDatabase) GetClientByID(id uint) (*model.Client, error) {
client := new(model.Client)
err := d.DB.Where("id = ?", id).Find(client).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if client.ID == id {
return client, err
}
return nil, err
}
// GetClientByToken returns the client for the given token or nil.
func (d *GormDatabase) GetClientByToken(token string) (*model.Client, error) {
client := new(model.Client)
err := d.DB.Where("token = ?", token).Find(client).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if client.Token == token {
return client, err
}
return nil, err
}
// CreateClient creates a client.
func (d *GormDatabase) CreateClient(client *model.Client) error {
return d.DB.Create(client).Error
}
// GetClientsByUser returns all clients from a user.
func (d *GormDatabase) GetClientsByUser(userID uint) ([]*model.Client, error) {
var clients []*model.Client
err := d.DB.Where("user_id = ?", userID).Find(&clients).Error
if err == gorm.ErrRecordNotFound {
err = nil
}
return clients, err
}
// DeleteClientByID deletes a client by its id.
func (d *GormDatabase) DeleteClientByID(id uint) error {
return d.DB.Where("id = ?", id).Delete(&model.Client{}).Error
}
// UpdateClient updates a client.
func (d *GormDatabase) UpdateClient(client *model.Client) error {
return d.DB.Save(client).Error
}