generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailrenderer.go
74 lines (67 loc) · 2.1 KB
/
emailrenderer.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
package emailrenderer
import (
"bytes"
"context"
di "github.com/fluffy-bunny/fluffy-dozm-di"
contracts_email "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/email"
fluffycore_utils "github.com/fluffy-bunny/fluffycore/utils"
status "github.com/gogo/status"
codes "google.golang.org/grpc/codes"
)
type (
service struct {
config *contracts_email.EmailConfig
}
)
var stemService = (*service)(nil)
func init() {
var _ contracts_email.IEmailRenderer = stemService
}
func (s *service) Ctor(config *contracts_email.EmailConfig) (contracts_email.IEmailRenderer, error) {
return &service{
config: config,
}, nil
}
func AddSingletonIEmailRenderer(cb di.ContainerBuilder) {
di.AddSingleton[contracts_email.IEmailRenderer](cb, stemService.Ctor)
}
func (s *service) validateRenderEmailRequest(request *contracts_email.RenderEmailRequest) error {
if request == nil {
return status.Error(codes.InvalidArgument, "request is nil")
}
if request.Data == nil {
request.Data = make(map[string]interface{})
}
if fluffycore_utils.IsEmptyOrNil(request.HtmlTemplate) {
return status.Error(codes.InvalidArgument, "HtmlTemplate is empty")
}
if fluffycore_utils.IsEmptyOrNil(request.TextTemplate) {
return status.Error(codes.InvalidArgument, "TextTemplate is empty")
}
if fluffycore_utils.IsEmptyOrNil(request.Data) {
return status.Error(codes.InvalidArgument, "Data is empty")
}
return nil
}
func (s *service) RenderEmail(ctx context.Context, request *contracts_email.RenderEmailRequest) (*contracts_email.RenderEmailResponse, error) {
err := s.validateRenderEmailRequest(request)
if err != nil {
return nil, err
}
streamWriter := new(bytes.Buffer)
err = s.config.TemplateEngine.ExecuteTemplate(streamWriter, request.HtmlTemplate, request.Data)
if err != nil {
return nil, err
}
html := streamWriter.Bytes()
streamWriter = new(bytes.Buffer)
err = s.config.TemplateEngine.ExecuteTemplate(streamWriter, request.TextTemplate, request.Data)
if err != nil {
return nil, err
}
text := streamWriter.Bytes()
return &contracts_email.RenderEmailResponse{
Html: string(html),
Text: string(text),
}, nil
}