-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathrpc_get_post.go
37 lines (31 loc) · 888 Bytes
/
rpc_get_post.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
package gapi
import (
"context"
"strings"
"github.com/wpcodevo/golang-mongodb/pb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (postServer *PostServer) GetPost(ctx context.Context, req *pb.PostRequest) (*pb.PostResponse, error) {
postId := req.GetId()
post, err := postServer.postService.FindPostById(postId)
if err != nil {
if strings.Contains(err.Error(), "Id exists") {
return nil, status.Errorf(codes.NotFound, err.Error())
}
return nil, status.Errorf(codes.Internal, err.Error())
}
res := &pb.PostResponse{
Post: &pb.Post{
Id: post.Id.Hex(),
Title: post.Title,
Content: post.Content,
Image: post.Image,
User: post.User,
CreatedAt: timestamppb.New(post.CreateAt),
UpdatedAt: timestamppb.New(post.UpdatedAt),
},
}
return res, nil
}