-
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.
Merge pull request #9 from Monify-Dev/FriendBill
feature: modify friend bill
- Loading branch information
Showing
7 changed files
with
216 additions
and
91 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
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
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,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 | ||
} |
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