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:
@@ -6,10 +6,13 @@ tasks:
|
|||||||
- go build -o "./bin/api.exe" "./cmd/api/main.go"
|
- go build -o "./bin/api.exe" "./cmd/api/main.go"
|
||||||
run:
|
run:
|
||||||
cmds:
|
cmds:
|
||||||
- go run cmd/api/main.go
|
- ./bin/api
|
||||||
migrate:
|
migrate:
|
||||||
cmds:
|
cmds:
|
||||||
- go run cmd/migrate/main.go
|
- go run cmd/migrate/main.go
|
||||||
build-run:
|
build-run:
|
||||||
cmds:
|
cmds:
|
||||||
- go build -o "./bin/api.exe" "./cmd/api/main.go" && ./bin/api.exe
|
- go build -o "./bin/api.exe" "./cmd/api/main.go" && ./bin/api.exe
|
||||||
|
start:
|
||||||
|
cmds:
|
||||||
|
- go run cmd/api/main.go
|
||||||
@@ -20,13 +20,14 @@ func NewAuthController(rs *services.UsersService, s *gin.RouterGroup) *AuthContr
|
|||||||
s.POST("login", rc.PostLogin)
|
s.POST("login", rc.PostLogin)
|
||||||
s.POST("register", rc.PostRegister)
|
s.POST("register", rc.PostRegister)
|
||||||
s.DELETE("deactivate", middleware.Auth, rc.Delete)
|
s.DELETE("deactivate", middleware.Auth, rc.Delete)
|
||||||
|
s.GET("check-token", rc.CheckToken)
|
||||||
|
|
||||||
return rc
|
return rc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *AuthController) PostLogin(c *gin.Context) {
|
func (rc *AuthController) PostLogin(c *gin.Context) {
|
||||||
body := new(models.Login)
|
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()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -43,7 +44,7 @@ func (rc *AuthController) PostRegister(c *gin.Context) {
|
|||||||
body := new(models.User)
|
body := new(models.User)
|
||||||
body.Init()
|
body.Init()
|
||||||
body.IsActive = true
|
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()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -68,3 +69,19 @@ func (rc *AuthController) Delete(c *gin.Context) {
|
|||||||
c.JSON(200, mr)
|
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) {
|
func (wc *TransactionTypeController) New(c *gin.Context) {
|
||||||
body := new(models.NewTransactionTypeBody)
|
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()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou
|
|||||||
|
|
||||||
func (wc *TransactionController) New(c *gin.Context) {
|
func (wc *TransactionController) New(c *gin.Context) {
|
||||||
body := new(models.NewTransactionBody)
|
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()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle
|
|||||||
func (wc *WalletsController) New(c *gin.Context) {
|
func (wc *WalletsController) New(c *gin.Context) {
|
||||||
body := new(models.NewWalletBody)
|
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()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
@@ -14,20 +15,7 @@ import (
|
|||||||
func Auth(c *gin.Context) {
|
func Auth(c *gin.Context) {
|
||||||
exceptionReturn := new(models.Exception)
|
exceptionReturn := new(models.Exception)
|
||||||
tokenString := ExtractToken(c)
|
tokenString := ExtractToken(c)
|
||||||
secret := os.Getenv("ACCESS_SECRET")
|
token, err := CheckToken(tokenString)
|
||||||
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
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exceptionReturn.ErrorCode = "401001"
|
exceptionReturn.ErrorCode = "401001"
|
||||||
exceptionReturn.StatusCode = 401
|
exceptionReturn.StatusCode = 401
|
||||||
@@ -57,3 +45,19 @@ func ExtractToken(c *gin.Context) string {
|
|||||||
}
|
}
|
||||||
return ""
|
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 {
|
type Login struct {
|
||||||
Email string
|
Email string `form:"email"`
|
||||||
Password string
|
Password string `form:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Auth struct {
|
type Auth struct {
|
||||||
Id string
|
Id string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CheckToken struct {
|
||||||
|
Valid bool `json:"valid"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package models
|
|||||||
type User struct {
|
type User struct {
|
||||||
tableName struct{} `pg:"users,alias:users"`
|
tableName struct{} `pg:"users,alias:users"`
|
||||||
BaseModel
|
BaseModel
|
||||||
IsActive bool `json:"isActive" pg:"is_active"`
|
IsActive bool `json:"isActive" pg:"is_active" form:"isActive"`
|
||||||
Username string `json:"username" pg:"username"`
|
Username string `json:"username" pg:"username" form:"username"`
|
||||||
Password string `json:"password" pg:"password"`
|
Password string `json:"password" pg:"password" form:"password"`
|
||||||
Email string `json:"email" pg:"email"`
|
Email string `json:"email" pg:"email" form:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserReturnInfo struct {
|
type UserReturnInfo struct {
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ type TransactionType struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NewTransactionTypeBody struct {
|
type NewTransactionTypeBody struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name" form:"name"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type" form:"type"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ type Transaction struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NewTransactionBody struct {
|
type NewTransactionBody struct {
|
||||||
WalletID string `json:"walletId"`
|
WalletID string `json:"walletId" form:"walletId"`
|
||||||
TransactionTypeID string `json:"transactionTypeId"`
|
TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"`
|
||||||
TransactionDate time.Time `json:"transactionDate"`
|
TransactionDate time.Time `json:"transactionDate" form:"transactionDate"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description" form:"description"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ type Wallet struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NewWalletBody struct {
|
type NewWalletBody struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name" form:"name"`
|
||||||
UserID string `json:"userId"`
|
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) {
|
func (as *TransactionService) GetAll(walletId string, filtered *models.FilteredResponse) {
|
||||||
wm := new([]models.Transaction)
|
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)
|
FilteredResponse(query, wm, filtered)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user