Spaces:
Running
Running
File size: 9,636 Bytes
b4c7b98 dc9bcf8 b4c7b98 dc9bcf8 b4c7b98 ff2bb22 b4c7b98 ff2bb22 b4c7b98 bdfd28f b4c7b98 dc9bcf8 b4c7b98 dc9bcf8 bdfd28f 132b9f0 dc9bcf8 bdfd28f dc9bcf8 132b9f0 dc9bcf8 b4c7b98 132b9f0 b4c7b98 132b9f0 b4c7b98 dc9bcf8 b4c7b98 ff2bb22 b4c7b98 dc9bcf8 b4c7b98 132b9f0 b4c7b98 a57273b b4c7b98 c3e284f a57273b c3e284f b4c7b98 ff2bb22 b4c7b98 dc9bcf8 b4c7b98 ff2bb22 b4c7b98 dc9bcf8 b4c7b98 a57273b b4c7b98 a57273b c3e284f 8321c4a 8ee9db4 b4c7b98 dcca71a b4c7b98 a57273b b4c7b98 ff2bb22 b4c7b98 dc9bcf8 b4c7b98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | package controllers
import (
"strconv"
"abdanhafidz.com/go-boilerplate/models/dto"
entity "abdanhafidz.com/go-boilerplate/models/entity"
http_error "abdanhafidz.com/go-boilerplate/models/error"
"abdanhafidz.com/go-boilerplate/services"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type UserController interface {
ListUsers(ctx *gin.Context)
CreateUser(ctx *gin.Context)
BulkCreateUsers(ctx *gin.Context)
UpdateUser(ctx *gin.Context)
DeleteUser(ctx *gin.Context)
}
type userController struct {
accountService services.AccountService
}
func NewUserController(accountService services.AccountService) UserController {
return &userController{accountService}
}
// ListUsers godoc
// @Summary List All Users with Pagination
// @Description Retrieve a paginated list of all users with optional role filter. Supports pagination parameters (page, limit) and can filter by user role.
// @Tags Super Admin Users Management
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param page query int false "Page number for pagination. Minimum value is 1. Default is 1."
// @Param limit query int false "Number of items per page. Minimum 1, Maximum 50. Default is 10."
// @Param role query string false "Filter users by role. Allowed values: user, admin, super_admin. Leave empty for no filter."
// @Success 200 {object} dto.SuccessResponse[[]dto.UserResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/users [get]
func (c *userController) ListUsers(ctx *gin.Context) {
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
role := ctx.DefaultQuery("role", "")
search := ctx.DefaultQuery("search", "")
if limit < 1 {
limit = 10
} else if limit > 50 {
limit = 50
}
if page < 1 {
page = 1
}
validRoles := map[string]bool{
"user": true,
"admin": true,
"super_admin": true,
}
if role != "" && !validRoles[role] {
ResponseJSON(ctx, gin.H{}, []dto.UserResponse{}, http_error.BAD_REQUEST_ERROR)
return
}
offset := (page - 1) * limit
p := entity.Pagination{
Limit: limit,
Offset: offset,
Role: role,
Search: search,
}
accounts, total, err := c.accountService.GetAllAccountsWithPagination(ctx.Request.Context(), p)
if err != nil {
ResponseJSON(ctx, gin.H{}, []dto.UserResponse{}, err)
return
}
var users []dto.UserResponse
for _, acc := range accounts {
fullName := ""
if acc.AccountDetail != nil && acc.AccountDetail.FullName != nil {
fullName = *acc.AccountDetail.FullName
}
users = append(users, dto.UserResponse{
Id: acc.Id,
Username: acc.Username,
Email: acc.Email,
FullName: fullName,
Role: acc.Role,
IsEmailVerified: acc.IsEmailVerified,
IsDetailCompleted: acc.IsDetailCompleted,
CreatedAt: acc.CreatedAt,
})
}
var totalPages int
if total == 0 {
totalPages = 1
} else {
totalPages = int((total + int64(limit) - 1) / int64(limit))
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, users, nil)
}
// CreateUser godoc
// @Summary Create Single User
// @Description Create a new user account by providing user details
// @Tags Super Admin Users Management
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body dto.CreateUserRequest true "Create User Request"
// @Success 200 {object} dto.SuccessResponse[dto.UserResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/users [post]
func (c *userController) CreateUser(ctx *gin.Context) {
req := RequestJSON[dto.CreateUserRequest](ctx)
account, err := c.accountService.Create(ctx.Request.Context(), req.Name, req.Email, req.Username, req.Password, req.Role)
if err != nil {
ResponseJSON(ctx, req, dto.UserResponse{}, err)
return
}
if req.Role != "" && req.Role != "user" {
account.Role = req.Role
account.IsEmailVerified = true
account, _ = c.accountService.UpdateUserRole(ctx.Request.Context(), account)
err = c.accountService.SetEmailVerified(ctx.Request.Context(), account.Id, true)
if err != nil {
ResponseJSON(ctx, req, dto.UserResponse{}, err)
return
}
}
res := dto.UserResponse{
Id: account.Id,
Username: account.Username,
Email: account.Email,
Role: account.Role,
IsEmailVerified: account.IsEmailVerified,
IsDetailCompleted: account.IsDetailCompleted,
CreatedAt: account.CreatedAt,
}
ResponseJSON(ctx, req, res, nil)
}
// BulkCreateUsers godoc
// @Summary Bulk Create Users
// @Description Create multiple user accounts at once
// @Tags Super Admin Users Management
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param requests body []dto.CreateUserRequest true "Bulk Create User Requests"
// @Success 200 {object} dto.SuccessResponse[dto.CreateUserRequest]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/users/bulk [post]
func (c *userController) BulkCreateUsers(ctx *gin.Context) {
var requests []dto.CreateUserRequest
if err := ctx.ShouldBindJSON(&requests); err != nil {
ResponseJSON(ctx, requests, dto.CreateUserRequest{}, http_error.BAD_REQUEST_ERROR)
ctx.Abort()
return
}
response, err := c.accountService.BulkCreateAccounts(ctx.Request.Context(), requests)
ResponseJSON(ctx, gin.H{"count": len(requests)}, response, err)
}
// GetUserById godoc
// @Summary Edit User
// @Description Update user information by ID
// @Tags Super Admin Users Management
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "User ID"
// @Param request body dto.UpdateUserRequest true "Update User Request"
// @Success 200 {object} dto.SuccessResponse[dto.UserResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/users/{id} [put]
func (c *userController) UpdateUser(ctx *gin.Context) {
idParam := ctx.Param("id")
userId, err := uuid.Parse(idParam)
if err != nil {
ResponseJSON(ctx, gin.H{"id": idParam}, dto.UserResponse{}, http_error.INVALID_TOKEN)
return
}
req := RequestJSON[dto.UpdateUserRequest](ctx)
account, err := c.accountService.GetById(ctx.Request.Context(), userId)
if err != nil {
ResponseJSON(ctx, gin.H{"id": userId}, dto.UserResponse{}, err)
return
}
if account.Role == "super_admin" || account.Role == "admin" {
account.IsEmailVerified = true
err = c.accountService.SetEmailVerified(ctx.Request.Context(), account.Id, true)
if err != nil {
ResponseJSON(ctx, req, dto.UserResponse{}, err)
return
}
}
// Update fields if provided
if req.Email != "" {
account.Email = req.Email
}
if req.Username != "" {
account.Username = req.Username
}
if req.Role != "" {
account.Role = req.Role
}
if req.Password != "" {
account.Password = req.Password
}
if req.Password != "" {
account, err = c.accountService.Update(ctx.Request.Context(), account)
} else {
account, err = c.accountService.UpdateUserRole(ctx.Request.Context(), account)
}
if err != nil {
ResponseJSON(ctx, req, dto.UserResponse{}, err)
return
}
res := dto.UserResponse{
Id: account.Id,
Username: account.Username,
Email: account.Email,
Role: account.Role,
IsEmailVerified: account.IsEmailVerified,
IsDetailCompleted: account.IsDetailCompleted,
CreatedAt: account.CreatedAt,
}
ResponseJSON(ctx, req, res, nil)
}
// DeleteUser godoc
// @Summary Delete User
// @Description Delete a user account by ID
// @Tags Super Admin Users Management
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "User ID"
// @Success 200 {object} dto.SuccessResponse[any]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/users/{id} [delete]
func (c *userController) DeleteUser(ctx *gin.Context) {
idParam := ctx.Param("id")
userId, err := uuid.Parse(idParam)
if err != nil {
ResponseJSON[any](ctx, gin.H{"id": idParam}, nil, http_error.INVALID_TOKEN)
return
}
_, err = c.accountService.GetById(ctx.Request.Context(), userId)
if err != nil {
ResponseJSON[any](ctx, gin.H{"id": userId}, nil, err)
return
}
err = c.accountService.DeleteAccount(ctx.Request.Context(), userId)
if err != nil {
ResponseJSON[any](ctx, gin.H{"id": userId}, nil, err)
return
}
ResponseJSON(ctx, gin.H{"id": userId}, gin.H{"message": "User deleted successfully"}, nil)
}
|