From 31fd17c34db715f489e523c5cfb576c22d5d2e30 Mon Sep 17 00:00:00 2001 From: slhmy Date: Thu, 25 Jul 2024 22:30:45 +0800 Subject: [PATCH] Remove unnecessary code --- cmd/web_server/handler/user.go | 72 ---------------------------------- models/user/user.go | 1 - models/user/user_db.go | 10 +---- 3 files changed, 2 insertions(+), 81 deletions(-) diff --git a/cmd/web_server/handler/user.go b/cmd/web_server/handler/user.go index 2e15123..8cd852a 100644 --- a/cmd/web_server/handler/user.go +++ b/cmd/web_server/handler/user.go @@ -16,15 +16,12 @@ import ( func SetupUserRouter(baseRoute *gin.RouterGroup) { g := baseRoute.Group("/user") { - g.PUT("", updateUser) g.GET("", middleware.HandleRequireLogin, middleware.BuildCasbinEnforceHandlerWithDomain("system"), GetUserList, ) - g.POST("/login", login) g.GET("/me", middleware.HandleRequireLogin, me) - g.GET("/check-exist", checkUserExist) } } @@ -66,38 +63,6 @@ func GetUserList(ginCtx *gin.Context) { }) } -type loginBody struct { - Account string `json:"account" example:"admin"` - Password string `json:"password" example:"admin"` -} - -// Login -// -// @Summary Login by account and password -// @Description A Cookie will be set if login successfully -// @Tags user -// @Accept json -// @Param loginBody body loginBody true "body" -// @Router /user/login [post] -// @Success 200 -func login(ginCtx *gin.Context) { - body := &loginBody{} - err := ginCtx.BindJSON(body) - if err != nil { - modules.NewInvalidParamError("body", "invalid body").AppendToGin(ginCtx) - return - } - - ls, err := user_service.StartLoginSession(ginCtx, body.Account, body.Password) - if err != nil { - modules.NewInternalError(fmt.Sprintf("failed to login: %v", err)).AppendToGin(ginCtx) - return - } - middleware.SetLoginSessionKeyCookie(ginCtx, ls.Key) - - ginCtx.Status(http.StatusOK) -} - // Me // // @Summary Get current user @@ -120,40 +85,3 @@ func me(ginCtx *gin.Context) { ginCtx.JSON(http.StatusOK, user) } - -func checkUserExist(ginCtx *gin.Context) { - account := ginCtx.Query("account") - if account == "" { - modules.NewInvalidParamError("account", "account cannot be empty").AppendToGin(ginCtx) - return - } - - exist, err := user_service.CheckUserExist(ginCtx, account) - if err != nil { - modules.NewInternalError(fmt.Sprintf("failed to check user exist: %v", err)).AppendToGin(ginCtx) - return - } - - ginCtx.JSON(http.StatusOK, gin.H{ - "exist": exist, - }) -} - -type updateUserBody struct { - User user_model.User `json:"user"` -} - -func updateUser(ginCtx *gin.Context) { - body := &updateUserBody{} - err := ginCtx.BindJSON(body) - if err != nil { - modules.NewInvalidParamError("body", err.Error()).AppendToGin(ginCtx) - return - } - - err = user_service.UpdateUser(ginCtx, body.User) - if err != nil { - modules.NewInternalError(fmt.Sprintf("failed to update user: %v", err)).AppendToGin(ginCtx) - return - } -} diff --git a/models/user/user.go b/models/user/user.go index 4c76955..66c8149 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -9,7 +9,6 @@ type User struct { Password *string `json:"password,omitempty" gorm:"-:all"` HashedPassword string `json:"-" gorm:"not null"` Email *string `json:"email,omitempty" gorm:"unique"` - Mobile *string `json:"mobile,omitempty" gorm:"unique"` } var PublicUserSelection = append([]string{"account", "name"}, models.MetaFieldsSelection...) diff --git a/models/user/user_db.go b/models/user/user_db.go index 17900d0..1fe8d1c 100644 --- a/models/user/user_db.go +++ b/models/user/user_db.go @@ -36,7 +36,8 @@ func GetUser(tx *gorm.DB, account string) (*User, error) { func GetPublicUser(tx *gorm.DB, account string) (*User, error) { db_user := User{} - err := tx.Model(&User{}).Select(PublicUserSelection).Where("account = ?", account).First(&db_user).Error + err := tx.Model(&User{}).Select(PublicUserSelection). + Where("account = ?", account).First(&db_user).Error if err != nil { return nil, err } @@ -74,7 +75,6 @@ func UpdateUser(tx *gorm.DB, update User) error { type GetUserOptions struct { AccountQuery string EmailQuery string - MobileQuery string Offset *int Limit *int } @@ -92,9 +92,6 @@ func CountUserByOptions(tx *gorm.DB, options GetUserOptions) (int64, error) { if options.EmailQuery != "" { tx = tx.Where("email LIKE ?", options.EmailQuery) } - if options.MobileQuery != "" { - tx = tx.Where("mobile LIKE ?", options.MobileQuery) - } err := tx.Count(&count).Error @@ -115,9 +112,6 @@ func GetUserByOptions(tx *gorm.DB, options GetUserOptions) ([]User, int64, error if options.EmailQuery != "" { tx = tx.Where("email LIKE ?", options.EmailQuery) } - if options.MobileQuery != "" { - tx = tx.Where("mobile LIKE ?", options.MobileQuery) - } if options.Offset != nil { tx = tx.Offset(*options.Offset)