mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fixed multipart form issue
This commit is contained in:
@@ -20,13 +20,14 @@ func NewAuthController(rs *services.UsersService, s *gin.RouterGroup) *AuthContr
|
||||
s.POST("login", rc.PostLogin)
|
||||
s.POST("register", rc.PostRegister)
|
||||
s.DELETE("deactivate", middleware.Auth, rc.Delete)
|
||||
s.GET("check-token", rc.CheckToken)
|
||||
|
||||
return rc
|
||||
}
|
||||
|
||||
func (rc *AuthController) PostLogin(c *gin.Context) {
|
||||
body := new(models.Login)
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
if err := c.ShouldBind(&body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -43,7 +44,7 @@ func (rc *AuthController) PostRegister(c *gin.Context) {
|
||||
body := new(models.User)
|
||||
body.Init()
|
||||
body.IsActive = true
|
||||
if err := c.ShouldBindJSON(body); err != nil {
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -68,3 +69,19 @@ func (rc *AuthController) Delete(c *gin.Context) {
|
||||
c.JSON(200, mr)
|
||||
}
|
||||
}
|
||||
|
||||
func (rc *AuthController) CheckToken(c *gin.Context) {
|
||||
token, _ := c.GetQuery("token")
|
||||
re := new(models.CheckToken)
|
||||
|
||||
valid, err := middleware.CheckToken(token)
|
||||
|
||||
if err != nil && valid.Valid {
|
||||
re.Valid = false
|
||||
c.AbortWithStatusJSON(400, re)
|
||||
}
|
||||
|
||||
re.Valid = true
|
||||
|
||||
c.JSON(200, re)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func NewTransactionTypeController(as *services.TransactionTypeService, s *gin.Ro
|
||||
|
||||
func (wc *TransactionTypeController) New(c *gin.Context) {
|
||||
body := new(models.NewTransactionTypeBody)
|
||||
if err := c.ShouldBindJSON(body); err != nil {
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou
|
||||
|
||||
func (wc *TransactionController) New(c *gin.Context) {
|
||||
body := new(models.NewTransactionBody)
|
||||
if err := c.ShouldBindJSON(body); err != nil {
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle
|
||||
func (wc *WalletsController) New(c *gin.Context) {
|
||||
body := new(models.NewWalletBody)
|
||||
|
||||
if err := c.ShouldBindJSON(body); err != nil {
|
||||
if err := c.ShouldBind(body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"wallet-api/pkg/models"
|
||||
@@ -14,20 +15,7 @@ import (
|
||||
func Auth(c *gin.Context) {
|
||||
exceptionReturn := new(models.Exception)
|
||||
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)
|
||||
if !ok {
|
||||
exceptionReturn.ErrorCode = "401001"
|
||||
exceptionReturn.StatusCode = 401
|
||||
exceptionReturn.Message = "Invalid token"
|
||||
c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||
}
|
||||
return []byte(secret), nil
|
||||
})
|
||||
token, err := CheckToken(tokenString)
|
||||
if err != nil {
|
||||
exceptionReturn.ErrorCode = "401001"
|
||||
exceptionReturn.StatusCode = 401
|
||||
@@ -57,3 +45,19 @@ func ExtractToken(c *gin.Context) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func CheckToken(tokenString string) (*jwt.Token, error) {
|
||||
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)
|
||||
var err error
|
||||
if !ok {
|
||||
err = errors.New("Invalid token")
|
||||
}
|
||||
return []byte(secret), err
|
||||
})
|
||||
return token, err
|
||||
}
|
||||
|
||||
@@ -5,10 +5,14 @@ type Token struct {
|
||||
}
|
||||
|
||||
type Login struct {
|
||||
Email string
|
||||
Password string
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
Id string
|
||||
}
|
||||
|
||||
type CheckToken struct {
|
||||
Valid bool `json:"valid"`
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ package models
|
||||
type User struct {
|
||||
tableName struct{} `pg:"users,alias:users"`
|
||||
BaseModel
|
||||
IsActive bool `json:"isActive" pg:"is_active"`
|
||||
Username string `json:"username" pg:"username"`
|
||||
Password string `json:"password" pg:"password"`
|
||||
Email string `json:"email" pg:"email"`
|
||||
IsActive bool `json:"isActive" pg:"is_active" form:"isActive"`
|
||||
Username string `json:"username" pg:"username" form:"username"`
|
||||
Password string `json:"password" pg:"password" form:"password"`
|
||||
Email string `json:"email" pg:"email" form:"email"`
|
||||
}
|
||||
|
||||
type UserReturnInfo struct {
|
||||
|
||||
@@ -8,6 +8,6 @@ type TransactionType struct {
|
||||
}
|
||||
|
||||
type NewTransactionTypeBody struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name" form:"name"`
|
||||
Type string `json:"type" form:"type"`
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ type Transaction struct {
|
||||
}
|
||||
|
||||
type NewTransactionBody struct {
|
||||
WalletID string `json:"walletId"`
|
||||
TransactionTypeID string `json:"transactionTypeId"`
|
||||
TransactionDate time.Time `json:"transactionDate"`
|
||||
Description string `json:"description"`
|
||||
WalletID string `json:"walletId" form:"walletId"`
|
||||
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||
TransactionDate time.Time `json:"transactionDate" form:"transactionDate"`
|
||||
Description string `json:"description" form:"description"`
|
||||
}
|
||||
|
||||
@@ -9,6 +9,6 @@ type Wallet struct {
|
||||
}
|
||||
|
||||
type NewWalletBody struct {
|
||||
Name string `json:"name"`
|
||||
UserID string `json:"userId"`
|
||||
Name string `json:"name" form:"name"`
|
||||
UserID string `json:"userId" form:"userId"`
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ func (as *TransactionService) New(body *models.NewTransactionBody) *models.Trans
|
||||
func (as *TransactionService) GetAll(walletId string, filtered *models.FilteredResponse) {
|
||||
wm := new([]models.Transaction)
|
||||
|
||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
query := as.Db.Model((wm))
|
||||
if walletId != "" {
|
||||
query = query.Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||
}
|
||||
FilteredResponse(query, wm, filtered)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user