Skip to content

Commit

Permalink
feature: delete friend
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielllllllllllllll committed Jun 29, 2024
1 parent 304a98a commit f9c1494
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 72 deletions.
2 changes: 2 additions & 0 deletions protobuf/friend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ service FriendService{
delete: "/v1/friend/reject_invitation/{invite_id}"
};
}

}

message DeleteFriendRequest{
Expand All @@ -56,6 +57,7 @@ message ListFriendResponse{
message Friend{
string friend_id = 1;
string name = 2;
string relation_id = 3;
}

message InviteFriendRequest{
Expand Down
148 changes: 79 additions & 69 deletions protobuf/gen/go/friend.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions services/api/controllers/friend/delete_friend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package friend

import (
"context"
"database/sql"
"monify/lib"
monify "monify/protobuf/gen/go"

"github.com/google/uuid"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (s Service) DeleteFriend(ctx context.Context, req *monify.DeleteFriendRequest) (*monify.FriendEmpty, error) {
logger := ctx.Value(lib.LoggerContextKey{}).(*zap.Logger)
_, ok := ctx.Value(lib.UserIdContextKey{}).(uuid.UUID)
if !ok {
return nil, status.Error(codes.Unauthenticated, "Unauthorized.")
}
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)

_, err := db.ExecContext(ctx, `DELETE FROM friend WHERE relation_id = $1`, req.RelationId)
if err != nil {
logger.Error("Delete friend error.", zap.Error(err))
return nil, status.Error(codes.Internal, "")
}
return &monify.FriendEmpty{}, nil

}
1 change: 0 additions & 1 deletion services/api/controllers/friend/friend_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
func (s Service) InviteFriend(ctx context.Context, req *monify.InviteFriendRequest) (*monify.InviteFriendResponse, error) {

logger := ctx.Value(lib.LoggerContextKey{}).(*zap.Logger)
userId := ctx.Value(lib.UserIdContextKey{}).(uuid.UUID)
userId, ok := ctx.Value(lib.UserIdContextKey{}).(uuid.UUID)
if !ok {
return nil, status.Error(codes.Unauthenticated, "Unauthorized.")
Expand Down
4 changes: 2 additions & 2 deletions services/api/controllers/friend/list_friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s Service) ListFriend(ctx context.Context, req *monify.FriendEmpty) (*moni
}
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)
query, err := db.QueryContext(ctx, `
SELECT user1_id, user2_id, name
SELECT user1_id, user2_id, name, relation_id
FROM friend, user_identity
WHERE (user1_id = $1 AND user2_id = user_id) OR (user2_id = $2 AND user1_id = user_id)`, userId, userId)
if err != nil {
Expand All @@ -37,7 +37,7 @@ func (s Service) ListFriend(ctx context.Context, req *monify.FriendEmpty) (*moni
var friend monify.Friend
var user1Id uuid.UUID
var user2Id uuid.UUID
if err = query.Scan(&user1Id, &user2Id, &friend.Name); err != nil {
if err = query.Scan(&user1Id, &user2Id, &friend.Name, &friend.RelationId); err != nil {
logger.Error("Scan error.", zap.Error(err))
return nil, status.Error(codes.Internal, "")
}
Expand Down
11 changes: 11 additions & 0 deletions services/api/test/friend_invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ func TestInviteFriend(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, len(friends.GetFriends()), 2)

// test delete friend
var friendS_relationId []string
for _, friend := range friends.GetFriends() {
friendS_relationId = append(friendS_relationId, friend.RelationId)
}
_, err = client.DeleteFriend(context.TODO(), &monify.DeleteFriendRequest{RelationId: friendS_relationId[0]})
assert.NoError(t, err)
friends, err = client.ListFriend(context.TODO(), &monify.FriendEmpty{})
assert.NoError(t, err)
assert.Equal(t, len(friends.GetFriends()), 1)

}

0 comments on commit f9c1494

Please sign in to comment.