refactor(account): remove unused structs and logout by token

This commit is contained in:
Leon
2025-12-09 15:58:07 +08:00
parent f122a6170d
commit 4bedf58a03
2 changed files with 8 additions and 26 deletions

View File

@@ -15,17 +15,10 @@ type CreateAccountRequest struct {
Password string `json:"password"` Password string `json:"password"`
} }
type CreateAccountResponse struct { type RenameRequest struct {
}
type RenameByIDRequest struct {
ID uint `json:"id"`
NewUsername string `json:"new_username"` NewUsername string `json:"new_username"`
} }
type RenameByIDResponse struct {
}
type FindByIDRequest struct { type FindByIDRequest struct {
ID uint `json:"id"` ID uint `json:"id"`
} }
@@ -50,21 +43,10 @@ type ChangePasswordRequest struct {
NewPassword string `json:"new_password"` NewPassword string `json:"new_password"`
} }
type ChangePasswordResponse struct {
}
type LoginRequest struct { type LoginRequest struct {
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` 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 { func NewAccountHandler(accountService *AccountService) *AccountHandler {
return &AccountHandler{accountService: accountService} return &AccountHandler{accountService: accountService}
@@ -86,7 +68,7 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) {
} }
func (h *AccountHandler) Rename(c *gin.Context) { func (h *AccountHandler) Rename(c *gin.Context) {
var req RenameByIDRequest var req RenameRequest
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
@@ -154,21 +136,21 @@ func (h *AccountHandler) Login(c *gin.Context) {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} else { } else {
c.JSON(200, LoginResponse{Token: token}) c.JSON(200, gin.H{"token": token})
} }
} }
func (h *AccountHandler) Logout(c *gin.Context) { func (h *AccountHandler) Logout(c *gin.Context) {
var req LogoutRequest accountID, err := getAccountID(c)
if err := c.ShouldBindJSON(&req); err != nil { if err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return 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()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} }
c.JSON(200, LogoutResponse{}) c.JSON(200, gin.H{"message": "account logged out"})
} }
func getAccountID(c *gin.Context) (uint, error) { func getAccountID(c *gin.Context) (uint, error) {

View File

@@ -28,7 +28,7 @@ func SetRouter(db *gorm.DB) *gin.Engine {
protectedAccountGroup.Use(middleware.JWTAuth(accountRepository)) protectedAccountGroup.Use(middleware.JWTAuth(accountRepository))
{ {
protectedAccountGroup.POST("/logout", accountHandler.Logout) protectedAccountGroup.POST("/logout", accountHandler.Logout)
protectedAccountGroup.POST("/rename", accountHandler.RenameByID) protectedAccountGroup.POST("/rename", accountHandler.Rename)
} }
// video // video
videoRepository := video.NewVideoRepository(db) videoRepository := video.NewVideoRepository(db)