-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
49 lines (38 loc) · 1.39 KB
/
link.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
package handle
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/MicahParks/magiclinksdev/magiclink"
"github.com/MicahParks/magiclinksdev/model"
"github.com/MicahParks/magiclinksdev/network/middleware/ctxkey"
)
// HandleLinkCreate handles the link creation endpoint.
func (s *Server) HandleLinkCreate(ctx context.Context, req model.ValidLinkCreateRequest) (response model.LinkCreateResponse, err error) {
linkArgs := req.LinkArgs
magicLinkResp, err := s.createLink(ctx, linkArgs)
if err != nil {
return model.LinkCreateResponse{}, fmt.Errorf("failed to create magic link: %w", err)
}
resp := model.LinkCreateResponse{
LinkCreateResults: model.LinkCreateResults{
MagicLink: magicLinkResp.MagicLink.String(),
Secret: magicLinkResp.Secret,
},
RequestMetadata: model.RequestMetadata{
UUID: ctx.Value(ctxkey.RequestUUID).(uuid.UUID),
},
}
return resp, nil
}
func (s *Server) createLink(ctx context.Context, linkArgs model.ValidLinkCreateArgs) (magiclink.CreateResponse, error) {
magicLinkCreateArgs, err := s.createLinkArgs(ctx, linkArgs)
if err != nil {
return magiclink.CreateResponse{}, fmt.Errorf("failed to create magic link create args: %w", err)
}
magicLinkResp, err := s.MagicLink.NewLink(ctx, magicLinkCreateArgs)
if err != nil {
return magiclink.CreateResponse{}, fmt.Errorf("failed to create magic link: %w", err)
}
return magicLinkResp, nil
}