pg_sent_notification_repository.go
6.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package repository
import (
"fmt"
"github.com/linmadan/egglib-go/utils/snowflake"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
)
type SentNotificationRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *SentNotificationRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(3)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *SentNotificationRepository) Save(sentNotification *domain.SentNotification) (*domain.SentNotification, error) {
tx := repository.transactionContext.PgTx
if sentNotification.Identify() == nil {
if nextId, err := repository.nextIdentify(); err != nil {
return sentNotification, err
} else {
sentNotification.SentNotificationId = nextId
}
if _, err := tx.QueryOne(
pg.Scan(&sentNotification.SentNotificationId, &sentNotification.Notification.NotificationId, &sentNotification.Receiver, &sentNotification.IsRead, &sentNotification.ReadTime),
"INSERT INTO sent_notifications (id, notification_id, receiver, is_read, read_time) VALUES (?, ?, ?, ?, ?) RETURNING id, notification_id, receiver, is_read, read_time",
sentNotification.SentNotificationId, sentNotification.Notification.NotificationId, sentNotification.Receiver, sentNotification.IsRead, sentNotification.ReadTime); err != nil {
return sentNotification, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(&sentNotification.SentNotificationId, &sentNotification.Notification.NotificationId, &sentNotification.Receiver, &sentNotification.IsRead, &sentNotification.ReadTime),
"UPDATE sent_notifications SET notification_id=?, receiver=?, is_read=?, read_time=? WHERE id=? RETURNING id, notification_id, receiver, is_read, read_time",
sentNotification.Notification.NotificationId, sentNotification.Receiver, sentNotification.IsRead, sentNotification.ReadTime, sentNotification.Identify()); err != nil {
return sentNotification, err
}
}
return sentNotification, nil
}
func (repository *SentNotificationRepository) Remove(sentNotification *domain.SentNotification) (*domain.SentNotification, error) {
tx := repository.transactionContext.PgTx
sentNotificationModel := new(models.SentNotification)
sentNotificationModel.Id = sentNotification.Identify().(int64)
if _, err := tx.Model(sentNotificationModel).WherePK().Delete(); err != nil {
return sentNotification, err
}
return sentNotification, nil
}
func (repository *SentNotificationRepository) FindOne(queryOptions map[string]interface{}) (*domain.SentNotification, error) {
tx := repository.transactionContext.PgTx
sentNotificationModel := new(models.SentNotification)
query := tx.Model(sentNotificationModel).Relation("Notification")
if sentNotificationId, ok := queryOptions["sentNotificationId"]; ok {
query = query.Where("sent_notification.id = ?", sentNotificationId)
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if sentNotificationModel.Id == 0 {
return nil, nil
} else {
return repository.transformPgModelToDomainModel(sentNotificationModel)
}
}
func (repository *SentNotificationRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SentNotification, error) {
tx := repository.transactionContext.PgTx
var sentNotificationModels []*models.SentNotification
sentNotifications := make([]*domain.SentNotification, 0)
query := tx.Model(&sentNotificationModels).Relation("Notification")
if receiver, ok := queryOptions["receiver"]; ok && (receiver != int64(0)) {
query = query.Where(`sent_notification.receiver @> '{"uid":?}'`, receiver)
}
if notificationType, ok := queryOptions["notificationType"]; ok && (notificationType != int(0)) {
query = query.Where("notification.notification_type = ?", notificationType)
}
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, sentNotifications, err
} else {
for _, sentNotificationModel := range sentNotificationModels {
if sentNotification, err := repository.transformPgModelToDomainModel(sentNotificationModel); err != nil {
return 0, sentNotifications, err
} else {
sentNotifications = append(sentNotifications, sentNotification)
}
}
return int64(count), sentNotifications, nil
}
}
func (repository *SentNotificationRepository) transformPgModelToDomainModel(sentNotificationModel *models.SentNotification) (*domain.SentNotification, error) {
var notification *domain.Notification
if sentNotificationModel.Notification == nil {
notification = nil
} else {
notification = &domain.Notification{
NotificationId: sentNotificationModel.Notification.Id,
NotificationType: sentNotificationModel.Notification.NotificationType,
NotificationTitle: sentNotificationModel.Notification.NotificationTitle,
NotificationContent: sentNotificationModel.Notification.NotificationContent,
NotificationTime: sentNotificationModel.Notification.NotificationTime,
ExternalResourceType: sentNotificationModel.Notification.ExternalResourceType,
ExternalResource: sentNotificationModel.Notification.ExternalResource,
}
}
return &domain.SentNotification{
SentNotificationId: sentNotificationModel.Id,
Notification: notification,
Receiver: sentNotificationModel.Receiver,
IsRead: sentNotificationModel.IsRead,
ReadTime: sentNotificationModel.ReadTime,
}, nil
}
func NewSentNotificationRepository(transactionContext *pgTransaction.TransactionContext) (*SentNotificationRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &SentNotificationRepository{
transactionContext: transactionContext,
}, nil
}
}