Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active May 22, 2021 10:32
Show Gist options
  • Save dipeshhkc/077750fdca6b26c9306c11cd239df887 to your computer and use it in GitHub Desktop.
Save dipeshhkc/077750fdca6b26c9306c11cd239df887 to your computer and use it in GitHub Desktop.
package route
func RunAPI(address string) error {
userHandler := handler.NewUserHandler()
productHandler := handler.NewProductHandler()
orderHandler := handler.NewOrderHandler()
r := gin.Default()
r.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "Welcome to Our Mini Ecommerce")
})
apiRoutes := r.Group("/api")
userRoutes := apiRoutes.Group("/user")
{
userRoutes.POST("/register", userHandler.AddUser)
userRoutes.POST("/signin", userHandler.SignInUser)
}
userProtectedRoutes := apiRoutes.Group("/users", middleware.AuthorizeJWT())
{
userProtectedRoutes.GET("/", userHandler.GetAllUser)
userProtectedRoutes.GET("/:user", userHandler.GetUser)
userProtectedRoutes.GET("/:user/products", userHandler.GetProductOrdered)
userProtectedRoutes.PUT("/:user", userHandler.UpdateUser)
userProtectedRoutes.DELETE("/:user", userHandler.DeleteUser)
}
productProtectedRoutes := apiRoutes.Group("/products", middleware.AuthorizeJWT())
{
productProtectedRoutes.GET("/", productHandler.GetAllProduct)
productProtectedRoutes.GET("/:product", productHandler.GetProduct)
productProtectedRoutes.POST("/", productHandler.AddProduct)
productProtectedRoutes.PUT("/:product", productHandler.UpdateProduct)
productProtectedRoutes.DELETE("/:product", productHandler.DeleteProduct)
}
orderProtectedRoutes := apiRoutes.Group("/order", middleware.AuthorizeJWT())
{
orderProtectedRoutes.POST("/product/:product/quantity/:quantity", orderHandler.OrderProduct)
}
return r.Run(address)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment