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"`
}
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) {

View File

@@ -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)