Skip to content

Commit

Permalink
fix: add "in_debt" column in the "friend_bill"
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielllllllllllllll committed Aug 5, 2024
1 parent d0fd93c commit 91d08d6
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 74 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
)

3 changes: 3 additions & 0 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 @@ -53,6 +54,7 @@ message ModifyFriendBillRequest {
double amount = 3;
string title = 4;
string description = 5;
string in_debt = 6;
}

message ListFriendBillRequest {
Expand All @@ -69,4 +71,5 @@ message FriendBill {
double amount = 2;
string title = 3;
string description = 4;
string in_debt = 5;
}
159 changes: 94 additions & 65 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.

9 changes: 6 additions & 3 deletions services/api/controllers/friend_bill/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func (s Service) CreateFriendBill(ctx context.Context, req *monify.CreateFriendB
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")
Expand All @@ -29,9 +32,9 @@ func (s Service) CreateFriendBill(ctx context.Context, req *monify.CreateFriendB
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, req.RelationId, req.Amount, req.Title, req.Description)
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
Loading

0 comments on commit 91d08d6

Please sign in to comment.