Skip to content

Commit

Permalink
update: admin user updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kayprogrammer committed Nov 30, 2024
1 parent 930309c commit 55ee61a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 58 deletions.
28 changes: 16 additions & 12 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const docTemplate = `{
}
}
},
"/admin/users/user": {
"/admin/users/{username}": {
"put": {
"security": [
{
Expand All @@ -265,6 +265,14 @@ const docTemplate = `{
],
"summary": "Update User Role",
"parameters": [
{
"type": "string",
"default": "username",
"description": "Username",
"name": "username",
"in": "path",
"required": true
},
{
"description": "User role update data",
"name": "data",
Expand Down Expand Up @@ -303,7 +311,7 @@ const docTemplate = `{
}
}
},
"/admin/users/user/{username}/toggle-activation": {
"/admin/users/{username}/toggle-activation": {
"get": {
"security": [
{
Expand Down Expand Up @@ -4493,17 +4501,13 @@ const docTemplate = `{
"schemas.UpdateUserRoleSchema": {
"type": "object",
"properties": {
"acc_type": {
"type": "string",
"maxLength": 7,
"minLength": 6,
"account_type": {
"allOf": [
{
"$ref": "#/definitions/choices.AccType"
}
],
"example": "WRITER"
},
"username": {
"type": "string",
"maxLength": 1000,
"minLength": 3,
"example": "john-doe"
}
}
},
Expand Down
28 changes: 16 additions & 12 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
}
}
},
"/admin/users/user": {
"/admin/users/{username}": {
"put": {
"security": [
{
Expand All @@ -258,6 +258,14 @@
],
"summary": "Update User Role",
"parameters": [
{
"type": "string",
"default": "username",
"description": "Username",
"name": "username",
"in": "path",
"required": true
},
{
"description": "User role update data",
"name": "data",
Expand Down Expand Up @@ -296,7 +304,7 @@
}
}
},
"/admin/users/user/{username}/toggle-activation": {
"/admin/users/{username}/toggle-activation": {
"get": {
"security": [
{
Expand Down Expand Up @@ -4486,17 +4494,13 @@
"schemas.UpdateUserRoleSchema": {
"type": "object",
"properties": {
"acc_type": {
"type": "string",
"maxLength": 7,
"minLength": 6,
"account_type": {
"allOf": [
{
"$ref": "#/definitions/choices.AccType"
}
],
"example": "WRITER"
},
"username": {
"type": "string",
"maxLength": 1000,
"minLength": 3,
"example": "john-doe"
}
}
},
Expand Down
22 changes: 11 additions & 11 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1203,16 +1203,10 @@ definitions:
type: object
schemas.UpdateUserRoleSchema:
properties:
acc_type:
account_type:
allOf:
- $ref: '#/definitions/choices.AccType'
example: WRITER
maxLength: 7
minLength: 6
type: string
username:
example: john-doe
maxLength: 1000
minLength: 3
type: string
type: object
schemas.UserDataSchema:
properties:
Expand Down Expand Up @@ -1488,12 +1482,18 @@ paths:
summary: List Users with Pagination
tags:
- Admin | Users
/admin/users/user:
/admin/users/{username}:
put:
consumes:
- application/json
description: Updates the account type of a specified user.
parameters:
- default: username
description: Username
in: path
name: username
required: true
type: string
- description: User role update data
in: body
name: data
Expand Down Expand Up @@ -1524,7 +1524,7 @@ paths:
summary: Update User Role
tags:
- Admin | Users
/admin/users/user/{username}/toggle-activation:
/admin/users/{username}/toggle-activation:
get:
consumes:
- application/json
Expand Down
30 changes: 11 additions & 19 deletions routes/admin_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/LitPad/backend/models"
"github.com/LitPad/backend/models/choices"
"github.com/LitPad/backend/models/scopes"
"github.com/LitPad/backend/schemas"
"github.com/LitPad/backend/utils"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -65,37 +66,28 @@ func (ep Endpoint) AdminGetUsers(c *fiber.Ctx) error {
// @Tags Admin | Users
// @Accept json
// @Produce json
// @Param username path string true "Username" default(username)
// @Param data body schemas.UpdateUserRoleSchema true "User role update data"
// @Success 200 {object} schemas.UserProfileResponseSchema "Successfully updated user details"
// @Failure 400 {object} utils.ErrorResponse "Invalid request data"
// @Failure 404 {object} utils.ErrorResponse "User not found"
// @Failure 500 {object} utils.ErrorResponse "Internal server error"
// @Router /admin/users/user [put]
// @Router /admin/users/{username} [put]
// @Security BearerAuth
func (ep Endpoint) AdminUpdateUser(c *fiber.Ctx) error {
db := ep.DB
data := schemas.UpdateUserRoleSchema{}

if errCode, errData := ValidateRequest(c, &data);

errData != nil{
errCode, errData := ValidateRequest(c, &data);
if errData != nil{
return c.Status(*errCode).JSON(errData)
}

accountType := choices.AccType(data.AccountType)

var user models.User

if data.Username != nil{

result := db.Where("username = ?", data.Username).First(&user)
if result.Error != nil{
return c.Status(404).JSON(utils.RequestErr(utils.ERR_NOT_FOUND, "User Not Found"))
}

user.AccountType = accountType
user := models.User{Username: c.Params("username")}
db.Scopes(scopes.FollowerFollowingPreloaderScope).Take(&user, user)
if user.ID == uuid.Nil{
return c.Status(404).JSON(utils.NotFoundErr("User Not Found"))
}

user.AccountType = data.AccountType
db.Save(&user)

response := schemas.UserProfileResponseSchema{
Expand All @@ -114,7 +106,7 @@ func (ep Endpoint) AdminUpdateUser(c *fiber.Ctx) error {
// @Failure 400 {object} utils.ErrorResponse "Invalid request data"
// @Failure 404 {object} utils.ErrorResponse "User not found"
// @Failure 500 {object} utils.ErrorResponse "Internal server error"
// @Router /admin/users/user/{username}/toggle-activation [get]
// @Router /admin/users/{username}/toggle-activation [get]
// @Security BearerAuth
func (ep Endpoint) ToggleUserActivation(c *fiber.Ctx) error {
db := ep.DB
Expand Down
4 changes: 2 additions & 2 deletions routes/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func SetupRoutes(app *fiber.App, db *gorm.DB, ws *internetcomputer.WalletService
// Admin Users
adminRouter.Put("/", endpoint.AdminMiddleware, endpoint.UpdateProfile)
adminRouter.Get("/users", endpoint.AdminMiddleware, endpoint.AdminGetUsers)
adminRouter.Put("/users/user", endpoint.AdminMiddleware, endpoint.AdminUpdateUser)
adminRouter.Get("/users/user/:username/toggle-activation", endpoint.AdminMiddleware, endpoint.ToggleUserActivation)
adminRouter.Put("/users/:username", endpoint.AdminMiddleware, endpoint.AdminUpdateUser)
adminRouter.Get("/users/:username/toggle-activation", endpoint.AdminMiddleware, endpoint.ToggleUserActivation)

// Admin Books (2)
adminRouter.Get("/books", endpoint.AdminMiddleware, endpoint.AdminGetBooks)
Expand Down
3 changes: 1 addition & 2 deletions schemas/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ type UpdateUserProfileSchema struct {
}

type UpdateUserRoleSchema struct {
AccountType string `json:"acc_type" validate:"min=6,max=7" example:"WRITER"`
Username *string `json:"username,omitempty" validate:"min=3,max=1000" example:"john-doe"`
AccountType choices.AccType `json:"account_type" validate:"account_type_validator" example:"WRITER"`
}

type UpdatePasswordSchema struct {
Expand Down
2 changes: 2 additions & 0 deletions utils/validator_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
translator, _ = uni.GetTranslator("en")

// Register Custom Validators
customValidator.RegisterValidation("account_type_validator", AccountTypeValidator)
customValidator.RegisterValidation("payment_type_validator", PaymentTypeValidator)
customValidator.RegisterValidation("subscription_type_validator", SubscriptionTypeValidator)
customValidator.RegisterValidation("rating_choice_validator", RatingChoiceValidator)
Expand Down Expand Up @@ -63,6 +64,7 @@ func registerTranslations(param string) {
registerTranslation("required", "This field is required.", translator)
registerTranslation("required_if", "This field is required.", translator)
registerTranslation("required_without", "This field is required.", translator)
registerTranslation("account_type_validator", "Invalid account type", translator)
registerTranslation("payment_type_validator", "Invalid payment type", translator)
registerTranslation("subscription_type_validator", "Invalid subscription type", translator)
registerTranslation("rating_choice_validator", "Invalid rating choice", translator)
Expand Down
5 changes: 5 additions & 0 deletions utils/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"github.com/go-playground/validator/v10"
)

// Validates if a account type value is the correct one
func AccountTypeValidator(fl validator.FieldLevel) bool {
return fl.Field().Interface().(choices.AccType).IsValid()
}

// Validates if a payment type value is the correct one
func PaymentTypeValidator(fl validator.FieldLevel) bool {
return fl.Field().Interface().(choices.PaymentType).IsValid()
Expand Down

0 comments on commit 55ee61a

Please sign in to comment.