Skip to content

Commit

Permalink
Add API to retrieve todo list
Browse files Browse the repository at this point in the history
  • Loading branch information
ryichk committed Dec 30, 2024
1 parent 09609a4 commit 729ed95
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
25 changes: 21 additions & 4 deletions api/internal/handler/todo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
package handler

// func (h *Handler) ListTodos(c echo.Context) error {
// rc := c.Request().Context()
import (
"net/http"

// todos, err := h.queries.ListTodos(rc)
// }
"github.com/labstack/echo/v4"
)

func (h *Handler) ListTodos(c echo.Context) error {
ctx := c.Request().Context()

userInfo, err := h.AcquireConnection(ctx, c)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
defer userInfo.Conn.Release()

todos, err := h.queries.ListTodos(ctx, userInfo.Conn)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, todos)
}
6 changes: 6 additions & 0 deletions api/internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ import (
func PublicRoutes(e *echo.Echo, h *handler.Handler, db *pgxpool.Pool, queries *model.Queries) {
e.GET("/", h.Hello)
}

func PrivateRoutes(e *echo.Echo, h *handler.Handler, db *pgxpool.Pool, queries *model.Queries) {
e.Use(AuthMiddleware())

e.GET("/todos", h.ListTodos)
}
1 change: 1 addition & 0 deletions api/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewServer(db *pgxpool.Pool) (*echo.Echo, error) {
}))

PublicRoutes(e, h, db, queries)
PrivateRoutes(e, h, db, queries)

return e, nil
}

0 comments on commit 729ed95

Please sign in to comment.