✨ Payment related grpc method
This commit is contained in:
92
pkg/internal/grpc/payment.go
Normal file
92
pkg/internal/grpc/payment.go
Normal file
@ -0,0 +1,92 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/models"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/proto"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func (v *Server) GetWallet(ctx context.Context, request *proto.GetWalletRequest) (*proto.GetWalletResponse, error) {
|
||||
var wallet models.Wallet
|
||||
if err := database.C.Where("account_id = ?", request.AccountId).First(&wallet).Error; err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, "wallet not found")
|
||||
}
|
||||
|
||||
return &proto.GetWalletResponse{
|
||||
Wallet: wallet.ToWalletInfo(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *Server) GetTransaction(ctx context.Context, request *proto.GetTransactionRequest) (*proto.GetTransactionResponse, error) {
|
||||
var transaction models.Transaction
|
||||
if err := database.C.Where("id = ?", request.Id).First(&transaction).Error; err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, "transaction not found")
|
||||
}
|
||||
|
||||
return &proto.GetTransactionResponse{
|
||||
Transaction: transaction.ToTransactionInfo(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *Server) MakeTransaction(ctx context.Context, request *proto.MakeTransactionRequest) (*proto.TransactionInfo, error) {
|
||||
if request.PayerId == nil && request.PayeeId == nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "payer and payee cannot be both nil")
|
||||
}
|
||||
|
||||
transaction := models.Transaction{
|
||||
Amount: decimal.NewFromFloat(request.Amount),
|
||||
Remark: request.Remark,
|
||||
}
|
||||
if request.PayerId != nil {
|
||||
transaction.PayerID = lo.ToPtr(uint(*request.PayerId))
|
||||
}
|
||||
if request.PayeeId != nil {
|
||||
transaction.PayeeID = lo.ToPtr(uint(*request.PayeeId))
|
||||
}
|
||||
|
||||
if err := database.C.Create(&transaction).Error; err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
return transaction.ToTransactionInfo(), nil
|
||||
}
|
||||
|
||||
func (v *Server) MakeTransactionWithAccount(ctx context.Context, request *proto.MakeTransactionWithAccountRequest) (*proto.TransactionInfo, error) {
|
||||
if request.PayerAccountId == nil && request.PayeeAccountId == nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "payer and payee cannot be both nil")
|
||||
}
|
||||
|
||||
transaction := models.Transaction{
|
||||
Amount: decimal.NewFromFloat(request.Amount),
|
||||
Remark: request.Remark,
|
||||
}
|
||||
if request.PayerAccountId != nil {
|
||||
val := uint(*request.PayerAccountId)
|
||||
var wallet models.Wallet
|
||||
if err := database.C.Where("account_id = ?", val).First(&wallet).Error; err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, "payer wallet not found")
|
||||
}
|
||||
transaction.Payer = &wallet
|
||||
transaction.PayerID = &wallet.ID
|
||||
}
|
||||
if request.PayeeAccountId != nil {
|
||||
val := uint(*request.PayeeAccountId)
|
||||
var wallet models.Wallet
|
||||
if err := database.C.Where("account_id = ?", val).First(&wallet).Error; err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, "payee wallet not found")
|
||||
}
|
||||
transaction.Payee = &wallet
|
||||
transaction.PayeeID = &wallet.ID
|
||||
}
|
||||
|
||||
if err := database.C.Create(&transaction).Error; err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
return transaction.ToTransactionInfo(), nil
|
||||
}
|
@ -2,6 +2,8 @@ package models
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/proto"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
@ -15,3 +17,19 @@ type Transaction struct {
|
||||
PayerID *uint `json:"payer_id"` // Leave this field as nil means pay from the system
|
||||
PayeeID *uint `json:"payee_id"` // Leave this field as nil means pay to the system
|
||||
}
|
||||
|
||||
func (v *Transaction) ToTransactionInfo() *proto.TransactionInfo {
|
||||
amount, _ := v.Amount.Float64()
|
||||
info := &proto.TransactionInfo{
|
||||
Id: uint64(v.ID),
|
||||
Amount: amount,
|
||||
Remark: v.Remark,
|
||||
}
|
||||
if v.PayerID != nil {
|
||||
info.PayerId = lo.ToPtr(uint64(*v.PayerID))
|
||||
}
|
||||
if v.PayeeID != nil {
|
||||
info.PayeeId = lo.ToPtr(uint64(*v.PayeeID))
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/proto"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
@ -10,5 +11,15 @@ type Wallet struct {
|
||||
|
||||
Transactions []Transaction `json:"transactions"`
|
||||
Balance decimal.Decimal `json:"amount" sql:"type:decimal(30,2);"`
|
||||
Password string `json:"password"`
|
||||
AccountID uint `json:"account_id"`
|
||||
}
|
||||
|
||||
func (v *Wallet) ToWalletInfo() *proto.WalletInfo {
|
||||
balance, _ := v.Balance.Float64()
|
||||
return &proto.WalletInfo{
|
||||
Id: uint64(v.ID),
|
||||
Balance: balance,
|
||||
AccountId: uint64(v.AccountID),
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/server/exts"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/wallet/pkg/internal/database"
|
||||
@ -17,6 +18,14 @@ func createWallet(c *fiber.Ctx) error {
|
||||
}
|
||||
user := c.Locals("user").(*sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Password string `json:"password" validate:"min=4"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var wallet models.Wallet
|
||||
if err := database.C.Where("account_id = ?", user.ID).
|
||||
First(&wallet).Error; err == nil || errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
Reference in New Issue
Block a user