100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
package user
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"clickandjoin.app/managementsystem/modules/scylladb"
|
|
"clickandjoin.app/managementsystem/modules/structs"
|
|
gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper"
|
|
"git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/cnjvalidator"
|
|
"git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/dbstructs"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func UpdateUser(c *fiber.Ctx) error {
|
|
// swagger:operation PATCH /user/:userId user userUpdateUser
|
|
// ---
|
|
// summary: Update an user
|
|
// consumes:
|
|
// - application/json
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: body
|
|
// in: body
|
|
// schema:
|
|
// "$ref": "#/definitions/UpdateUserRequest"
|
|
// responses:
|
|
// '200':
|
|
// description: User was updated
|
|
// '500':
|
|
// description: Internal server error
|
|
|
|
var params structs.UserIdParam
|
|
|
|
if err := c.ParamsParser(¶ms); err != nil {
|
|
gocnjhelper.LogErrorf("Failed to parse params %s", err)
|
|
return c.Status(fiber.StatusBadRequest).JSON(err)
|
|
}
|
|
|
|
errValidation := cnjvalidator.ValidateStruct(params)
|
|
|
|
if errValidation != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(errValidation)
|
|
}
|
|
|
|
var body structs.UpdateUserRequest
|
|
|
|
if err := c.BodyParser(&body); err != nil {
|
|
gocnjhelper.LogErrorf("Failed to parse request data %s", err)
|
|
return c.Status(fiber.StatusBadRequest).JSON(err)
|
|
}
|
|
|
|
errValidation = cnjvalidator.ValidateStruct(body)
|
|
|
|
if errValidation != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(errValidation)
|
|
}
|
|
|
|
// get requested user from db
|
|
foundUser := dbstructs.User{Id: params.UserId}
|
|
|
|
if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Get("email", "account_name_lc", "last_account_name_lc")).BindStruct(foundUser).GetRelease(&foundUser); err != nil {
|
|
gocnjhelper.LogErrorf("Failed to find user, err: %s", err.Error())
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
|
|
// check whether the email in the requested has been changed in relation to the user
|
|
if foundUser.Email != body.Email {
|
|
if status, err := cnjvalidator.HandleEmailValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.Email); err != nil {
|
|
return c.SendStatus(status)
|
|
}
|
|
}
|
|
|
|
accountNameLc := strings.ToLower(body.AccountName)
|
|
|
|
// check whether the account name in the requested has been changed in relation to the user
|
|
if foundUser.AccountNameLc != accountNameLc {
|
|
if status, err := cnjvalidator.HandleAccountNameValidation(scylladb.Session, scylladb.Cluster.Keyspace, body.AccountName); err != nil {
|
|
return c.SendStatus(status)
|
|
}
|
|
}
|
|
|
|
updatedUser := dbstructs.User{
|
|
Id: params.UserId,
|
|
Username: body.Username,
|
|
AccountName: body.AccountName,
|
|
AccountNameLc: strings.ToLower(body.AccountName),
|
|
Email: body.Email,
|
|
Description: body.Description,
|
|
AccountStatus: body.AccountStatus,
|
|
}
|
|
|
|
if err := scylladb.Session.Query(gocnjhelper.DbMUsers.Update("username", "account_name", "account_name_lc", "email", "description", "account_status")).BindStruct(updatedUser).ExecRelease(); err != nil {
|
|
gocnjhelper.LogErrorf("Failed to update user, err: %s", err.Error())
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|