From bd872744453fcb4a42cb3f1ca0f27ca5745afeb9 Mon Sep 17 00:00:00 2001 From: Alice Date: Tue, 8 Mar 2022 18:38:15 +0800 Subject: [PATCH] chore: gRPC lab TODO --- modules/comment/pb/message.proto | 37 +++--------- modules/comment/pb/rpc.proto | 33 ++--------- modules/comment/service/service.go | 92 +++++++++++------------------- 3 files changed, 47 insertions(+), 115 deletions(-) diff --git a/modules/comment/pb/message.proto b/modules/comment/pb/message.proto index efa2000..9122e6d 100644 --- a/modules/comment/pb/message.proto +++ b/modules/comment/pb/message.proto @@ -20,43 +20,24 @@ message CommentInfo { google.protobuf.Timestamp updated_at = 5; } -message CreateCommentRequest { - string video_id = 1; - string content = 2; -} +// gRPC TODO: Define following message schemas. +message CreateCommentRequest {} -message CreateCommentResponse { - string id = 1; -} +message CreateCommentResponse {} -message ListCommentRequest { - string video_id = 1; - int32 limit = 2; - int32 offset = 3; -} +message ListCommentRequest {} -message ListCommentResponse { - repeated CommentInfo comments = 1; -} +message ListCommentResponse {} -message UpdateCommentRequest { - string id = 1; - string content = 2; -} +message UpdateCommentRequest {} -message UpdateCommentResponse { - CommentInfo comment = 1; -} +message UpdateCommentResponse {} -message DeleteCommentRequest { - string id = 1; -} +message DeleteCommentRequest {} message DeleteCommentResponse {} -message DeleteCommentByVideoIDRequest { - string video_id = 1; -} +message DeleteCommentByVideoIDRequest {} message DeleteCommentByVideoIDResponse {} diff --git a/modules/comment/pb/rpc.proto b/modules/comment/pb/rpc.proto index 74b2b55..02fcd09 100644 --- a/modules/comment/pb/rpc.proto +++ b/modules/comment/pb/rpc.proto @@ -14,35 +14,14 @@ service Comment { }; } - rpc ListComment(ListCommentRequest) returns (ListCommentResponse) { - option (google.api.http) = { - get: "/v1/comments/{video_id}" - response_body: "*" - }; - } + // gRPC TODO: Complete the following RPC service with correct parameter, return type and gateway option. + rpc ListComment() returns () {} - rpc CreateComment(CreateCommentRequest) returns (CreateCommentResponse) { - option (google.api.http) = { - post: "/v1/comments" - body: "*" - response_body: "*" - }; - } + rpc CreateComment() returns () {} - rpc UpdateComment(UpdateCommentRequest) returns (UpdateCommentResponse) { - option (google.api.http) = { - put: "/v1/comments/{id}" - body: "*" - response_body: "comment" - }; - } + rpc UpdateComment() returns () {} - rpc DeleteComment(DeleteCommentRequest) returns (DeleteCommentResponse) { - option (google.api.http) = { - delete: "/v1/comments/{id}" - response_body: "*" - }; - } + rpc DeleteComment() returns () {} - rpc DeleteCommentByVideoID(DeleteCommentByVideoIDRequest) returns (DeleteCommentByVideoIDResponse) {} + rpc DeleteCommentByVideoID() returns () {} } diff --git a/modules/comment/service/service.go b/modules/comment/service/service.go index 3fef1a0..224d17f 100644 --- a/modules/comment/service/service.go +++ b/modules/comment/service/service.go @@ -2,7 +2,6 @@ package service import ( "context" - "errors" "github.com/NTHU-LSALAB/NTHU-Distributed-System/modules/comment/dao" "github.com/NTHU-LSALAB/NTHU-Distributed-System/modules/comment/pb" @@ -28,84 +27,57 @@ func (s *service) Healthz(ctx context.Context, req *pb.HealthzRequest) (*pb.Heal return &pb.HealthzResponse{Status: "ok"}, nil } +/* +gRPC TODO: +1. Call dao API to list comment and check if any error happen. If so, return an nil response. +2. Create an array to store the return value. Remember to transform the data schema from dao into protobuf. + (Refer to the ToProto method defined in dao/comment.go. This method is used to transform data schema from dao into protobuf.) +3. Pack the array into correct format. +*/ func (s *service) ListComment(ctx context.Context, req *pb.ListCommentRequest) (*pb.ListCommentResponse, error) { - comments, err := s.commentDAO.ListByVideoID(ctx, req.GetVideoId(), int(req.GetLimit()), int(req.GetOffset())) - if err != nil { - return nil, err - } - - pbComments := make([]*pb.CommentInfo, 0, len(comments)) - for _, comment := range comments { - pbComments = append(pbComments, comment.ToProto()) - } - - return &pb.ListCommentResponse{Comments: pbComments}, nil } +/* +gRPC TODO: +1. Send gRPC to video server to check if the video id in request is valid. Think about which gRPC provided by video server to use? + If any error happened, return nil response and the error. +2. Create a comment with information in request. +3. Call dao API to create a new comment and do error handling. +4. Return the result. You may use .String() method to transform the return value of dao API to a string. +*/ func (s *service) CreateComment(ctx context.Context, req *pb.CreateCommentRequest) (*pb.CreateCommentResponse, error) { - if _, err := s.videoClient.GetVideo(ctx, &videopb.GetVideoRequest{ - Id: req.GetVideoId(), - }); err != nil { - return nil, err - } - - comment := &dao.Comment{ - VideoID: req.GetVideoId(), - Content: req.GetContent(), - } - - commentID, err := s.commentDAO.Create(ctx, comment) - if err != nil { - return nil, err - } - - return &pb.CreateCommentResponse{Id: commentID.String()}, nil } +/* +gRPC TODO: +1. Update a comment with information in request. +2. Call dao API to update a comment and do error handling. You need to handle comment not found error and other unknown error here. +3. Return the result. Don't forget to transform the data schema from dao into proto. + (Refer to the ToProto method defined in dao/comment.go. This method is used to transform data schema from dao into protobuf.) +*/ func (s *service) UpdateComment(ctx context.Context, req *pb.UpdateCommentRequest) (*pb.UpdateCommentResponse, error) { commentID, err := uuid.Parse(req.GetId()) if err != nil { return nil, ErrInvalidUUID } - - comment := &dao.Comment{ - ID: commentID, - Content: req.GetContent(), - } - if err := s.commentDAO.Update(ctx, comment); err != nil { - if errors.Is(err, dao.ErrCommentNotFound) { - return nil, ErrCommentNotFound - } - - return nil, err - } - - return &pb.UpdateCommentResponse{ - Comment: comment.ToProto(), - }, nil } +/* +gRPC TODO: +1. Call dao API to delete a comment and do error handling. You need to handle comment not found error and other unknown error here. +2. Return the response. +*/ func (s *service) DeleteComment(ctx context.Context, req *pb.DeleteCommentRequest) (*pb.DeleteCommentResponse, error) { commentID, err := uuid.Parse(req.GetId()) if err != nil { return nil, ErrInvalidUUID } - - if err := s.commentDAO.Delete(ctx, commentID); err != nil { - if errors.Is(err, dao.ErrCommentNotFound) { - return nil, ErrCommentNotFound - } - - return nil, err - } - - return &pb.DeleteCommentResponse{}, nil } +/* +gRPC TODO: +1. Call dao API to delete comments by video id and do error handling. You need to do error handling here. +2. Return the response. +*/ func (s *service) DeleteCommentByVideoID(ctx context.Context, req *pb.DeleteCommentByVideoIDRequest) (*pb.DeleteCommentByVideoIDResponse, error) { - if err := s.commentDAO.DeleteByVideoID(ctx, req.GetVideoId()); err != nil { - return nil, err - } - - return &pb.DeleteCommentByVideoIDResponse{}, nil }