fix query user by id

This commit is contained in:
andrusyakka
2023-07-14 16:11:24 +03:00
parent 567d07ce89
commit 459c5bebad
2 changed files with 17 additions and 6 deletions

View File

@@ -29,6 +29,22 @@ func (h *Handler) GetCurrentUser(c echo.Context) error {
return c.JSON(http.StatusOK, &u) return c.JSON(http.StatusOK, &u)
} }
func (h *Handler) GetUser(c echo.Context) error {
var user model.User
id := c.Param("id")
if id == "" {
return c.NoContent(http.StatusBadRequest)
}
tx := h.DB.Preload("Position").Where("id = ?", id).Find(user)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusNotFound)
}
return c.JSON(http.StatusOK, user)
}
func (h *Handler) GetUsers(c echo.Context) error { func (h *Handler) GetUsers(c echo.Context) error {
var users []model.User var users []model.User
@@ -38,16 +54,11 @@ func (h *Handler) GetUsers(c echo.Context) error {
tx := h.DB.Limit(limit).Offset(offset) tx := h.DB.Limit(limit).Offset(offset)
utils.ApplyIdFilter(c, tx)
utils.ApplySort(tx, sortCol, isDesc) utils.ApplySort(tx, sortCol, isDesc)
utils.ApplyFilters(tx, filterCol, filterVal) utils.ApplyFilters(tx, filterCol, filterVal)
tx = tx.Preload("Position").Omit("Password").Find(&users) tx = tx.Preload("Position").Omit("Password").Find(&users)
if tx.RowsAffected == 1 {
return c.JSON(http.StatusOK, users[0])
}
return c.JSON(http.StatusOK, users) return c.JSON(http.StatusOK, users)
} }

View File

@@ -45,7 +45,7 @@ func main() {
e.GET("/me", h.GetCurrentUser) e.GET("/me", h.GetCurrentUser)
e.POST("/", h.CreateUser) e.POST("/", h.CreateUser)
e.GET("/", h.GetUsers) e.GET("/", h.GetUsers)
e.GET("/:id", h.GetUsers) e.GET("/:id", h.GetUser)
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)