feat: added first api route
This commit is contained in:
36
internal/echo/routes/routes.go
Normal file
36
internal/echo/routes/routes.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"blog/internal/echo/handlers"
|
||||
"blog/internal/echo/middleware"
|
||||
"blog/internal/services"
|
||||
"strconv"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
// Helper method to get int param or default value
|
||||
func getIntParam(c *echo.Context, name string, defaultValue int) int {
|
||||
value, err := strconv.Atoi(c.ParamOr(name, strconv.Itoa(defaultValue)))
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func SetupRoutes(e *echo.Echo, s *services.StrapiService) {
|
||||
// Global middleware
|
||||
e.Use(middleware.ServerHandler)
|
||||
|
||||
// Post routes
|
||||
posts := e.Group("/posts") // Routing group
|
||||
|
||||
// GET /posts/all
|
||||
posts.GET("/all", func(c *echo.Context) error {
|
||||
pageSize := getIntParam(c, "pageSize", 10)
|
||||
page := getIntParam(c, "page", 1)
|
||||
|
||||
return handlers.GetAllPosts(c, s, pageSize, page)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user