Merge branch 'feature/list-all-wallets-WA-9'

This commit is contained in:
Fran Jurmanović
2021-05-29 21:51:06 +02:00
7 changed files with 63 additions and 6 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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
View 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()
}
}

View File

@@ -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)

View File

@@ -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)
}