-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
83 lines (69 loc) · 2.58 KB
/
user.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package repository
import (
"github.com/plexmediamanager/micro-database/database"
"github.com/plexmediamanager/micro-database/models"
)
type UserRepository struct {}
// Create new user
func (*UserRepository) Create(model *models.User) (*models.User, error) {
return model, database.CreateRecord(database.CreationParameters {
Model: model,
CreateWithID: false,
})
}
// Update existing user
func (*UserRepository) Update(model *models.User, fields UpdatedFields) (*models.User, error) {
return model, database.UpdateRecord(model, fields)
}
// Soft delete existing user
func (*UserRepository) SoftDelete(model *models.User) (*models.User, error) {
return model, database.SoftDeleteRecord(model)
}
// Force delete existing user
func (*UserRepository) ForceDelete(model *models.User) (*models.User, error) {
return model, database.ForceDeleteRecord(model)
}
// Find single user by ID
func (repository *UserRepository) FindOneByID(id uint64) (*models.User, error) {
return repository.findOne(models.User{ ID: id })
}
// Find many users by ID
func (repository *UserRepository) FindManyByID(id ...uint64) ([]*models.User, error) {
return repository.findMany(models.KeyUserID + " IN (?)", id)
}
// Find single user by Username
func (repository *UserRepository) FindOneByUsername(username string) (*models.User, error) {
return repository.findOne(models.User{ Username: username })
}
// Find many users by Username
func (repository *UserRepository) FindManyByUsername(username ...string) ([]*models.User, error) {
return repository.findMany(models.KeyUserUsername + " IN (?)", username)
}
// Find single user by Email
func (repository *UserRepository) FindOneByEmail(email string) (*models.User, error) {
return repository.findOne(models.User{ Email: email })
}
// Find many users by E-Mail
func (repository *UserRepository) FindManyByEmail(email ...string) ([]*models.User, error) {
return repository.findMany(models.KeyUserEmail + " IN (?)", email)
}
// Find single user
func (*UserRepository) findOne(query interface{}) (*models.User, error) {
user := &models.User{}
err := database.GetRecord(&database.DatabaseQuery{
Output: user,
Query: query,
})
return user, err
}
// Find multiple users
func (*UserRepository) findMany(query interface{}, parameters interface{}) ([]*models.User, error) {
var users []*models.User
err := database.GetRecord(&database.DatabaseQuery{
Output: &users,
Query: query,
Parameters: parameters,
Multiple: true,
})
return users, err
}