pg_users_repository.go
4.5 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/tiptok/godevp/pkg/domain/users"
domain "github.com/tiptok/godevp/pkg/domain/users"
"github.com/tiptok/godevp/pkg/infrastructure/pg/models"
)
type UsersRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *UsersRepository) nextIdentify() (int64, error) {
return 0, nil
}
func (repository *UsersRepository) Save(users *users.Users) (*users.Users, error) {
tx := repository.transactionContext.PgTx
if users.Identify() == nil {
_, err := repository.nextIdentify()
if err != nil {
return users, err
}
if _, err := tx.QueryOne(
pg.Scan(&users.Id, &users.Name, &users.Phone, &users.Passwd, pg.Array(&users.Roles), &users.Status, &users.AdminType, &users.CreateTime, &users.UpdateTime),
"INSERT INTO users (name, phone, passwd, roles, status, admin_type, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id, name, phone, passwd, roles, status, admin_type, create_time, update_time",
users.Name, users.Phone, users.Passwd, pg.Array(users.Roles), users.Status, users.AdminType, users.CreateTime, users.UpdateTime); err != nil {
return users, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(&users.Id, &users.Name, &users.Phone, &users.Passwd, pg.Array(&users.Roles), &users.Status, &users.AdminType, &users.CreateTime, &users.UpdateTime),
"UPDATE users SET name=?, phone=?, passwd=?, roles=?, status=?, admin_type=?, create_time=?, update_time=? WHERE id=? RETURNING id, name, phone, passwd, roles, status, admin_type, create_time, update_time",
users.Name, users.Phone, users.Passwd, pg.Array(users.Roles), users.Status, users.AdminType, users.CreateTime, users.UpdateTime, users.Identify()); err != nil {
return users, err
}
}
return users, nil
}
func (repository *UsersRepository) Remove(users *users.Users) (*users.Users, error) {
tx := repository.transactionContext.PgTx
usersModel := new(models.Users)
usersModel.Id = users.Identify().(int64)
if _, err := tx.Model(usersModel).WherePK().Delete(); err != nil {
return users, err
}
return users, nil
}
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*users.Users, error) {
tx := repository.transactionContext.PgTx
usersModel := new(models.Users)
query := tx.Model(usersModel)
if usersId, ok := queryOptions["usersId"]; ok {
query = query.Where("users.id = ?", usersId)
}
if phone, ok := queryOptions["phone"]; ok {
query = query.Where("users.phone = ?", phone)
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if usersModel.Id == 0 {
return nil, nil
} else {
return repository.transformPgModelToDomainModel(usersModel)
}
}
func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (int64, []*users.Users, error) {
tx := repository.transactionContext.PgTx
var usersModels []*models.Users
userss := make([]*users.Users, 0)
query := tx.Model(&usersModels)
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
query = query.Offset(offset)
}
} else {
query = query.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
query = query.Limit(limit)
}
} else {
query = query.Limit(20)
}
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
return 0, userss, err
} else {
for _, usersModel := range usersModels {
if users, err := repository.transformPgModelToDomainModel(usersModel); err != nil {
return 0, userss, err
} else {
userss = append(userss, users)
}
}
return int64(count), userss, nil
}
}
func (repository *UsersRepository) transformPgModelToDomainModel(usersModel *models.Users) (*domain.Users, error) {
return &users.Users{
Id: usersModel.Id,
Name: usersModel.Name,
Phone: usersModel.Phone,
Passwd: usersModel.Passwd,
Roles: usersModel.Roles,
Status: usersModel.Status,
AdminType: usersModel.AdminType,
CreateTime: usersModel.CreateTime,
UpdateTime: usersModel.UpdateTime,
}, nil
}
func NewUsersRepository(transactionContext *pgTransaction.TransactionContext) (*UsersRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &UsersRepository{
transactionContext: transactionContext,
}, nil
}
}