pg_user_repository.go
3.2 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
131
package repository
import (
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)
type UserRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.UserRepository = (*UserRepository)(nil)
func NewUserRepository(tx *pgTransaction.TransactionContext) *UserRepository {
return &UserRepository{
transactionContext: tx,
}
}
func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
userModel := models.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&userModel).Insert()
if err != nil {
return nil, err
}
user.Id = userModel.Id
return user, nil
}
func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
userModel := models.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&userModel).WherePK().Update()
if err != nil {
return nil, err
}
return user, nil
}
func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) {
nowTime := time.Now()
user.DeleteAt = &nowTime
_, err := repo.Update(user)
return user, err
}
func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
tx := repo.transactionContext.PgTx
userModel := models.User{}
query := tx.Model(&userModel)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
err := query.First()
if err == pg.ErrNoRows {
return nil, ErrNoRows
}
if err != nil {
return nil, err
}
result := repo.TransformToUserDomain(&userModel)
return result, nil
}
func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*domain.User, error) {
tx := repo.transactionContext.PgTx
userModel := []models.User{}
query := tx.Model(&userModel).
Limit(20)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
if v, ok := queryOptions["limit"]; ok {
query.Limit(v.(int))
}
if v, ok := queryOptions["offset"]; ok {
query.Offset(v.(int))
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var resultList []*domain.User
for i := range userModel {
result := repo.TransformToUserDomain(&userModel[i])
resultList = append(resultList, result)
}
return cnt, resultList, nil
}
func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User {
return &domain.User{
Id: user.Id,
Account: user.Account,
AvatarUrl: user.AvatarUrl,
CompanyId: user.CompanyId,
AdminType: user.AdminType,
Name: user.Name,
Status: user.Status,
UpdateAt: user.UpdateAt,
CreateAt: user.CreateAt,
DeleteAt: user.DeleteAt,
}
}