diff --git a/code/docker-compose.yml b/code/docker-compose.yml index 261f24f..fa26e95 100644 --- a/code/docker-compose.yml +++ b/code/docker-compose.yml @@ -109,6 +109,8 @@ services: DB_SERVICE_PORT: ${DB_SERVICE_PORT} COMMUNITY_SERVICE_HOST: community-service COMMUNITY_SERVICE_PORT: ${COMMUNITY_SERVICE_PORT} + COMMENT_SERVICE_HOST: comment-service + COMMENT_SERVICE_PORT: ${COMMENT_SERVICE_PORT} ports: - "${THREAD_SERVICE_PORT}:${THREAD_SERVICE_PORT}" networks: diff --git a/code/kubernetes/services/thread-service/deployment.yaml b/code/kubernetes/services/thread-service/deployment.yaml index 11b9ee9..d86098a 100644 --- a/code/kubernetes/services/thread-service/deployment.yaml +++ b/code/kubernetes/services/thread-service/deployment.yaml @@ -45,6 +45,13 @@ spec: configMapKeyRef: name: threadit-config key: COMMUNITY_SERVICE_PORT + - name: COMMENT_SERVICE_HOST + value: "comment-service" + - name: COMMENT_SERVICE_PORT + valueFrom: + configMapKeyRef: + name: threadit-config + key: COMMENT_SERVICE_PORT readinessProbe: tcpSocket: port: 50053 diff --git a/code/services/db-service/src/comments.go b/code/services/db-service/src/comments.go index aa6349e..2976324 100644 --- a/code/services/db-service/src/comments.go +++ b/code/services/db-service/src/comments.go @@ -16,7 +16,7 @@ func (s *DBServer) ListComments(ctx context.Context, req *dbpb.ListCommentsReque collection := s.Mongo.Collection("comments") filter := bson.M{} if req.GetThreadId() != "" { - filter["thread_id"] = req.GetThreadId() + filter["parent_id"] = req.GetThreadId() } cursor, err := collection.Find(ctx, filter, getFindOptions(req.Offset, req.Limit, req.GetSortBy())) if err != nil { diff --git a/code/services/thread-service/main.go b/code/services/thread-service/main.go index 3c20468..78087bd 100644 --- a/code/services/thread-service/main.go +++ b/code/services/thread-service/main.go @@ -1,8 +1,8 @@ package main import ( - "context" "fmt" + commentpb "gen/comment-service/pb" communitypb "gen/community-service/pb" dbpb "gen/db-service/pb" threadpb "gen/thread-service/pb" @@ -47,10 +47,15 @@ func main() { communityConn := connectGrpcClient("COMMUNITY_SERVICE_HOST", "COMMUNITY_SERVICE_PORT") defer communityConn.Close() + // connect to comment service + commentConn := connectGrpcClient("COMMENT_SERVICE_HOST", "COMMENT_SERVICE_PORT") + defer commentConn.Close() + // create thread service with database service threadService := &server.ThreadServer{ DBClient: dbpb.NewDBServiceClient(dbConn), CommunityClient: communitypb.NewCommunityServiceClient(communityConn), + CommentClient: commentpb.NewCommentServiceClient(commentConn), } // get env port @@ -71,13 +76,6 @@ func main() { ) threadpb.RegisterThreadServiceServer(grpcServer, threadService) - // hardcode a thread creation - threadService.CreateThread(context.Background(), &threadpb.CreateThreadRequest{ - CommunityId: "1", - Title: "Hello World", - Content: "This is a test thread", - }) - log.Printf("gRPC server is listening on :%s", port) if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) diff --git a/code/services/thread-service/src/server.go b/code/services/thread-service/src/server.go index 2e334a7..a4e84a0 100644 --- a/code/services/thread-service/src/server.go +++ b/code/services/thread-service/src/server.go @@ -2,6 +2,7 @@ package server import ( "context" + commentpb "gen/comment-service/pb" communitypb "gen/community-service/pb" dbpb "gen/db-service/pb" models "gen/models/pb" @@ -17,6 +18,7 @@ type ThreadServer struct { threadpb.UnimplementedThreadServiceServer CommunityClient communitypb.CommunityServiceClient DBClient dbpb.DBServiceClient + CommentClient commentpb.CommentServiceClient } const ( @@ -171,6 +173,24 @@ func (s *ThreadServer) DeleteThread(ctx context.Context, req *threadpb.DeleteThr return nil, err } + // find all comments from thread + comments, err := s.CommentClient.ListComments(ctx, &commentpb.ListCommentsRequest{ + ThreadId: &req.Id, + }) + if err != nil { + return nil, err + } + + // delete comments + for _, comment := range comments.Comments { + _, err = s.CommentClient.DeleteComment(ctx, &commentpb.DeleteCommentRequest{ + Id: comment.Id, + }) + if err != nil { + return nil, err + } + } + // delete thread _, err = s.DBClient.DeleteThread(ctx, &dbpb.DeleteThreadRequest{ Id: req.Id,