mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
Merge branch 'feature/#2-wallet-system'
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"wallet-api/pkg/controllers"
|
"wallet-api/pkg/controllers"
|
||||||
|
"wallet-api/pkg/middleware"
|
||||||
"wallet-api/pkg/services"
|
"wallet-api/pkg/services"
|
||||||
"wallet-api/pkg/utl/configs"
|
"wallet-api/pkg/utl/configs"
|
||||||
|
|
||||||
@@ -12,14 +13,17 @@ import (
|
|||||||
func Routes(s *gin.Engine, db *pg.DB) {
|
func Routes(s *gin.Engine, db *pg.DB) {
|
||||||
ver := s.Group(configs.Prefix)
|
ver := s.Group(configs.Prefix)
|
||||||
|
|
||||||
api := ver.Group("api")
|
api := ver.Group("api", middleware.Auth)
|
||||||
register := ver.Group("register")
|
register := ver.Group("register")
|
||||||
login := ver.Group("login")
|
login := ver.Group("login")
|
||||||
|
wallet := ver.Group("wallet", middleware.Auth)
|
||||||
|
|
||||||
apiService := services.ApiService{Db: db}
|
apiService := services.ApiService{Db: db}
|
||||||
usersService := services.UsersService{Db: db}
|
usersService := services.UsersService{Db: db}
|
||||||
|
walletService := services.WalletService{Db: db}
|
||||||
|
|
||||||
controllers.NewApiController(&apiService, api)
|
controllers.NewApiController(&apiService, api)
|
||||||
controllers.NewRegisterController(&usersService, register)
|
controllers.NewRegisterController(&usersService, register)
|
||||||
controllers.NewLoginController(&usersService, login)
|
controllers.NewLoginController(&usersService, login)
|
||||||
|
controllers.NewWalletsController(&walletService, wallet)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,15 +22,15 @@ func NewLoginController(rs *services.UsersService, s *gin.RouterGroup) *LoginCon
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *LoginController) Post(c *gin.Context) {
|
func (rc *LoginController) Post(c *gin.Context) {
|
||||||
loginBody := new(models.LoginModel)
|
body := new(models.LoginModel)
|
||||||
if err := c.ShouldBindJSON(&loginBody); err != nil {
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
returnedUser, returnException := rc.UsersService.Login(loginBody)
|
returnedUser, exceptionReturn := rc.UsersService.Login(body)
|
||||||
|
|
||||||
if returnException.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
c.JSON(returnException.StatusCode, returnException)
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, returnedUser)
|
c.JSON(200, returnedUser)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/services"
|
"wallet-api/pkg/services"
|
||||||
"wallet-api/pkg/utl/common"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -23,23 +22,18 @@ func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *Regis
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *RegisterController) Post(c *gin.Context) {
|
func (rc *RegisterController) Post(c *gin.Context) {
|
||||||
registerBody := createUserModel()
|
body := new(models.UserModel)
|
||||||
if err := c.ShouldBindJSON(®isterBody); err != nil {
|
body.Init()
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
returnedUser, returnException := rc.UsersService.Create(®isterBody)
|
returnedUser, exceptionReturn := rc.UsersService.Create(body)
|
||||||
|
|
||||||
if returnException.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
c.JSON(returnException.StatusCode, returnException)
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, returnedUser.Payload())
|
c.JSON(200, returnedUser.Payload())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUserModel() models.UserModel {
|
|
||||||
commonModel := common.CreateDbModel()
|
|
||||||
userModel := models.UserModel{CommonModel: commonModel}
|
|
||||||
return userModel
|
|
||||||
}
|
|
||||||
|
|||||||
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)
|
||||||
|
}
|
||||||
@@ -1,7 +1,58 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/utl/configs"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
func Auth(c *gin.Context) {
|
func Auth(c *gin.Context) {
|
||||||
|
exceptionReturn := new(models.ExceptionModel)
|
||||||
|
tokenString := ExtractToken(c)
|
||||||
|
secret := os.Getenv("ACCESS_SECRET")
|
||||||
|
if secret == "" {
|
||||||
|
secret = configs.Secret
|
||||||
|
}
|
||||||
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
||||||
|
println(ok)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||||
|
}
|
||||||
|
return []byte(secret), nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
exceptionReturn.ErrorCode = "401001"
|
||||||
|
exceptionReturn.StatusCode = 401
|
||||||
|
exceptionReturn.Message = "Invalid token"
|
||||||
|
c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
|
}
|
||||||
|
claims, ok := token.Claims.(jwt.MapClaims)
|
||||||
|
if ok && token.Valid {
|
||||||
|
userId, _ := claims["id"].(string)
|
||||||
|
|
||||||
|
authModel := new(models.AuthModel)
|
||||||
|
authModel.Id = userId
|
||||||
|
|
||||||
|
c.Set("auth", authModel)
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExtractToken(c *gin.Context) string {
|
||||||
|
bearerToken := c.GetHeader("Authorization")
|
||||||
|
tokenArr := strings.Split(bearerToken, " ")
|
||||||
|
if len(tokenArr) == 2 {
|
||||||
|
bearerCheck := strings.ToLower(tokenArr[0])
|
||||||
|
if bearerCheck == "bearer" {
|
||||||
|
return tokenArr[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,10 @@ import (
|
|||||||
func Start(conn *pg.DB) {
|
func Start(conn *pg.DB) {
|
||||||
apiMigration := migrations.ApiMigration{Db: conn}
|
apiMigration := migrations.ApiMigration{Db: conn}
|
||||||
usersMigration := migrations.UsersMigration{Db: conn}
|
usersMigration := migrations.UsersMigration{Db: conn}
|
||||||
|
walletsMigration := migrations.WalletsMigration{Db: conn}
|
||||||
|
|
||||||
apiMigration.Create()
|
apiMigration.Create()
|
||||||
usersMigration.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()
|
||||||
|
}
|
||||||
@@ -8,3 +8,7 @@ type LoginModel struct {
|
|||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AuthModel struct {
|
||||||
|
Id string
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,20 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type CommonModel struct {
|
type CommonModel struct {
|
||||||
Id string `json:"id" pg:"id"`
|
Id string `json:"id" pg:"id,pk"`
|
||||||
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
|
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
|
||||||
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
|
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cm *CommonModel) Init() {
|
||||||
|
date := time.Now()
|
||||||
|
cm.Id = uuid.NewString()
|
||||||
|
cm.DateCreated = date
|
||||||
|
cm.DateUpdated = date
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package models
|
|||||||
type UserModel struct {
|
type UserModel struct {
|
||||||
tableName struct{} `pg:"users,alias:users"`
|
tableName struct{} `pg:"users,alias:users"`
|
||||||
CommonModel
|
CommonModel
|
||||||
Username string `json:"username"`
|
Username string `json:"username" pg:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password" pg:"password"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email" pg:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserReturnInfoModel struct {
|
type UserReturnInfoModel struct {
|
||||||
@@ -15,11 +15,11 @@ type UserReturnInfoModel struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (um *UserModel) Payload() UserReturnInfoModel {
|
func (um *UserModel) Payload() *UserReturnInfoModel {
|
||||||
payload := UserReturnInfoModel{
|
payload := new(UserReturnInfoModel)
|
||||||
CommonModel: um.CommonModel,
|
payload.CommonModel = um.CommonModel
|
||||||
Username: um.Username,
|
payload.Username = um.Username
|
||||||
Email: um.Email,
|
payload.Email = um.Email
|
||||||
}
|
|
||||||
return payload
|
return payload
|
||||||
}
|
}
|
||||||
|
|||||||
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"`
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/utl/common"
|
"wallet-api/pkg/utl/common"
|
||||||
|
"wallet-api/pkg/utl/configs"
|
||||||
|
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@@ -16,16 +17,16 @@ type UsersService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, models.ExceptionModel) {
|
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, *models.ExceptionModel) {
|
||||||
var checkModel models.UserModel
|
check := new(models.UserModel)
|
||||||
var exceptionReturn models.ExceptionModel
|
exceptionReturn := new(models.ExceptionModel)
|
||||||
|
|
||||||
us.Db.Model(&checkModel).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||||
if checkModel.Username != "" || checkModel.Email != "" {
|
if check.Username != "" || check.Email != "" {
|
||||||
exceptionReturn.Message = "User already exists"
|
exceptionReturn.Message = "User already exists"
|
||||||
exceptionReturn.ErrorCode = "400101"
|
exceptionReturn.ErrorCode = "400101"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
return &checkModel, exceptionReturn
|
return check, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||||
@@ -43,27 +44,27 @@ func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserMode
|
|||||||
return registerBody, exceptionReturn
|
return registerBody, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Login(loginBody *models.LoginModel) (models.TokenModel, models.ExceptionModel) {
|
func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel, *models.ExceptionModel) {
|
||||||
var checkModel models.UserModel
|
check := new(models.UserModel)
|
||||||
var exceptionReturn models.ExceptionModel
|
exceptionReturn := new(models.ExceptionModel)
|
||||||
var tokenPayload models.TokenModel
|
tokenPayload := new(models.TokenModel)
|
||||||
|
|
||||||
us.Db.Model(&checkModel).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||||
if checkModel.Email == "" {
|
if check.Email == "" {
|
||||||
exceptionReturn.Message = "Email not found"
|
exceptionReturn.Message = "Email not found"
|
||||||
exceptionReturn.ErrorCode = "400103"
|
exceptionReturn.ErrorCode = "400103"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
if bcrypt.CompareHashAndPassword([]byte(checkModel.Password), []byte(loginBody.Password)) != nil {
|
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
||||||
exceptionReturn.Message = "Incorrect password"
|
exceptionReturn.Message = "Incorrect password"
|
||||||
exceptionReturn.ErrorCode = "400104"
|
exceptionReturn.ErrorCode = "400104"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := CreateToken(checkModel)
|
token, err := CreateToken(check)
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|
||||||
tokenPayload.Token = token
|
tokenPayload.Token = token
|
||||||
@@ -71,15 +72,15 @@ func (us *UsersService) Login(loginBody *models.LoginModel) (models.TokenModel,
|
|||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateToken(user models.UserModel) (string, error) {
|
func CreateToken(user *models.UserModel) (string, error) {
|
||||||
atClaims := jwt.MapClaims{}
|
atClaims := jwt.MapClaims{}
|
||||||
atClaims["authorized"] = true
|
atClaims["authorized"] = true
|
||||||
atClaims["id"] = user.Id
|
atClaims["id"] = user.Id
|
||||||
atClaims["exp"] = time.Now().Add(time.Minute * 15).Unix()
|
atClaims["exp"] = time.Now().Add(time.Minute).Unix()
|
||||||
|
|
||||||
secret := os.Getenv("ACCESS_SECRET")
|
secret := os.Getenv("ACCESS_SECRET")
|
||||||
if secret == "" {
|
if secret == "" {
|
||||||
secret = "Dond3sta"
|
secret = configs.Secret
|
||||||
}
|
}
|
||||||
|
|
||||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||||
|
|||||||
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
|
||||||
|
}
|
||||||
@@ -2,10 +2,6 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CheckError(err error) {
|
func CheckError(err error) {
|
||||||
@@ -13,13 +9,3 @@ func CheckError(err error) {
|
|||||||
log.Fatalf("Error occured. %v", err)
|
log.Fatalf("Error occured. %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDbModel() models.CommonModel {
|
|
||||||
date := time.Now()
|
|
||||||
dbModel := models.CommonModel{
|
|
||||||
Id: uuid.NewString(),
|
|
||||||
DateCreated: date,
|
|
||||||
DateUpdated: date,
|
|
||||||
}
|
|
||||||
return dbModel
|
|
||||||
}
|
|
||||||
|
|||||||
17
pkg/utl/common/embeds.go
Normal file
17
pkg/utl/common/embeds.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateEmbed(qr *orm.Query, embed string) *orm.Query{
|
||||||
|
if embed != "" {
|
||||||
|
rels := strings.Split(embed, ",")
|
||||||
|
for _, rel := range rels {
|
||||||
|
qr = qr.Relation(rel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return qr
|
||||||
|
}
|
||||||
@@ -3,4 +3,5 @@ package configs
|
|||||||
const (
|
const (
|
||||||
Version = "0.0.1"
|
Version = "0.0.1"
|
||||||
Prefix = "v1"
|
Prefix = "v1"
|
||||||
|
Secret = "Donde4sta"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user