diff --git a/internal/services/group_bill/create.go b/internal/services/group_bill/create.go index 9dd7f5d..cd5014d 100644 --- a/internal/services/group_bill/create.go +++ b/internal/services/group_bill/create.go @@ -53,7 +53,7 @@ func (s Service) CreateGroupBill(ctx context.Context, req *monify.CreateGroupBil defer tx.Rollback() //Insert billId := uuid.New() - if err = insertBill(ctx, tx, logger, insertBillInfo{ + if err = insertBill(ctx, tx, insertBillInfo{ billId: billId, groupId: groupId, createdBy: memberId, @@ -97,7 +97,8 @@ type insertBillInfo struct { prepaidPeople []*monify.InsertPrepaidPerson } -func insertBill(ctx context.Context, tx *sql.Tx, logger *zap.Logger, info insertBillInfo) error { +func insertBill(ctx context.Context, tx *sql.Tx, info insertBillInfo) error { + logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) _, err := tx.ExecContext(ctx, ` INSERT INTO group_bill (bill_id, group_id, created_by, total_money, title, description) VALUES ($1, $2, $3, $4, $5, $6) diff --git a/internal/services/group_bill/modify_bill.go b/internal/services/group_bill/modify_bill.go index 3c23a6c..dd16e15 100644 --- a/internal/services/group_bill/modify_bill.go +++ b/internal/services/group_bill/modify_bill.go @@ -58,7 +58,7 @@ func (s Service) ModifyGroupBill(ctx context.Context, req *monify.ModifyGroupBil } //Insert - if err = insertBill(ctx, tx, logger, insertBillInfo{ + if err = insertBill(ctx, tx, insertBillInfo{ billId: billId, groupId: groupId, createdBy: createdBy, diff --git a/internal/services/group_bill/service.go b/internal/services/group_bill/service.go index 1b84767..ba7ad07 100644 --- a/internal/services/group_bill/service.go +++ b/internal/services/group_bill/service.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "github.com/google/uuid" + "go.uber.org/zap" "monify/internal/middlewares" monify "monify/protobuf/gen/go" ) @@ -28,19 +29,25 @@ type billHistoryInsertion struct { } func insertBillHistory(ctx context.Context, db *sql.Tx, history billHistoryInsertion) error { + logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) row := db.QueryRowContext(ctx, ` SELECT group_id FROM group_member WHERE group_member_id = $1 `, history.operator) var groupId uuid.UUID if err := row.Scan(&groupId); err != nil { + logger.Error("failed to get group_id", zap.Error(err)) return err } - _, err := db.ExecContext(ctx, ` + if _, err := db.ExecContext(ctx, ` INSERT INTO group_bill_history( history_id, type, bill_id, title, operator, group_id) VALUES ($1, $2, $3, $4, $5, $6) - `, uuid.New(), history.ty, history.billId, history.title, history.operator, groupId) - return err + `, uuid.New(), history.ty, history.billId, history.title, history.operator, groupId); err != nil { + logger.Error("failed to insert bill history", zap.Error(err)) + return err + } + + return nil } // context requires middlewares.DatabaseContextKey:*sql.DB diff --git a/internal/test/group_bill_test.go b/internal/test/group_bill_test.go index cb0d34a..003487c 100644 --- a/internal/test/group_bill_test.go +++ b/internal/test/group_bill_test.go @@ -101,5 +101,4 @@ func TestCreateAndGetGroupBill(t *testing.T) { assert.NoError(t, err) response2, err = client.GetGroupBills(context.TODO(), &monify.GetGroupBillsRequest{GroupId: group.GroupId}) assert.Empty(t, response2.GroupBills) - } diff --git a/migrations/2405310815_create_friend_table.down.sql b/migrations/2405310815_create_friend_table.down.sql index e69de29..8601dee 100644 --- a/migrations/2405310815_create_friend_table.down.sql +++ b/migrations/2405310815_create_friend_table.down.sql @@ -0,0 +1,2 @@ +DROP TABLE friend_bill; +DROP TABLE friend; \ No newline at end of file diff --git a/migrations/2405310815_create_friend_table.up.sql b/migrations/2405310815_create_friend_table.up.sql index 2a3fdd0..baace5b 100644 --- a/migrations/2405310815_create_friend_table.up.sql +++ b/migrations/2405310815_create_friend_table.up.sql @@ -10,11 +10,10 @@ CREATE TABLE friend( CREATE TABLE friend_bill( friend_bill_id uuid PRIMARY KEY, - relation_id uuid NOT NULL , + relation_id uuid NOT NULL REFERENCES friend(relation_id), amount double precision NOT NULL, title varchar(50) NOT NULL , description varchar(100) NOT NULL default '', - created_at timestamp NOT NULL default CURRENT_TIMESTAMP, - FOREIGN KEY (relation_id) REFERENCES friend(relation_id) + created_at timestamp NOT NULL default CURRENT_TIMESTAMP ) diff --git a/migrations/2406010512_create_bill_history_table.up.sql b/migrations/2406010512_create_bill_history_table.up.sql index e2d84a7..ecdb05d 100644 --- a/migrations/2406010512_create_bill_history_table.up.sql +++ b/migrations/2406010512_create_bill_history_table.up.sql @@ -11,7 +11,7 @@ INSERT INTO group_bill_history_type (name, type) VALUES ('modify', 2); CREATE TABLE group_bill_history( history_id uuid PRIMARY KEY, type int REFERENCES group_bill_history_type(type), - bill_id uuid UNIQUE, + bill_id uuid, group_id uuid REFERENCES "group"(group_id), title varchar(30) NOT NULL, operator uuid REFERENCES group_member(group_member_id), diff --git a/migrations/2406030027_create_friend_invite_table.up.sql b/migrations/2406030027_create_friend_invite_table.up.sql index 311053d..9f1bf46 100644 --- a/migrations/2406030027_create_friend_invite_table.up.sql +++ b/migrations/2406030027_create_friend_invite_table.up.sql @@ -3,6 +3,6 @@ CREATE TABLE friend_invite ( sender uuid NOT NULL, receiver uuid NOT NULL, created_at timestamp default CURRENT_TIMESTAMP, - FOREIGN KEY (sender) REFERENCES "user_identity"(user_id) + FOREIGN KEY (sender) REFERENCES "user_identity"(user_id), FOREIGN KEY (receiver) REFERENCES "user_identity"(user_id) ); \ No newline at end of file