feat(account): use token instead of id for rename
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -83,13 +85,18 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) {
|
|||||||
c.JSON(200, gin.H{"message": "account created"})
|
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
|
var req RenameByIDRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
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()})
|
c.JSON(500, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -163,3 +170,15 @@ func (h *AccountHandler) Logout(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.JSON(200, LogoutResponse{})
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func (ar *AccountRepository) CreateAccount(account *Account) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ar *AccountRepository) RenameByID(id uint, newUsername string) error {
|
func (ar *AccountRepository) Rename(id uint, newUsername string) error {
|
||||||
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
|
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ func (as *AccountService) CreateAccount(account *Account) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *AccountService) RenameByID(id uint, newUsername string) error {
|
func (as *AccountService) Rename(accountID uint, newUsername string) error {
|
||||||
if err := as.accountRepository.RenameByID(id, newUsername); err != nil {
|
if err := as.accountRepository.Rename(accountID, newUsername); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user