mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
added wallet controller
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/controllers"
|
||||
"wallet-api/pkg/middleware"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/configs"
|
||||
|
||||
@@ -12,14 +13,17 @@ import (
|
||||
func Routes(s *gin.Engine, db *pg.DB) {
|
||||
ver := s.Group(configs.Prefix)
|
||||
|
||||
api := ver.Group("api")
|
||||
api := ver.Group("api", middleware.Auth)
|
||||
register := ver.Group("register")
|
||||
login := ver.Group("login")
|
||||
wallet := ver.Group("wallet", middleware.Auth)
|
||||
|
||||
apiService := services.ApiService{Db: db}
|
||||
usersService := services.UsersService{Db: db}
|
||||
walletService := services.WalletService{Db: db}
|
||||
|
||||
controllers.NewApiController(&apiService, api)
|
||||
controllers.NewRegisterController(&usersService, register)
|
||||
controllers.NewLoginController(&usersService, login)
|
||||
controllers.NewWalletsController(&walletService, wallet)
|
||||
}
|
||||
|
||||
44
pkg/controllers/wallets.go
Normal file
44
pkg/controllers/wallets.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type WalletsController struct {
|
||||
WalletService *services.WalletService
|
||||
}
|
||||
|
||||
func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *WalletsController {
|
||||
wc := new(WalletsController)
|
||||
wc.WalletService = as
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.Get)
|
||||
|
||||
return wc
|
||||
}
|
||||
|
||||
func (wc *WalletsController) New(c *gin.Context) {
|
||||
body := new(models.AuthModel)
|
||||
|
||||
get := c.MustGet("auth")
|
||||
body.Id = get.(*models.AuthModel).Id
|
||||
|
||||
wm := wc.WalletService.New(body)
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
|
||||
func (wc *WalletsController) Get(c *gin.Context) {
|
||||
body := new(models.AuthModel)
|
||||
|
||||
embed, _ := c.GetQuery("embed")
|
||||
auth := c.MustGet("auth")
|
||||
body.Id = auth.(*models.AuthModel).Id
|
||||
|
||||
wm := wc.WalletService.Get(body, embed)
|
||||
|
||||
c.JSON(200, wm)
|
||||
}
|
||||
@@ -9,7 +9,10 @@ import (
|
||||
func Start(conn *pg.DB) {
|
||||
apiMigration := migrations.ApiMigration{Db: conn}
|
||||
usersMigration := migrations.UsersMigration{Db: conn}
|
||||
walletsMigration := migrations.WalletsMigration{Db: conn}
|
||||
|
||||
apiMigration.Create()
|
||||
usersMigration.Create()
|
||||
walletsMigration.Create()
|
||||
walletsMigration.PopulateTypes()
|
||||
}
|
||||
|
||||
40
pkg/migrate/migrations/wallets.go
Normal file
40
pkg/migrate/migrations/wallets.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type WalletsMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *WalletsMigration) Create() {
|
||||
models := []interface{}{
|
||||
(*models.WalletTypeModel)(nil),
|
||||
(*models.WalletModel)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: false,
|
||||
FKConstraints: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (am *WalletsMigration) PopulateTypes() {
|
||||
walletTypeModel := new(models.WalletTypeModel)
|
||||
walletTypeModel.Init()
|
||||
walletTypeModel.Name = "Test"
|
||||
am.Db.Model(walletTypeModel).Insert()
|
||||
}
|
||||
16
pkg/models/wallets.go
Normal file
16
pkg/models/wallets.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
type WalletModel struct {
|
||||
tableName struct{} `pg:"wallets,alias:wallets"`
|
||||
CommonModel
|
||||
WalletTypeID string `json:"walletTypeId" pg:"wallet_type_id"`
|
||||
WalletType *WalletTypeModel `json:"walletType" pg:"rel:has-one,fk:wallet_type_id"`
|
||||
UserID string `json:"userId" pg:"user_id"`
|
||||
User *UserReturnInfoModel `json:"user" pg:"rel:has-one,fk:user_id"`
|
||||
}
|
||||
|
||||
type WalletTypeModel struct {
|
||||
tableName struct{} `pg:"walletTypes,alias:walletTypes"`
|
||||
CommonModel
|
||||
Name string `json:"name"`
|
||||
}
|
||||
40
pkg/services/wallets.go
Normal file
40
pkg/services/wallets.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type WalletService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (as *WalletService) New(am *models.AuthModel) *models.WalletModel {
|
||||
walletType := as.GetType()
|
||||
|
||||
walletModel := new(models.WalletModel)
|
||||
walletModel.Init()
|
||||
walletModel.UserID = am.Id
|
||||
walletModel.WalletTypeID = walletType.Id
|
||||
as.Db.Model(walletModel).Insert()
|
||||
return walletModel
|
||||
}
|
||||
|
||||
func (as *WalletService) Get(am *models.AuthModel, embed string) *models.WalletModel {
|
||||
wm := new(models.WalletModel)
|
||||
|
||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||
common.GenerateEmbed(query, embed).Select()
|
||||
|
||||
return wm
|
||||
}
|
||||
|
||||
func (as *WalletService) GetType() *models.WalletTypeModel {
|
||||
wt := new(models.WalletTypeModel)
|
||||
|
||||
as.Db.Model(wt).Select()
|
||||
|
||||
return wt
|
||||
}
|
||||
Reference in New Issue
Block a user