@@ -2,10 +2,17 @@ package invitationstore
2
2
3
3
import (
4
4
"context"
5
+ "crypto/rand"
5
6
"database/sql"
7
+ "encoding/base32"
8
+ "strings"
9
+
10
+ "github.com/pkg/errors"
11
+ "github.com/volatiletech/sqlboiler/boil"
6
12
7
13
"github.com/ProgrammingLab/prolab-accounts/infra/record"
8
14
"github.com/ProgrammingLab/prolab-accounts/infra/store"
15
+ "github.com/ProgrammingLab/prolab-accounts/model"
9
16
)
10
17
11
18
type invitationStoreImpl struct {
@@ -21,6 +28,10 @@ func NewInvitationStore(ctx context.Context, db *sql.DB) store.InvitationStore {
21
28
}
22
29
}
23
30
31
+ const (
32
+ invitationCodeBytes = 32
33
+ )
34
+
24
35
func (s * invitationStoreImpl ) ListInvitations () ([]* record.Invitation , error ) {
25
36
panic ("not implemented" )
26
37
}
@@ -29,10 +40,36 @@ func (s *invitationStoreImpl) GetInvitation(id int64) (*record.Invitation, error
29
40
panic ("not implemented" )
30
41
}
31
42
32
- func (s * invitationStoreImpl ) CreateInvitation (email string ) (* record.Invitation , error ) {
33
- panic ("not implemented" )
43
+ func (s * invitationStoreImpl ) CreateInvitation (inviter model.UserID , email string ) (* record.Invitation , error ) {
44
+ code , err := generateInvitationCode ()
45
+ if err != nil {
46
+ return nil , err
47
+ }
48
+
49
+ inv := & record.Invitation {
50
+ Code : code ,
51
+ Email : email ,
52
+ InviterID : int64 (inviter ),
53
+ }
54
+ err = inv .Insert (s .ctx , s .db , boil .Infer ())
55
+ if err != nil {
56
+ return nil , errors .WithStack (err )
57
+ }
58
+
59
+ return inv , nil
34
60
}
35
61
36
62
func (s * invitationStoreImpl ) DeleteInvitation (id int64 ) error {
37
63
panic ("not implemented" )
38
64
}
65
+
66
+ func generateInvitationCode () (string , error ) {
67
+ b := make ([]byte , invitationCodeBytes )
68
+ _ , err := rand .Read (b )
69
+ if err != nil {
70
+ return "" , errors .WithStack (err )
71
+ }
72
+
73
+ enc := base32 .StdEncoding .WithPadding (base32 .NoPadding )
74
+ return strings .ToLower (enc .EncodeToString (b )), nil
75
+ }
0 commit comments