From fcf1e61d4aee9991d102d81d397b0dc2c36e3a93 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 26 Jan 2025 17:48:35 +0800 Subject: [PATCH] :sparkles: Create wallet, get transaction by id --- pkg/internal/server/api/index.go | 2 ++ pkg/internal/server/api/transaction_api.go | 22 +++++++++++++++++ pkg/internal/server/api/wallet_api.go | 28 ++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index b00bfa5..6cf84df 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -9,12 +9,14 @@ func MapAPIs(app *fiber.App, baseURL string) { { wallet := api.Group("/wallet").Name("Wallet API") { + wallet.Post("/me", createWallet) wallet.Get("/me", getMyWallet) } transaction := api.Group("/transaction").Name("Transaction API") { transaction.Get("/me", getTransaction) + transaction.Get("/:id", getTransactionByID) } } } diff --git a/pkg/internal/server/api/transaction_api.go b/pkg/internal/server/api/transaction_api.go index 227375d..bcbcdd8 100644 --- a/pkg/internal/server/api/transaction_api.go +++ b/pkg/internal/server/api/transaction_api.go @@ -39,3 +39,25 @@ func getTransaction(c *fiber.Ctx) error { "data": transactions, }) } + +func getTransactionByID(c *fiber.Ctx) error { + id, _ := c.ParamsInt("id", 0) + + if err := sec.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(*sec.UserInfo) + + var transaction models.Transaction + if err := database.C.Where("id = ?", id). + Preload("Payer").Preload("Payee"). + First(&transaction).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + if transaction.Payer.AccountID != user.ID && transaction.Payee.AccountID != user.ID { + return fiber.NewError(fiber.StatusForbidden, "you are not related to this transaction") + } + + return c.JSON(transaction) +} diff --git a/pkg/internal/server/api/wallet_api.go b/pkg/internal/server/api/wallet_api.go index 4106d39..44521dc 100644 --- a/pkg/internal/server/api/wallet_api.go +++ b/pkg/internal/server/api/wallet_api.go @@ -1,12 +1,40 @@ package api import ( + "errors" + "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/wallet/pkg/internal/database" "git.solsynth.dev/hypernet/wallet/pkg/internal/models" "github.com/gofiber/fiber/v2" + "github.com/shopspring/decimal" + "gorm.io/gorm" ) +func createWallet(c *fiber.Ctx) error { + if err := sec.EnsureGrantedPerm(c, "CreateWallet", true); err != nil { + return err + } + user := c.Locals("user").(*sec.UserInfo) + + var wallet models.Wallet + if err := database.C.Where("account_id = ?", user.ID). + First(&wallet).Error; err == nil || errors.Is(err, gorm.ErrRecordNotFound) { + return fiber.NewError(fiber.StatusConflict, "wallet already exists") + } + + wallet = models.Wallet{ + Balance: decimal.NewFromInt(0), + AccountID: user.ID, + } + + if err := database.C.Create(&wallet).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return c.JSON(wallet) +} + func getMyWallet(c *fiber.Ctx) error { if err := sec.EnsureAuthenticated(c); err != nil { return err