diff --git a/go.mod b/go.mod index 458378e..d24c94c 100644 --- a/go.mod +++ b/go.mod @@ -3,28 +3,32 @@ module clickandjoin.app/managementsystem go 1.18 require ( - git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.54 + git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.65 github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1 github.com/gofiber/fiber/v2 v2.42.0 github.com/gofiber/websocket/v2 v2.1.4 github.com/google/uuid v1.3.0 github.com/joho/godotenv v1.5.0 github.com/rabbitmq/amqp091-go v1.7.0 - github.com/rs/zerolog v1.29.0 github.com/scylladb/gocqlx/v2 v2.8.0 ) require ( github.com/andybalholm/brotli v1.0.4 // indirect github.com/fasthttp/websocket v1.5.1 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/klauspost/compress v1.15.15 // indirect + github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/philhofer/fwd v1.1.2 // indirect github.com/rivo/uniseg v0.4.3 // indirect + github.com/rs/zerolog v1.29.0 // indirect github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect github.com/savsgio/gotils v0.0.0-20230203094617-bcbc01813b4f // indirect github.com/scylladb/go-reflectx v1.0.1 // indirect @@ -32,6 +36,8 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.44.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/crypto v0.5.0 // indirect golang.org/x/sys v0.5.0 // indirect + golang.org/x/text v0.6.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect ) diff --git a/go.sum b/go.sum index b73f779..8e95cdd 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ -git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.47 h1:vbXCuHSA7IGpZSGyZfzi31MK/rlxBParBMDEZsdBdWk= -git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.47/go.mod h1:Jzc4/4ntrOLMOZYnUjkr1uBCVtRvPbEbQD+8kwBOdf4= -git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.54 h1:bAB8Z9qJgX/H+PNakoP1PK9Sox+oyy9+L4w7n/6JkBs= -git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.54/go.mod h1:/CvzIeBG4vJK/MgXzwmwCYwodiTGTMOYGDF3ao/fFLM= +git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.65 h1:YH6wNW49DhDzzhb40GwHJnwAOtLw07JUIUiZA+9g8xo= +git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper v1.0.65/go.mod h1:rnEM9rcZy2dg4SaDCGmSf34fp7ECzdyxxnRut2HBmrs= github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY= @@ -10,9 +8,17 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fasthttp/websocket v1.5.1 h1:iZsMv5OtZ1E52hhCnlOm/feLCrPhutlrZgvEGcZa1FM= github.com/fasthttp/websocket v1.5.1/go.mod h1:s+gJkEn38QXLkNfOe/n75Yb8we+VEho1vYqeUYheomw= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU= +github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s= github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1 h1:px9qUCy/RNJNsfCam4m2IxWGxNuimkrioEF0vrrbPsg= github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -33,11 +39,13 @@ github.com/joho/godotenv v1.5.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw= github.com/klauspost/compress v1.15.15/go.mod h1:ZcK2JAFqKOpnBlxcLsJzYfrS9X1akm9fHZNnD9+Vo/4= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= @@ -51,12 +59,14 @@ github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rabbitmq/amqp091-go v1.7.0 h1:V5CF5qPem5OGSnEo8BoSbsDGwejg6VUJsKEdneaoTUo= github.com/rabbitmq/amqp091-go v1.7.0/go.mod h1:wfClAtY0C7bOHxd3GjmF26jEHn+rR/0B3+YV+Vn9/NI= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= @@ -72,7 +82,9 @@ github.com/scylladb/gocqlx/v2 v2.8.0/go.mod h1:4/+cga34PVqjhgSoo5Nr2fX1MQIqZB5eC github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw= github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= @@ -93,6 +105,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= +golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -139,6 +153,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -155,4 +171,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index d0e45f9..b894b7e 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "clickandjoin.app/managementsystem/modules/config" "clickandjoin.app/managementsystem/modules/rabbitmq" "clickandjoin.app/managementsystem/modules/scylladb" + "clickandjoin.app/managementsystem/modules/utils" "clickandjoin.app/managementsystem/routers/router" "clickandjoin.app/managementsystem/socketserver" gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper" @@ -40,6 +41,8 @@ func init() { } func main() { + utils.ValidatorInit() + app := fiber.New() //app.Use(cors.New()) diff --git a/modules/structs/user.go b/modules/structs/user.go index a26e98d..f16ff8c 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -21,3 +21,16 @@ type UserRelationshipsResponse struct { type UserPrivacySettingsResponse struct { PrivacySettings []dbstructs.UserPrivacySettings } + +// swagger:model UpdateUserRequest +type UpdateUserRequest struct { + Username string + AccountName string + Email string + Description string + AccountStatus uint8 +} + +type UserIdParam struct { + UserId string +} diff --git a/modules/utils/utils.go b/modules/utils/utils.go index ec61898..2356562 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -3,7 +3,10 @@ package utils import ( "encoding/json" + "clickandjoin.app/managementsystem/modules/structs" gocnjhelper "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper" + "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/cnjglobals" + "git.clickandjoin.umbach.dev/ClickandJoin/go-cnj-helper/cnjvalidator" ) func MarshalMessage(message any) (marshaledMessage []byte, err error) { @@ -28,3 +31,11 @@ func UnmarshalReceivedMessage(body []byte, message any) error { return nil } + +func ValidatorInit() { + cnjvalidator.Validate.RegisterStructValidationMapRules(cnjglobals.GeneralRules, + structs.UpdateUserRequest{}, + structs.UserIdParam{}) + + cnjvalidator.Validate.RegisterValidation("regexp", cnjglobals.RegexTag) +} diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go new file mode 100644 index 0000000..ec169dd --- /dev/null +++ b/routers/api/v1/user/user.go @@ -0,0 +1,71 @@ +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 + // responses: + // '200': + // schema: + // "$ref": "#/definitions/UsersResponse" + // '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) + } + + 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) +} diff --git a/routers/router/router.go b/routers/router/router.go index c0186fb..02300d6 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -4,6 +4,7 @@ import ( "clickandjoin.app/managementsystem/modules/config" "clickandjoin.app/managementsystem/routers/api/v1/chats" "clickandjoin.app/managementsystem/routers/api/v1/stats" + "clickandjoin.app/managementsystem/routers/api/v1/user" "clickandjoin.app/managementsystem/routers/api/v1/users" "clickandjoin.app/managementsystem/routers/api/v1/wssessions" "github.com/gofiber/fiber/v2" @@ -17,7 +18,9 @@ func SetupRoutes(app *fiber.App) { us.Get("/signupprocesses", ApiKeyValidation, users.GetAllUserSignUpProcesses) us.Get("/relationships", ApiKeyValidation, users.GetAllUserRelationships) us.Get("/privacysettings", ApiKeyValidation, users.GetAllUserPrivacySettings) - //us.Get("/:userId", ApiKeyValidation, users.GetUserProfile) + + u := v1.Group("/user") + u.Patch("/:userId", ApiKeyValidation, user.UpdateUser) wss := v1.Group("/wssessions") wss.Get("/", ApiKeyValidation, wssessions.GetAllWsSessions)