pg_received_message_repository.go
1.6 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
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 ReceivedMessageRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.ReceivedMessageRepository = (*ReceivedMessageRepository)(nil)
func NewReceivedMessageRepository(tx *pgTransaction.TransactionContext) *ReceivedMessageRepository {
return &ReceivedMessageRepository{
transactionContext: tx,
}
}
func (repo *ReceivedMessageRepository) SaveMessage(param *domain.ReceivedMessage) error {
message := &models.ReceivedMessage{
MessageId: param.MessageId,
MessageType: param.MessageType,
MessageBody: param.MessageBody,
OccurredOn: param.OccurredOn,
CreateAt: time.Now(),
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(message).
Insert()
return err
}
func (repo *ReceivedMessageRepository) FindMessage(messageId int64) (*domain.ReceivedMessage, error) {
tx := repo.transactionContext.PgTx
receivedMessageModel := new(models.ReceivedMessage)
query := tx.Model(receivedMessageModel).
Where("message_id = ?", messageId)
if err := query.First(); err != nil {
if err != pg.ErrNoRows {
return nil, err
}
}
message := &domain.ReceivedMessage{
MessageId: receivedMessageModel.MessageId,
MessageType: receivedMessageModel.MessageType,
MessageBody: receivedMessageModel.MessageBody,
OccurredOn: receivedMessageModel.OccurredOn,
CreateAt: receivedMessageModel.CreateAt,
}
return message, nil
}