pg_message_personal_repository.go
2.3 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
package repository
import (
"time"
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 MessagePersonalRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.MessagePersonalRepository = (*MessagePersonalRepository)(nil)
func NewMessagePersonalRepository(tx *pgTransaction.TransactionContext) *MessagePersonalRepository {
return &MessagePersonalRepository{
transactionContext: tx,
}
}
func (repo *MessagePersonalRepository) TransformToDomain(param *models.MessagePersonal) *domain.MessagePersonal {
return &domain.MessagePersonal{
Id: param.Id,
Types: domain.MessageTypes(param.Types),
TargetUserId: param.TargetUserId,
ReadFlag: domain.MessageReadFlag(param.ReadFlag),
Title: param.Title,
Content: param.Content,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
Payload: param.Payload,
}
}
func (repo *MessagePersonalRepository) Save(param *domain.MessagePersonal) error {
message := &models.MessagePersonal{
Id: param.Id,
Types: string(param.Types),
TargetUserId: param.TargetUserId,
ReadFlag: string(param.ReadFlag),
Title: param.Title,
Content: param.Content,
Payload: param.Payload,
UpdatedAt: time.Now(),
CreatedAt: param.CreatedAt,
}
tx := repo.transactionContext.PgTx
if message.Id == 0 {
message.CreatedAt = time.Now()
_, err := tx.Model(message).Insert()
return err
}
_, err := tx.Model(message).WherePK().Update()
return err
}
func (repo *MessagePersonalRepository) Find(param map[string]interface{}) (int, []*domain.MessagePersonal, error) {
tx := repo.transactionContext.PgTx
var m []*models.MessagePersonal
query := tx.Model(&m).Limit(20)
if v, ok := param["targetUserId"]; ok {
query.Where("target_user_id=?", v)
}
if v, ok := param["types"]; ok {
query.Where("types=?", v)
}
if v, ok := param["payload"]; ok {
query.Where("payload @>?", v)
}
query.Order("id desc")
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var datas []*domain.MessagePersonal
for _, v := range m {
d := repo.TransformToDomain(v)
datas = append(datas, d)
}
return count, datas, nil
}