/
get_article.go
61 lines (48 loc) · 1.4 KB
/
get_article.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
package usecase
import (
"article-service/domain/article/payload"
articlePb "article-service/pb/article"
"context"
"errors"
"time"
"article-service/lib/helper"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
)
func (s *service) GetArticle(ctx context.Context, req *payload.GetArticleRequest) (*articlePb.GetArticleReturn, error) {
var articleReturn articlePb.GetArticleReturn
tracestr := "article.service.GetArticle"
select {
case <-ctx.Done():
return nil, helper.Error(codes.Aborted, tracestr+": Request Aborted", nil)
default:
}
span, ctx := opentracing.StartSpanFromContext(ctx, tracestr)
defer span.Finish()
s.logger.WithFields(logrus.Fields{
"ID": req.Id,
}).Info("incoming request payload")
if req.Id == "" {
return &articleReturn, errors.New("Invalid ID")
}
result, err := s.articleRepo.GetArticle(ctx, req.Id)
if err != nil {
return &articleReturn, helper.Error(codes.Internal, tracestr, err)
}
articleReturn.Id = result.Id
if result.Author != nil {
articleReturn.Author = *result.Author
}
if result.Title != nil {
articleReturn.Title = *result.Title
}
if result.Body != nil {
articleReturn.Body = *result.Body
}
if result.CreatedAt != nil {
t := *result.CreatedAt
articleReturn.CreatedAt = t.Format(time.RFC3339)
}
return &articleReturn, nil
}