Files
dev-service/pkg/api/handlers/user.go
itzaname fecfc520a8
Some checks failed
continuous-integration/drone/push Build is failing
Update package path
2025-06-22 12:43:19 -04:00

78 lines
1.9 KiB
Go

package handlers
import (
"git.itzana.me/StrafesNET/dev-service/pkg/api/dto"
"git.itzana.me/StrafesNET/dev-service/pkg/datastore"
"github.com/gin-gonic/gin"
"net/http"
)
// UserHandler handles HTTP requests related to users.
type UserHandler struct {
*Handler
}
// NewUserHandler creates a new UserHandler with the provided options.
// It requires both a datastore and an authentication service to function properly.
func NewUserHandler(options ...HandlerOption) (*UserHandler, error) {
baseHandler, err := NewHandler(options...)
if err != nil {
return nil, err
}
return &UserHandler{
Handler: baseHandler,
}, nil
}
// ListAll returns a paginated list of all users
func (h *UserHandler) ListAll(ctx *gin.Context) {
pagination := &datastore.CursorPagination{
Cursor: ctx.Query("cursor"),
Limit: DefaultPageLimit,
}
users, err := h.Store.GetAllUsers(ctx, pagination)
if err != nil {
h.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
return
}
h.RespondWithData(ctx, users, pagination)
}
// GetCurrentUser returns the currently logged-in user
func (h *UserHandler) GetCurrentUser(ctx *gin.Context) {
user, valid := h.Auth.GetUserFromContext(ctx)
if !valid {
h.RespondWithError(ctx, http.StatusUnauthorized, ErrMsgUnauth)
return
}
profile, valid := h.Auth.GetAuthProfileFromContext(ctx)
if !valid {
h.RespondWithError(ctx, http.StatusUnauthorized, ErrMsgUnauth)
return
}
h.RespondWithData(ctx, &dto.UserInfo{
ID: user.ID,
Username: user.Username,
AvatarURL: profile.AvatarURL,
RateLimit: user.RateLimit,
Permissions: user.Permissions,
}, nil)
}
// GetCurrentRoles returns the roles for the logged-in user
func (h *UserHandler) GetCurrentRoles(ctx *gin.Context) {
roles, valid := h.Auth.GetRolesFromContext(ctx)
if !valid {
h.RespondWithError(ctx, http.StatusUnauthorized, ErrMsgUnauth)
return
}
h.RespondWithData(ctx, roles, nil)
}