Skip to content

Commit

Permalink
Merge pull request #9 from Monify-Dev/FriendBill
Browse files Browse the repository at this point in the history
feature: modify friend bill
  • Loading branch information
Danielllllllllllllll authored Aug 5, 2024
2 parents 96a1ca6 + 91d08d6 commit 7f9518e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 91 deletions.
3 changes: 2 additions & 1 deletion migrations/2405310815_create_friend_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ CREATE TABLE friend_bill(
friend_bill_id uuid PRIMARY KEY,
relation_id uuid NOT NULL REFERENCES friend(relation_id),
amount double precision NOT NULL,
title varchar(50) NOT NULL ,
title varchar(50) NOT NULL,
description varchar(100) NOT NULL default '',
in_debt varchar(30) NOT NULL,
created_at timestamp NOT NULL default CURRENT_TIMESTAMP
)

10 changes: 7 additions & 3 deletions protobuf/friend_bill.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ message CreateFriendBillRequest {
double amount = 2;
string title = 3;
string description = 4;
string in_debt = 5;
}

message CreateFriendBillResponse {
Expand All @@ -49,9 +50,11 @@ 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;
string in_debt = 6;
}

message ListFriendBillRequest {
Expand All @@ -68,4 +71,5 @@ message FriendBill {
double amount = 2;
string title = 3;
string description = 4;
string in_debt = 5;
}
171 changes: 105 additions & 66 deletions protobuf/gen/go/friend_bill.pb.go

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions services/api/controllers/friend_bill/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@ 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")
}
if req.InDebt == "" {
return nil, status.Error(codes.InvalidArgument, "InDebt person is required")
}
_, 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)
_, err := db.ExecContext(ctx, `
INSERT INTO friend_bill (friend_bill_id, relation_id, amount, title, description, in_debt)
VALUES ($1, $2, $3, $4, $5, $6)
`, friend_billId, req.RelationId, req.Amount, req.Title, req.Description, req.InDebt)
if err != nil {
logger.Error("Insert values into friend_bill error.", zap.Error(err))
return nil, status.Error(codes.Internal, "")
Expand Down
4 changes: 2 additions & 2 deletions services/api/controllers/friend_bill/list_friend_bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s Service) ListFriendBill(ctx context.Context, req *monify.ListFriendBillR
}
db := ctx.Value(lib.DatabaseContextKey{}).(*sql.DB)
query, err := db.QueryContext(ctx, `
SELECT friend_bill_id, amount, title, description
SELECT friend_bill_id, amount, title, description, in_debt
FROM friend_bill
WHERE relation_id = $1`, req.RelationId)
if err != nil {
Expand All @@ -35,7 +35,7 @@ func (s Service) ListFriendBill(ctx context.Context, req *monify.ListFriendBillR
break
}
var friend_bill monify.FriendBill
if err = query.Scan(&friend_bill.FriendBillId, &friend_bill.Amount, &friend_bill.Title, &friend_bill.Description); err != nil {
if err = query.Scan(&friend_bill.FriendBillId, &friend_bill.Amount, &friend_bill.Title, &friend_bill.Description, &friend_bill.InDebt); err != nil {
logger.Error("Scan friend bill information 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, in_debt)
VALUES ($1, $2, $3, $4, $5, $6)
`, req.FriendBillId, req.RelationId, req.Amount, req.Title, req.Description, req.InDebt)
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
}
45 changes: 36 additions & 9 deletions services/api/test/friend_bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,61 @@ func TestFriendBill(t *testing.T) {
Amount: 300,
Title: "test1",
Description: "test1",
InDebt: "friend_bill_nickId1",
})
_, err = client.CreateFriendBill(context.TODO(), &monify.CreateFriendBillRequest{
RelationId: friendS_relationId[0],
Amount: 500,
Title: "test2",
Description: "test2",
InDebt: "friend_bill_nickId1",
})
assert.NoError(t, err)
_, err = client.CreateFriendBill(context.TODO(), &monify.CreateFriendBillRequest{
RelationId: friendS_relationId[0],
Amount: 0,
Title: "test2",
Description: "test2",
InDebt: "friend_bill_nickId1",
})
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, "friend_bill_nickId1", res1.FriendBills[0].InDebt)
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)
assert.Equal(t, "friend_bill_nickId1", res1.FriendBills[1].InDebt)

// 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)
assert.Equal(t, "friend_bill_nickId1", res2.FriendBills[0].InDebt)

// 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",
InDebt: "friend_bill_nickId2",
})
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)
assert.Equal(t, "friend_bill_nickId2", res3.FriendBills[0].InDebt)
}

0 comments on commit 7f9518e

Please sign in to comment.