mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 14:18:12 +00:00
list all wallets for current user (WA-9)
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"wallet-api/pkg/api"
|
"wallet-api/pkg/api"
|
||||||
|
"wallet-api/pkg/middleware"
|
||||||
"wallet-api/pkg/utl/db"
|
"wallet-api/pkg/utl/db"
|
||||||
"wallet-api/pkg/utl/server"
|
"wallet-api/pkg/utl/server"
|
||||||
|
|
||||||
@@ -11,10 +13,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
fmt.Println("Start")
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
|
||||||
dbUrl := os.Getenv("DATABASE_URL")
|
dbUrl := os.Getenv("DATABASE_URL")
|
||||||
r := gin.Default()
|
fmt.Println("Database: ", dbUrl)
|
||||||
|
r := gin.New()
|
||||||
|
r.Use(middleware.CORSMiddleware())
|
||||||
|
|
||||||
conn := db.CreateConnection(dbUrl)
|
conn := db.CreateConnection(dbUrl)
|
||||||
api.Init(r, conn)
|
api.Init(r, conn)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"wallet-api/pkg/migrate"
|
"wallet-api/pkg/migrate"
|
||||||
"wallet-api/pkg/utl/db"
|
"wallet-api/pkg/utl/db"
|
||||||
@@ -9,9 +10,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
fmt.Println("Start migrate")
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
|
||||||
dbUrl := os.Getenv("DATABASE_URL")
|
dbUrl := os.Getenv("DATABASE_URL")
|
||||||
|
fmt.Println("Database: ", dbUrl)
|
||||||
|
|
||||||
conn := db.CreateConnection(dbUrl)
|
conn := db.CreateConnection(dbUrl)
|
||||||
migrate.Start(conn)
|
migrate.Start(conn)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle
|
|||||||
wc.WalletService = as
|
wc.WalletService = as
|
||||||
|
|
||||||
s.POST("", wc.New)
|
s.POST("", wc.New)
|
||||||
s.GET("", wc.Get)
|
s.GET("", wc.GetAll)
|
||||||
|
|
||||||
return wc
|
return wc
|
||||||
}
|
}
|
||||||
@@ -48,3 +48,16 @@ func (wc *WalletsController) Get(c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wc *WalletsController) GetAll(c *gin.Context) {
|
||||||
|
body := new(models.Auth)
|
||||||
|
auth := c.MustGet("auth")
|
||||||
|
body.Id = auth.(*models.Auth).Id
|
||||||
|
|
||||||
|
fr := FilteredResponse(c)
|
||||||
|
|
||||||
|
wc.WalletService.GetAll(body, fr)
|
||||||
|
|
||||||
|
c.JSON(200, fr)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
@@ -21,9 +20,11 @@ func Auth(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
||||||
println(ok)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
exceptionReturn.ErrorCode = "401001"
|
||||||
|
exceptionReturn.StatusCode = 401
|
||||||
|
exceptionReturn.Message = "Invalid token"
|
||||||
|
c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
}
|
}
|
||||||
return []byte(secret), nil
|
return []byte(secret), nil
|
||||||
})
|
})
|
||||||
|
|||||||
19
pkg/middleware/cors.go
Normal file
19
pkg/middleware/cors.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func CORSMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
|
||||||
|
|
||||||
|
if c.Request.Method == "OPTIONS" {
|
||||||
|
c.AbortWithStatus(204)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredResponse) {
|
func FilteredResponse(qry *pg.Query, mdl interface{}, filtered *models.FilteredResponse) {
|
||||||
qry = qry.Limit(filtered.Rpp).Offset((filtered.Page - 1) * filtered.Rpp)
|
if filtered.Page == 0 {
|
||||||
|
filtered.Page = 1
|
||||||
|
}
|
||||||
|
if filtered.Rpp == 0 {
|
||||||
|
filtered.Rpp = 20
|
||||||
|
}
|
||||||
|
if filtered.SortBy == "" {
|
||||||
|
filtered.SortBy = "date_created DESC"
|
||||||
|
}
|
||||||
|
qry = qry.Limit(filtered.Rpp).Offset((filtered.Page - 1) * filtered.Rpp).Order(filtered.SortBy)
|
||||||
common.GenerateEmbed(qry, filtered.Embed)
|
common.GenerateEmbed(qry, filtered.Embed)
|
||||||
count, err := qry.SelectAndCount()
|
count, err := qry.SelectAndCount()
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|||||||
@@ -29,3 +29,10 @@ func (as *WalletService) Get(am *models.Auth, embed string) *models.Wallet {
|
|||||||
|
|
||||||
return wm
|
return wm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (as *WalletService) GetAll(am *models.Auth, filtered *models.FilteredResponse) {
|
||||||
|
wm := new([]models.Wallet)
|
||||||
|
|
||||||
|
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||||
|
FilteredResponse(query, wm, filtered)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user