-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30fb0f8
commit 4af63e1
Showing
7 changed files
with
229 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters