Files
quyun/backend_v1/app/http/users.go
Rogee 7b18a6a0e6
Some checks failed
build quyun / Build (push) Failing after 2m1s
feat: Enhance Swagger documentation with new admin and user endpoints
- Added definitions for admin authentication, media, posts, orders, and user management.
- Implemented new API paths for admin functionalities including authentication, media management, order processing, and user statistics.
- Updated existing endpoints to improve clarity and structure in the Swagger documentation.
- Introduced new response schemas for various operations to standardize API responses.
2025-12-19 23:43:46 +08:00

72 lines
1.7 KiB
Go

package http
import (
"strings"
"time"
"quyun/v2/app/services"
"quyun/v2/database/models"
"github.com/gofiber/fiber/v3"
)
// @provider
type users struct{}
type UserInfo struct {
ID int64 `json:"id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Username string `json:"username,omitempty"`
Avatar string `json:"avatar,omitempty"`
Balance int64 `json:"balance"`
}
// Profile 获取当前登录用户的基础信息。
//
// @Summary 获取个人信息
// @Tags Users
// @Produce json
// @Success 200 {object} UserInfo "成功"
// @Router /users/profile [get]
// @Bind user local
func (ctl *users) Profile(ctx fiber.Ctx, user *models.User) (*UserInfo, error) {
return &UserInfo{
ID: user.ID,
CreatedAt: user.CreatedAt,
Username: user.Username,
Avatar: user.Avatar,
Balance: user.Balance,
}, nil
}
type ProfileForm struct {
Username string `json:"username" validate:"required"`
}
// Update 修改当前登录用户的用户名。
//
// @Summary 修改用户名
// @Tags Users
// @Accept json
// @Produce json
// @Param form body ProfileForm true "请求体"
// @Success 200 {object} any "成功"
// @Router /users/username [put]
// @Bind user local
// @Bind form body
func (ctl *users) Update(ctx fiber.Ctx, user *models.User, form *ProfileForm) error {
username := strings.TrimSpace(form.Username)
if len([]rune(username)) > 12 {
return fiber.NewError(fiber.StatusBadRequest, "Username exceeds maximum length of 12 characters")
}
if username == "" {
return fiber.NewError(fiber.StatusBadRequest, "Username cannot be empty")
}
if err := services.Users.SetUsername(ctx, user.ID, username); err != nil {
return err
}
return nil
}