package handler import ( "astra-users/config" "astra-users/model" "astra-users/pkg/api" "astra-users/pkg/utils" "crypto/sha256" "encoding/json" "fmt" "github.com/google/uuid" "github.com/labstack/echo/v4" "net/http" "net/url" ) func (h *Handler) GetCurrentUser(c echo.Context) error { userId := c.Request().Header.Get("X-User-Id") if userId == "" { return c.NoContent(http.StatusBadRequest) } u := new(model.User) tx := h.DB.Preload("Position").Where("id = ?", userId).First(&u) 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 []uuid.UUID 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) } func (h *Handler) UploadAvatar(c echo.Context) error { id := c.Param("id") type FileSuccess struct { FileName string `json:"file_name"` } result := new(FileSuccess) data, err, resp := api.PostRequest(config.Env.MSHosts.StorageHost+"/upload", c.Request().Body, c.Request().Header) if err != nil || resp.StatusCode != 200 { return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data) } err = json.Unmarshal(data, &result) if err != nil { return c.NoContent(http.StatusBadGateway) } user := new(model.User) tx := h.DB.Where("id = ?", id).First(&user) if tx.RowsAffected == 0 { return c.NoContent(http.StatusNotFound) } user.Avatar = result.FileName tx = h.DB.Save(&user) if tx.RowsAffected == 0 { return c.NoContent(http.StatusBadGateway) } return c.JSON(http.StatusOK, map[string]string{ "avatar": result.FileName, }) }