Skip to content

Commit

Permalink
chore: gRPC lab TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
alice890308 authored and justin0u0 committed Mar 5, 2024
1 parent 4698cac commit bd87274
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 115 deletions.
37 changes: 9 additions & 28 deletions modules/comment/pb/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

33 changes: 6 additions & 27 deletions modules/comment/pb/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {}
}
92 changes: 32 additions & 60 deletions modules/comment/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

0 comments on commit bd87274

Please sign in to comment.