add new methods and update current

This commit is contained in:
andrusyakka
2023-08-17 17:08:34 +03:00
parent 8bfbee4fe2
commit e007d66f51
4 changed files with 140 additions and 17 deletions

View File

@@ -92,21 +92,33 @@ func (h *Handler) GetUsers(c echo.Context) error {
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
id := c.Param("id")
tx := h.DB.Find(&user, id)
if tx.RowsAffected == 0 {
return c.NoContent(http.StatusNotFound)
}
if apiUser.FirstName != "" {
user.FirstName = apiUser.FirstName
}
if apiUser.LastName != "" {
user.LastName = apiUser.LastName
}
if apiUser.PositionID.ID() != 0 {
user.PositionID = apiUser.PositionID
}
if apiUser.Role != "" {
user.Role = apiUser.Role
}
if apiUser.Password != "" {
user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(apiUser.Password)))
}
tx = h.DB.Save(&user)