Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions code/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions code/kubernetes/services/thread-service/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion code/services/db-service/src/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions code/services/thread-service/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions code/services/thread-service/src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,6 +18,7 @@ type ThreadServer struct {
threadpb.UnimplementedThreadServiceServer
CommunityClient communitypb.CommunityServiceClient
DBClient dbpb.DBServiceClient
CommentClient commentpb.CommentServiceClient
}

const (
Expand Down Expand Up @@ -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,
Expand Down
Loading