Skip to content

Commit

Permalink
Remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Jul 25, 2024
1 parent 38b5411 commit 31fd17c
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 81 deletions.
72 changes: 0 additions & 72 deletions cmd/web_server/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
1 change: 0 additions & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
10 changes: 2 additions & 8 deletions models/user/user_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 31fd17c

Please sign in to comment.