Skip to content

Commit

Permalink
feature: create friend bill
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielllllllllllllll committed Aug 4, 2024
1 parent 30fb0f8 commit 4af63e1
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 173 deletions.
2 changes: 0 additions & 2 deletions protobuf/friend_bill.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ option go_package = "github.com/SpeedReach/monify";
import "google/api/annotations.proto";
import "google/protobuf/empty.proto";

message MEmpty{}

service FriendBillService {
rpc CreateFriendBill(CreateFriendBillRequest) returns (CreateFriendBillResponse) {
option (google.api.http) = {
Expand Down
290 changes: 119 additions & 171 deletions protobuf/gen/go/friend_bill.pb.go

Large diffs are not rendered by default.

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

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) CreateFriendBill(ctx context.Context, req *monify.CreateFriendBillRequest) (*monify.CreateFriendBillResponse, error) {
logger := ctx.Value(lib.LoggerContextKey{}).(*zap.Logger)
if req.Title == "" {
return nil, status.Error(codes.InvalidArgument, "Title is required")
}
if req.Amount <= 0 {
return nil, status.Error(codes.InvalidArgument, "Amount should be more than zero")
}
_, ok := ctx.Value(lib.UserIdContextKey{}).(uuid.UUID)
if !ok {
return nil, status.Error(codes.Unauthenticated, "Unauthorized")
}
relationId, err := uuid.Parse(req.RelationId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid relation id")
}

//Insert
friend_billId := uuid.New()
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)
_, err = db.ExecContext(ctx, `
INSERT INTO friend_bill (friend_bill_id, relation_id, amount, title, description)
VALUES ($1, $2, $3, $4, $5)
`, friend_billId, relationId, req.Amount, req.Title, req.Description)
if err != nil {
logger.Error("Insert values into friend_bill error.", zap.Error(err))
return nil, status.Error(codes.Internal, "")
}

return &monify.CreateFriendBillResponse{
FriendBillId: friend_billId.String(),
}, nil

}
9 changes: 9 additions & 0 deletions services/api/controllers/friend_bill/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package friend_bill

import (
monify "monify/protobuf/gen/go"
)

type Service struct {
monify.UnimplementedFriendBillServiceServer
}
2 changes: 2 additions & 0 deletions services/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
monify "monify/protobuf/gen/go"
"monify/services/api/controllers/auth"
"monify/services/api/controllers/friend"
"monify/services/api/controllers/friend_bill"
"monify/services/api/controllers/group"
"monify/services/api/controllers/group_bill"
"monify/services/api/controllers/user"
Expand Down Expand Up @@ -51,6 +52,7 @@ func SetupServices(g *grpc.Server, config ServerConfig) {
monify.RegisterGroupsBillServiceServer(g, group_bill.Service{})
monify.RegisterUserServiceServer(g, user.Service{})
monify.RegisterFriendServiceServer(g, friend.Service{})
monify.RegisterFriendBillServiceServer(g, friend_bill.Service{})
}

func setupInterceptor(resources infra.Resources, config ServerConfig) grpc.ServerOption {
Expand Down
49 changes: 49 additions & 0 deletions services/api/test/friend_bill_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package test

import (
"context"
monify "monify/protobuf/gen/go"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFriendBill(t *testing.T) {
// Set a friend relation
client := GetTestClient(t)
user1 := client.CreateTestUser()
_, err := client.UpdateUserNickId(context.Background(), &monify.UpdateUserNickIdRequest{NickId: "friend_bill_nickId1"})
assert.NoError(t, err)
user2 := client.CreateTestUser()
_, err = client.UpdateUserNickId(context.Background(), &monify.UpdateUserNickIdRequest{NickId: "friend_bill_nickId2"})
_, err = client.InviteFriend(context.TODO(), &monify.InviteFriendRequest{ReceiverNickId: "friend_bill_nickId1"})
assert.NoError(t, err)
client.SetTestUser(user1)
invitaions, err := client.ListFriendInvitation(context.TODO(), &monify.FriendEmpty{})
assert.NoError(t, err)
_, err = client.AcceptInvitation(context.TODO(), &monify.AcceptInvitationRequest{User1Id: user1, User2Id: user2, InviteId: invitaions.Invitation[0].InviteId})
assert.NoError(t, err)
friends, err := client.ListFriend(context.TODO(), &monify.FriendEmpty{})
assert.NoError(t, err)
var friendS_relationId []string
for _, friend := range friends.GetFriends() {
friendS_relationId = append(friendS_relationId, friend.RelationId)
}

// Test create friend bill
_, err = client.CreateFriendBill(context.TODO(), &monify.CreateFriendBillRequest{
RelationId: friendS_relationId[0],
Amount: 300,
Title: "test1",
Description: "test1",
})
assert.NoError(t, err)
_, err = client.CreateFriendBill(context.TODO(), &monify.CreateFriendBillRequest{
RelationId: friendS_relationId[0],
Amount: 0,
Title: "test2",
Description: "test2",
})
assert.Error(t, err) // Amount must not be zero

}
2 changes: 2 additions & 0 deletions services/api/test/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Client struct {
monify.GroupsBillServiceClient
monify.UserServiceClient
monify.FriendServiceClient
monify.FriendBillServiceClient
users *map[string]string
currentUser *string
}
Expand Down Expand Up @@ -111,6 +112,7 @@ func createClient(lis *bufconn.Listener) Client {
GroupsBillServiceClient: monify.NewGroupsBillServiceClient(conn),
UserServiceClient: monify.NewUserServiceClient(conn),
FriendServiceClient: monify.NewFriendServiceClient(conn),
FriendBillServiceClient: monify.NewFriendBillServiceClient(conn),
users: &users,
currentUser: &currentUser,
}
Expand Down

0 comments on commit 4af63e1

Please sign in to comment.