/
session.go
128 lines (117 loc) · 3.2 KB
/
session.go
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
package database
import (
"context"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/shogo82148/actions-notify-slack/gha-notify/internal/repository"
"github.com/shogo82148/actions-notify-slack/gha-notify/internal/service"
)
var _ repository.SessionPutter = (*SessionTable)(nil)
var _ repository.SessionGetter = (*SessionTable)(nil)
type SessionTable struct {
cfg *SessionTableConfig
}
type SessionTableConfig struct {
service.DynamoDBItemPutter
service.DynamoDBItemGetter
service.DynamoDBItemDeleter
TableName string
}
func NewSessionTable(cfg *SessionTableConfig) (*SessionTable, error) {
return &SessionTable{
cfg: cfg,
}, nil
}
// PutSession puts a session.
func (t *SessionTable) PutSession(ctx context.Context, input *repository.PutSessionInput) (*repository.PutSessionOutput, error) {
expiresAt := time.Now().Add(24 * time.Hour)
item := map[string]types.AttributeValue{
"session_id": &types.AttributeValueMemberS{
Value: input.SessionID,
},
"expires_at": &types.AttributeValueMemberN{
Value: strconv.FormatFloat(timeToUnixTime(expiresAt), 'f', -1, 64),
},
}
if input.State != "" {
item["state"] = &types.AttributeValueMemberS{
Value: input.State,
}
}
if input.TeamID != "" {
item["team_id"] = &types.AttributeValueMemberS{
Value: input.TeamID,
}
}
if input.TeamName != "" {
item["team_name"] = &types.AttributeValueMemberS{
Value: input.TeamName,
}
}
_, err := t.cfg.PutItem(ctx, &dynamodb.PutItemInput{
TableName: aws.String(t.cfg.TableName),
Item: item,
})
if err != nil {
return nil, err
}
return nil, nil
}
// GetSession gets a session.
func (t *SessionTable) GetSession(ctx context.Context, input *repository.GetSessionInput) (*repository.GetSessionOutput, error) {
out, err := t.cfg.GetItem(ctx, &dynamodb.GetItemInput{
TableName: aws.String(t.cfg.TableName),
Key: map[string]types.AttributeValue{
"session_id": &types.AttributeValueMemberS{
Value: input.SessionID,
},
},
})
if err != nil {
return nil, err
}
if _, ok := out.Item["session_id"]; !ok {
return &repository.GetSessionOutput{}, nil
}
conv := new(attrConverter)
sessionID := conv.convertString(out.Item["session_id"])
var state string
if attr, ok := out.Item["state"]; ok {
state = conv.convertString(attr)
}
var teamID string
if attr, ok := out.Item["team_id"]; ok {
teamID = conv.convertString(attr)
}
var teamName string
if attr, ok := out.Item["team_name"]; ok {
teamName = conv.convertString(attr)
}
if conv.err != nil {
return nil, conv.err
}
return &repository.GetSessionOutput{
SessionID: sessionID,
State: state,
TeamID: teamID,
TeamName: teamName,
}, nil
}
// DeleteSession deletes a session.
func (t *SessionTable) DeleteSession(ctx context.Context, input *repository.DeleteSessionInput) (*repository.DeleteSessionOutput, error) {
_, err := t.cfg.DeleteItem(ctx, &dynamodb.DeleteItemInput{
TableName: aws.String(t.cfg.TableName),
Key: map[string]types.AttributeValue{
"session_id": &types.AttributeValueMemberS{
Value: input.SessionID,
},
},
})
if err != nil {
return nil, err
}
return nil, nil
}