add batch request

This commit is contained in:
andrusyakka
2023-07-14 16:54:49 +03:00
parent afaf47acd3
commit 4a6a326f6d
2 changed files with 25 additions and 0 deletions

View File

@@ -4,9 +4,11 @@ import (
"astra-users/model" "astra-users/model"
"astra-users/pkg/utils" "astra-users/pkg/utils"
"crypto/sha256" "crypto/sha256"
"encoding/json"
"fmt" "fmt"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"net/http" "net/http"
"net/url"
) )
func (h *Handler) GetCurrentUser(c echo.Context) error { 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) 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 { func (h *Handler) GetUser(c echo.Context) error {
var user model.User var user model.User
id := c.Param("id") id := c.Param("id")

View File

@@ -46,6 +46,7 @@ func main() {
e.POST("/", h.CreateUser) e.POST("/", h.CreateUser)
e.GET("/", h.GetUsers) e.GET("/", h.GetUsers)
e.GET("/:id", h.GetUser) e.GET("/:id", h.GetUser)
e.GET("/batch/:ids", h.GetUserBatch)
e.PATCH("/:id", h.UpdateUser) e.PATCH("/:id", h.UpdateUser)
e.DELETE("/:id", h.DeleteUser) e.DELETE("/:id", h.DeleteUser)
e.GET("/health", h.Health) e.GET("/health", h.Health)