feat(account): use token instead of id for rename

This commit is contained in:
Leon
2025-12-09 15:47:55 +08:00
parent 6c0b8c7d7a
commit f122a6170d
3 changed files with 24 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
package account
import (
"errors"
"github.com/gin-gonic/gin"
)
@@ -83,13 +85,18 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) {
c.JSON(200, gin.H{"message": "account created"})
}
func (h *AccountHandler) RenameByID(c *gin.Context) {
func (h *AccountHandler) Rename(c *gin.Context) {
var req RenameByIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.accountService.RenameByID(req.ID, req.NewUsername); err != nil {
accountID, err := getAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := h.accountService.Rename(accountID, req.NewUsername); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
@@ -163,3 +170,15 @@ func (h *AccountHandler) Logout(c *gin.Context) {
}
c.JSON(200, LogoutResponse{})
}
func getAccountID(c *gin.Context) (uint, error) {
value, exists := c.Get("accountID")
if !exists {
return 0, errors.New("accountID not found")
}
id, ok := value.(uint)
if !ok {
return 0, errors.New("accountID has invalid type")
}
return id, nil
}