From 4bedf58a039a07682553b9455fe6aaea14a97792 Mon Sep 17 00:00:00 2001 From: Leon <147289645+LeoninCS@users.noreply.github.com> Date: Tue, 9 Dec 2025 15:58:07 +0800 Subject: [PATCH] refactor(account): remove unused structs and logout by token --- internal/account/handler.go | 32 +++++++------------------------- internal/http/router.go | 2 +- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/internal/account/handler.go b/internal/account/handler.go index 1977572..e29173b 100644 --- a/internal/account/handler.go +++ b/internal/account/handler.go @@ -15,17 +15,10 @@ type CreateAccountRequest struct { Password string `json:"password"` } -type CreateAccountResponse struct { -} - -type RenameByIDRequest struct { - ID uint `json:"id"` +type RenameRequest struct { NewUsername string `json:"new_username"` } -type RenameByIDResponse struct { -} - type FindByIDRequest struct { ID uint `json:"id"` } @@ -50,21 +43,10 @@ type ChangePasswordRequest struct { NewPassword string `json:"new_password"` } -type ChangePasswordResponse struct { -} - type LoginRequest struct { Username string `json:"username"` Password string `json:"password"` } -type LoginResponse struct { - Token string `json:"token"` -} -type LogoutRequest struct { - ID uint `json:"id"` -} -type LogoutResponse struct { -} func NewAccountHandler(accountService *AccountService) *AccountHandler { return &AccountHandler{accountService: accountService} @@ -86,7 +68,7 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) { } func (h *AccountHandler) Rename(c *gin.Context) { - var req RenameByIDRequest + var req RenameRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(400, gin.H{"error": err.Error()}) return @@ -154,21 +136,21 @@ func (h *AccountHandler) Login(c *gin.Context) { c.JSON(500, gin.H{"error": err.Error()}) return } else { - c.JSON(200, LoginResponse{Token: token}) + c.JSON(200, gin.H{"token": token}) } } func (h *AccountHandler) Logout(c *gin.Context) { - var req LogoutRequest - if err := c.ShouldBindJSON(&req); err != nil { + accountID, err := getAccountID(c) + if err != nil { c.JSON(400, gin.H{"error": err.Error()}) return } - if err := h.accountService.Logout(req.ID); err != nil { + if err := h.accountService.Logout(accountID); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } - c.JSON(200, LogoutResponse{}) + c.JSON(200, gin.H{"message": "account logged out"}) } func getAccountID(c *gin.Context) (uint, error) { diff --git a/internal/http/router.go b/internal/http/router.go index 5d1c04c..1f1cd55 100644 --- a/internal/http/router.go +++ b/internal/http/router.go @@ -28,7 +28,7 @@ func SetRouter(db *gorm.DB) *gin.Engine { protectedAccountGroup.Use(middleware.JWTAuth(accountRepository)) { protectedAccountGroup.POST("/logout", accountHandler.Logout) - protectedAccountGroup.POST("/rename", accountHandler.RenameByID) + protectedAccountGroup.POST("/rename", accountHandler.Rename) } // video videoRepository := video.NewVideoRepository(db)