Skip to content

Commit 1ec3e25

Browse files
committed
updated
1 parent b66ef28 commit 1ec3e25

26 files changed

+1859
-28
lines changed

client/createPost_client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/wpcodevo/golang-mongodb/pb"
10+
"google.golang.org/grpc"
11+
)
12+
13+
type CreatePostClient struct {
14+
service pb.PostServiceClient
15+
}
16+
17+
func NewCreatePostClient(conn *grpc.ClientConn) *CreatePostClient {
18+
service := pb.NewPostServiceClient(conn)
19+
20+
return &CreatePostClient{service}
21+
}
22+
23+
func (createPostClient *CreatePostClient) CreatePost(args *pb.CreatePostRequest) {
24+
25+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*5000))
26+
defer cancel()
27+
28+
res, err := createPostClient.service.CreatePost(ctx, args)
29+
30+
if err != nil {
31+
log.Fatalf("CreatePost: %v", err)
32+
}
33+
34+
fmt.Println(res)
35+
}

client/deletePost_client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/wpcodevo/golang-mongodb/pb"
10+
"google.golang.org/grpc"
11+
)
12+
13+
type DeletePostClient struct {
14+
service pb.PostServiceClient
15+
}
16+
17+
func NewDeletePostClient(conn *grpc.ClientConn) *DeletePostClient {
18+
service := pb.NewPostServiceClient(conn)
19+
20+
return &DeletePostClient{service}
21+
}
22+
23+
func (deletePostClient *DeletePostClient) DeletePost(args *pb.PostRequest) {
24+
25+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*5000))
26+
defer cancel()
27+
28+
_, err := deletePostClient.service.DeletePost(ctx, args)
29+
30+
if err != nil {
31+
log.Fatalf("DeletePost: %v", err)
32+
}
33+
34+
fmt.Println("Post deleted successfully")
35+
}

client/getPost_client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/wpcodevo/golang-mongodb/pb"
10+
"google.golang.org/grpc"
11+
)
12+
13+
type GetPostClient struct {
14+
service pb.PostServiceClient
15+
}
16+
17+
func NewGetPostClient(conn *grpc.ClientConn) *GetPostClient {
18+
service := pb.NewPostServiceClient(conn)
19+
20+
return &GetPostClient{service}
21+
}
22+
23+
func (getPostClient *GetPostClient) GetPost(args *pb.PostRequest) {
24+
25+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*5000))
26+
defer cancel()
27+
28+
res, err := getPostClient.service.GetPost(ctx, args)
29+
30+
if err != nil {
31+
log.Fatalf("GetPost: %v", err)
32+
}
33+
34+
fmt.Println(res)
35+
}

client/listPosts_client.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"log"
8+
"time"
9+
10+
"github.com/wpcodevo/golang-mongodb/pb"
11+
"google.golang.org/grpc"
12+
)
13+
14+
type ListPostsClient struct {
15+
service pb.PostServiceClient
16+
}
17+
18+
func NewListPostsClient(conn *grpc.ClientConn) *ListPostsClient {
19+
service := pb.NewPostServiceClient(conn)
20+
21+
return &ListPostsClient{service}
22+
}
23+
24+
func (listPostsClient *ListPostsClient) ListPosts(args *pb.GetPostsRequest) {
25+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*5000))
26+
defer cancel()
27+
28+
stream, err := listPostsClient.service.GetPosts(ctx, args)
29+
if err != nil {
30+
log.Fatalf("ListPosts: %v", err)
31+
}
32+
33+
for {
34+
res, err := stream.Recv()
35+
36+
if err == io.EOF {
37+
break
38+
}
39+
40+
if err != nil {
41+
log.Fatalf("ListPosts: %v", err)
42+
}
43+
44+
fmt.Println(res)
45+
}
46+
47+
}

client/updatePost_client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/wpcodevo/golang-mongodb/pb"
10+
"google.golang.org/grpc"
11+
)
12+
13+
type UpdatePostClient struct {
14+
service pb.PostServiceClient
15+
}
16+
17+
func NewUpdatePostClient(conn *grpc.ClientConn) *UpdatePostClient {
18+
service := pb.NewPostServiceClient(conn)
19+
20+
return &UpdatePostClient{service}
21+
}
22+
23+
func (updatePostClient *UpdatePostClient) UpdatePost(args *pb.UpdatePostRequest) {
24+
25+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*5000))
26+
defer cancel()
27+
28+
res, err := updatePostClient.service.UpdatePost(ctx, args)
29+
30+
if err != nil {
31+
log.Fatalf("UpdatePost: %v", err)
32+
}
33+
34+
fmt.Println(res)
35+
}

cmd/client/main.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
}
3636

3737
// Sign In
38-
if true {
38+
if false {
3939
signInUserClient := client.NewSignInUserClient(conn)
4040

4141
credentials := &pb.SignInUserInput{
@@ -56,4 +56,68 @@ func main() {
5656

5757
}
5858

59+
// List Posts
60+
if false {
61+
listPostsClient := client.NewListPostsClient(conn)
62+
63+
var page int64 = 1
64+
var limit int64 = 10
65+
args := &pb.GetPostsRequest{
66+
Page: &page,
67+
Limit: &limit,
68+
}
69+
70+
listPostsClient.ListPosts(args)
71+
}
72+
73+
// Create Post
74+
if false {
75+
createPostClient := client.NewCreatePostClient(conn)
76+
77+
args := &pb.CreatePostRequest{
78+
Title: "My second gRPC post with joy",
79+
Content: "It's always good to learn new technologies",
80+
User: "62908e0a42a608d5aeae2f64",
81+
Image: "default.png",
82+
}
83+
84+
createPostClient.CreatePost(args)
85+
}
86+
87+
// Update Post
88+
if false {
89+
updatePostClient := client.NewUpdatePostClient(conn)
90+
91+
title := "My new updated title"
92+
args := &pb.UpdatePostRequest{
93+
Id: "629147ff3c92aed11d49394b",
94+
Post: &pb.UpdatePostBody{
95+
Title: &title,
96+
},
97+
}
98+
99+
updatePostClient.UpdatePost(args)
100+
}
101+
102+
// Get Post
103+
if true {
104+
getPostClient := client.NewGetPostClient(conn)
105+
106+
args := &pb.PostRequest{
107+
Id: "629169e00a6c7cfd24e2129d",
108+
}
109+
110+
getPostClient.GetPost(args)
111+
}
112+
113+
// Delete Post
114+
if false {
115+
deletePostClient := client.NewDeletePostClient(conn)
116+
117+
args := &pb.PostRequest{
118+
Id: "629147ff3c92aed11d49394b",
119+
}
120+
121+
deletePostClient.DeletePost(args)
122+
}
59123
}

cmd/server/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func main() {
109109

110110
defer mongoclient.Disconnect(ctx)
111111

112-
startGinServer(config)
113-
// startGrpcServer(config)
112+
// startGinServer(config)
113+
startGrpcServer(config)
114114
}
115115

116116
func startGrpcServer(config config.Config) {
@@ -124,10 +124,16 @@ func startGrpcServer(config config.Config) {
124124
log.Fatal("cannot create grpc userServer: ", err)
125125
}
126126

127+
postServer, err := gapi.NewGrpcPostServer(postCollection, postService)
128+
if err != nil {
129+
log.Fatal("cannot create grpc postServer: ", err)
130+
}
131+
127132
grpcServer := grpc.NewServer()
128133

129134
pb.RegisterAuthServiceServer(grpcServer, authServer)
130135
pb.RegisterUserServiceServer(grpcServer, userServer)
136+
pb.RegisterPostServiceServer(grpcServer, postServer)
131137
reflection.Register(grpcServer)
132138

133139
listener, err := net.Listen("tcp", config.GrpcServerAddress)

gapi/post-server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gapi
2+
3+
import (
4+
"github.com/wpcodevo/golang-mongodb/pb"
5+
"github.com/wpcodevo/golang-mongodb/services"
6+
"go.mongodb.org/mongo-driver/mongo"
7+
)
8+
9+
type PostServer struct {
10+
pb.UnimplementedPostServiceServer
11+
postCollection *mongo.Collection
12+
postService services.PostService
13+
}
14+
15+
func NewGrpcPostServer(postCollection *mongo.Collection, postService services.PostService) (*PostServer, error) {
16+
postServer := &PostServer{
17+
postCollection: postCollection,
18+
postService: postService,
19+
}
20+
21+
return postServer, nil
22+
}

gapi/rpc_create_post.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gapi
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/wpcodevo/golang-mongodb/models"
8+
"github.com/wpcodevo/golang-mongodb/pb"
9+
"google.golang.org/grpc/codes"
10+
"google.golang.org/grpc/status"
11+
"google.golang.org/protobuf/types/known/timestamppb"
12+
)
13+
14+
func (postServer *PostServer) CreatePost(ctx context.Context, req *pb.CreatePostRequest) (*pb.PostResponse, error) {
15+
16+
post := &models.CreatePostRequest{
17+
Title: req.GetTitle(),
18+
Content: req.GetContent(),
19+
Image: req.GetImage(),
20+
User: req.GetUser(),
21+
}
22+
23+
newPost, err := postServer.postService.CreatePost(post)
24+
25+
if err != nil {
26+
if strings.Contains(err.Error(), "title already exists") {
27+
return nil, status.Errorf(codes.AlreadyExists, err.Error())
28+
}
29+
30+
return nil, status.Errorf(codes.Internal, err.Error())
31+
}
32+
33+
res := &pb.PostResponse{
34+
Post: &pb.Post{
35+
Id: newPost.Id.Hex(),
36+
Title: newPost.Title,
37+
Content: newPost.Content,
38+
User: newPost.User,
39+
CreatedAt: timestamppb.New(newPost.CreateAt),
40+
UpdatedAt: timestamppb.New(newPost.UpdatedAt),
41+
},
42+
}
43+
return res, nil
44+
}

gapi/rpc_delete_post.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package gapi
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/wpcodevo/golang-mongodb/pb"
8+
"google.golang.org/grpc/codes"
9+
"google.golang.org/grpc/status"
10+
)
11+
12+
func (postServer *PostServer) DeletePost(ctx context.Context, req *pb.PostRequest) (*pb.DeletePostResponse, error) {
13+
postId := req.GetId()
14+
15+
if err := postServer.postService.DeletePost(postId); err != nil {
16+
if strings.Contains(err.Error(), "Id exists") {
17+
return nil, status.Errorf(codes.NotFound, err.Error())
18+
}
19+
return nil, status.Errorf(codes.Internal, err.Error())
20+
}
21+
22+
res := &pb.DeletePostResponse{
23+
Success: true,
24+
}
25+
26+
return res, nil
27+
}

0 commit comments

Comments
 (0)