🎉 Initial Commit
Some checks failed
release-nightly / build-docker (push) Has been cancelled
Some checks failed
release-nightly / build-docker (push) Has been cancelled
This commit is contained in:
20
pkg/internal/server/api/index.go
Normal file
20
pkg/internal/server/api/index.go
Normal file
@ -0,0 +1,20 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func MapAPIs(app *fiber.App, baseURL string) {
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
wallet := api.Group("/wallet").Name("Wallet API")
|
||||
{
|
||||
wallet.Get("/me", getMyWallet)
|
||||
}
|
||||
|
||||
transaction := api.Group("/transaction").Name("Transaction API")
|
||||
{
|
||||
transaction.Get("/me", getTransaction)
|
||||
}
|
||||
}
|
||||
}
|
41
pkg/internal/server/api/transaction_api.go
Normal file
41
pkg/internal/server/api/transaction_api.go
Normal file
@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
func getTransaction(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); 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 {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := database.C.Model(&models.Transaction{}).Where("payer_id = ? OR payee_id = ?", user.ID, user.ID).
|
||||
Count(&count).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
var transactions []models.Transaction
|
||||
if err := database.C.Where("payer_id = ? OR payee_id = ?", user.ID, user.ID).
|
||||
Limit(take).Offset(offset).
|
||||
Find(&transactions).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": transactions,
|
||||
})
|
||||
}
|
22
pkg/internal/server/api/wallet_api.go
Normal file
22
pkg/internal/server/api/wallet_api.go
Normal file
@ -0,0 +1,22 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
func getMyWallet(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); 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 {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(wallet)
|
||||
}
|
Reference in New Issue
Block a user