diff --git a/handler/users.go b/handler/users.go index 9868419..bad5505 100644 --- a/handler/users.go +++ b/handler/users.go @@ -4,9 +4,11 @@ import ( "astra-users/model" "astra-users/pkg/utils" "crypto/sha256" + "encoding/json" "fmt" "github.com/labstack/echo/v4" "net/http" + "net/url" ) func (h *Handler) GetCurrentUser(c echo.Context) error { @@ -29,6 +31,28 @@ func (h *Handler) GetCurrentUser(c echo.Context) error { return c.JSON(http.StatusOK, &u) } +func (h *Handler) GetUserBatch(c echo.Context) error { + var users []model.User + var ids []uint + + rawIds := c.Param("ids") + decodedIds, err := url.QueryUnescape(rawIds) + + if err != nil { + return c.NoContent(http.StatusBadRequest) + } + + err = json.Unmarshal([]byte(decodedIds), &ids) + + if err != nil { + return c.NoContent(http.StatusBadRequest) + } + + h.DB.Find(&users, ids) + + return c.JSON(http.StatusOK, users) +} + func (h *Handler) GetUser(c echo.Context) error { var user model.User id := c.Param("id") diff --git a/main.go b/main.go index 178a1f6..51a8cb2 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,7 @@ func main() { e.POST("/", h.CreateUser) e.GET("/", h.GetUsers) e.GET("/:id", h.GetUser) + e.GET("/batch/:ids", h.GetUserBatch) e.PATCH("/:id", h.UpdateUser) e.DELETE("/:id", h.DeleteUser) e.GET("/health", h.Health)