Skip to content

Commit

Permalink
feature: modify friend bill
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielllllllllllllll committed Aug 4, 2024
1 parent 45122bc commit d0fd93c
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 73 deletions.
7 changes: 4 additions & 3 deletions protobuf/friend_bill.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ message DeleteFriendBillRequest {

message ModifyFriendBillRequest {
string friend_bill_id = 1;
double amount = 2;
string title = 3;
string description = 4;
string relation_id = 2;
double amount = 3;
string title = 4;
string description = 5;
}

message ListFriendBillRequest {
Expand Down
116 changes: 63 additions & 53 deletions protobuf/gen/go/friend_bill.pb.go

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

12 changes: 4 additions & 8 deletions services/api/controllers/friend_bill/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,21 @@ func (s Service) CreateFriendBill(ctx context.Context, req *monify.CreateFriendB
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")
if req.Amount == 0 {
return nil, status.Error(codes.InvalidArgument, "Amount should not be 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, `
_, 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)
`, friend_billId, req.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, "")
Expand Down
55 changes: 55 additions & 0 deletions services/api/controllers/friend_bill/modify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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"
"google.golang.org/protobuf/types/known/emptypb"
)

func (s Service) ModifyFriendBill(ctx context.Context, req *monify.ModifyFriendBillRequest) (*emptypb.Empty, 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)

//START transaction
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadUncommitted})
if err != nil {
logger.Error("Failed to begin transaction", zap.Error(err))
return nil, status.Error(codes.Internal, "Internal")
}
defer tx.Rollback()

// Delete
_, err = tx.ExecContext(ctx, `DELETE FROM friend_bill WHERE friend_bill_id = $1`, req.FriendBillId)
if err != nil {
logger.Error("Delete friend bill error. (Modify)", zap.Error(err))
return nil, status.Error(codes.Internal, "")
}

// Insert
_, err = tx.ExecContext(ctx, `
INSERT INTO friend_bill (friend_bill_id, relation_id, amount, title, description)
VALUES ($1, $2, $3, $4, $5)
`, req.FriendBillId, req.RelationId, req.Amount, req.Title, req.Description)
if err != nil {
logger.Error("Insert values into friend_bill error. (Modify)", zap.Error(err))
return nil, status.Error(codes.Internal, "")
}

//Commit
if err = tx.Commit(); err != nil {
logger.Error("Failed to commit transaction", zap.Error(err))
return nil, status.Error(codes.Internal, "Internal")
}
return &emptypb.Empty{}, nil
}
37 changes: 28 additions & 9 deletions services/api/test/friend_bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,36 @@ func TestFriendBill(t *testing.T) {
assert.Error(t, err) // Amount must not be zero

// Test list friend bill
friend_bills, err := client.ListFriendBill(context.TODO(), &monify.ListFriendBillRequest{RelationId: friendS_relationId[0]})
res1, err := client.ListFriendBill(context.TODO(), &monify.ListFriendBillRequest{RelationId: friendS_relationId[0]})
assert.NoError(t, err)
assert.Equal(t, len(friend_bills.GetFriendBills()), 2)
assert.Equal(t, len(res1.GetFriendBills()), 2)
assert.Equal(t, 300.0, res1.FriendBills[0].Amount)
assert.Equal(t, "test1", res1.FriendBills[0].Title)
assert.Equal(t, "test1", res1.FriendBills[0].Description)
assert.Equal(t, 500.0, res1.FriendBills[1].Amount)
assert.Equal(t, "test2", res1.FriendBills[1].Title)
assert.Equal(t, "test2", res1.FriendBills[1].Description)

// Test delete friend bill
var friend_billId []string
for _, friend_bill := range friend_bills.GetFriendBills() {
friend_billId = append(friend_billId, friend_bill.FriendBillId)
}
_, err = client.DeleteFriendBill(context.TODO(), &monify.DeleteFriendBillRequest{FriendBillId: friend_billId[0]})
_, err = client.DeleteFriendBill(context.TODO(), &monify.DeleteFriendBillRequest{FriendBillId: res1.FriendBills[0].FriendBillId})
assert.NoError(t, err)
res2, err := client.ListFriendBill(context.TODO(), &monify.ListFriendBillRequest{RelationId: friendS_relationId[0]})
assert.Equal(t, len(res2.GetFriendBills()), 1)
assert.Equal(t, 500.0, res2.FriendBills[0].Amount)
assert.Equal(t, "test2", res2.FriendBills[0].Title)
assert.Equal(t, "test2", res2.FriendBills[0].Description)

// Test modify friend bill
_, err = client.ModifyFriendBill(context.TODO(), &monify.ModifyFriendBillRequest{
FriendBillId: res2.FriendBills[0].FriendBillId,
RelationId: friendS_relationId[0],
Amount: 1000,
Title: "test3",
Description: "test3",
})
assert.NoError(t, err)
friend_bills, err = client.ListFriendBill(context.TODO(), &monify.ListFriendBillRequest{RelationId: friendS_relationId[0]})
assert.Equal(t, len(friend_bills.GetFriendBills()), 1)
res3, err := client.ListFriendBill(context.TODO(), &monify.ListFriendBillRequest{RelationId: friendS_relationId[0]})
assert.Equal(t, 1000.0, res3.FriendBills[0].Amount)
assert.Equal(t, "test3", res3.FriendBills[0].Title)
assert.Equal(t, "test3", res3.FriendBills[0].Description)
}

0 comments on commit d0fd93c

Please sign in to comment.