diff --git a/handler/users.go b/handler/users.go index dbe8cff..c00623d 100644 --- a/handler/users.go +++ b/handler/users.go @@ -29,6 +29,22 @@ func (h *Handler) GetCurrentUser(c echo.Context) error { 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 { var users []model.User @@ -38,16 +54,11 @@ func (h *Handler) GetUsers(c echo.Context) error { tx := h.DB.Limit(limit).Offset(offset) - utils.ApplyIdFilter(c, tx) utils.ApplySort(tx, sortCol, isDesc) utils.ApplyFilters(tx, filterCol, filterVal) 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) } diff --git a/main.go b/main.go index 1ac03e2..178a1f6 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,7 @@ func main() { e.GET("/me", h.GetCurrentUser) e.POST("/", h.CreateUser) e.GET("/", h.GetUsers) - e.GET("/:id", h.GetUsers) + e.GET("/:id", h.GetUser) e.PATCH("/:id", h.UpdateUser) e.DELETE("/:id", h.DeleteUser) e.GET("/health", h.Health)