154 lines
3.0 KiB
Go
154 lines
3.0 KiB
Go
package handler
|
|
|
|
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 {
|
|
userId := c.Request().Header.Get("UserId")
|
|
|
|
if userId == "" {
|
|
return c.NoContent(http.StatusBadRequest)
|
|
}
|
|
|
|
u := new(model.User)
|
|
|
|
tx := h.DB.First(&u, userId)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusNotFound)
|
|
}
|
|
|
|
u.Password = ""
|
|
|
|
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.Omit("Password").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")
|
|
if id == "" {
|
|
return c.NoContent(http.StatusBadRequest)
|
|
}
|
|
|
|
tx := h.DB.Preload("Position").Omit("Password").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
|
|
|
|
query := c.Request().URL.Query()
|
|
limit, offset := utils.GetPaginationParams(query)
|
|
sortCol, isDesc, filterCol, filterVal := utils.GetFilterParams(query)
|
|
|
|
tx := h.DB.Limit(limit).Offset(offset)
|
|
|
|
utils.ApplySort(tx, sortCol, isDesc)
|
|
utils.ApplyFilters(tx, filterCol, filterVal)
|
|
|
|
tx = tx.Preload("Position").Omit("Password").Find(&users)
|
|
|
|
return c.JSON(http.StatusOK, users)
|
|
}
|
|
|
|
func (h *Handler) UpdateUser(c echo.Context) (err error) {
|
|
var user = new(model.User)
|
|
var apiUser = new(model.APIUser)
|
|
tx := h.DB
|
|
|
|
id := c.Param("id")
|
|
tx = tx.Find(&user, id)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusNotFound)
|
|
}
|
|
|
|
if err = c.Bind(apiUser); err != nil {
|
|
return c.NoContent(http.StatusBadRequest)
|
|
}
|
|
|
|
user.FirstName = apiUser.FirstName
|
|
user.LastName = apiUser.LastName
|
|
|
|
tx = h.DB.Save(&user)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusBadGateway)
|
|
}
|
|
|
|
user.Password = ""
|
|
|
|
return c.JSON(http.StatusOK, &user)
|
|
}
|
|
|
|
func (h *Handler) DeleteUser(c echo.Context) (err error) {
|
|
id := c.Param("id")
|
|
|
|
tx := h.DB.Delete(&model.User{}, id)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusBadGateway)
|
|
}
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (h *Handler) CreateUser(c echo.Context) (err error) {
|
|
user := new(model.APIUser)
|
|
|
|
if err = c.Bind(user); err != nil {
|
|
return c.NoContent(http.StatusBadRequest)
|
|
}
|
|
|
|
user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(user.Password)))
|
|
|
|
newUser, err := utils.TypeConverter[model.User](&user)
|
|
|
|
tx := h.DB.Create(&newUser)
|
|
|
|
if tx.RowsAffected == 0 {
|
|
return c.NoContent(http.StatusBadGateway)
|
|
}
|
|
|
|
newUser.Password = ""
|
|
|
|
h.DB.Find(&newUser.Position, newUser.PositionID)
|
|
|
|
return c.JSON(http.StatusCreated, newUser)
|
|
}
|