diff --git a/cmd/api/main.go b/cmd/api/main.go index 5c72941..84d5a58 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -1,8 +1,10 @@ package main import ( + "fmt" "os" "wallet-api/pkg/api" + "wallet-api/pkg/middleware" "wallet-api/pkg/utl/db" "wallet-api/pkg/utl/server" @@ -11,10 +13,13 @@ import ( ) func main() { + fmt.Println("Start") godotenv.Load() dbUrl := os.Getenv("DATABASE_URL") - r := gin.Default() + fmt.Println("Database: ", dbUrl) + r := gin.New() + r.Use(middleware.CORSMiddleware()) conn := db.CreateConnection(dbUrl) api.Init(r, conn) diff --git a/cmd/migrate/main.go b/cmd/migrate/main.go index 98e7e9a..1e5fac7 100644 --- a/cmd/migrate/main.go +++ b/cmd/migrate/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "wallet-api/pkg/migrate" "wallet-api/pkg/utl/db" @@ -9,9 +10,11 @@ import ( ) func main() { + fmt.Println("Start migrate") godotenv.Load() dbUrl := os.Getenv("DATABASE_URL") + fmt.Println("Database: ", dbUrl) conn := db.CreateConnection(dbUrl) migrate.Start(conn) diff --git a/pkg/controllers/wallets.go b/pkg/controllers/wallets.go index c4e3008..cb1d434 100644 --- a/pkg/controllers/wallets.go +++ b/pkg/controllers/wallets.go @@ -17,7 +17,7 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle wc.WalletService = as s.POST("", wc.New) - s.GET("", wc.Get) + s.GET("", wc.GetAll) return wc } @@ -48,3 +48,16 @@ func (wc *WalletsController) Get(c *gin.Context) { 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) + +} diff --git a/pkg/middleware/auth.go b/pkg/middleware/auth.go index 14f3b53..2eb0ef0 100644 --- a/pkg/middleware/auth.go +++ b/pkg/middleware/auth.go @@ -1,7 +1,6 @@ package middleware import ( - "fmt" "os" "strings" "wallet-api/pkg/models" @@ -21,9 +20,11 @@ func Auth(c *gin.Context) { } 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"]) + exceptionReturn.ErrorCode = "401001" + exceptionReturn.StatusCode = 401 + exceptionReturn.Message = "Invalid token" + c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn) } return []byte(secret), nil }) diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go new file mode 100644 index 0000000..23d62cc --- /dev/null +++ b/pkg/middleware/cors.go @@ -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() + } +} diff --git a/pkg/services/services.go b/pkg/services/services.go index 3694de0..0f84e73 100644 --- a/pkg/services/services.go +++ b/pkg/services/services.go @@ -8,7 +8,16 @@ import ( ) 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) count, err := qry.SelectAndCount() common.CheckError(err) diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 66caaa3..2213fee 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -29,3 +29,10 @@ func (as *WalletService) Get(am *models.Auth, embed string) *models.Wallet { 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) +}