Skip to content

Commit

Permalink
fix(middleware): CreateUser middleware func fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mj committed Jun 17, 2024
1 parent 1a33000 commit a97e252
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
13 changes: 7 additions & 6 deletions internal/engine/command/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
)

func (h *MiddlewareHandler) CreateUser(cmd *Command, appID entity.AppID, callerID string, _ ...string) error {
if !h.db.HasUserInApp(appID, callerID) {
user := &entity.User{ApplicationID: appID, CallerID: callerID}
if err := h.db.AddUser(user); err != nil {
return err
}

if user, _ := h.db.GetUserInApp(appID, callerID); user != nil {
cmd.User = user
return nil
}

user := &entity.User{ApplicationID: appID, CallerID: callerID}
if err := h.db.AddUser(user); err != nil {
return err
}
cmd.User = user
return nil
}
31 changes: 11 additions & 20 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ func (db *DB) AddUser(u *entity.User) error {
return nil
}

/*func (db *DB) GetUser(id string) (*entity.User, error) {
var u *entity.User
tx := db.Model(&entity.User{}).Preload("Faucets").First(&u, "id = ?", id)
if tx.Error != nil {
return &entity.User{}, ReadError{
Message: tx.Error.Error(),
}
}
return u, nil
}*/

func (db *DB) HasUser(id string) bool {
var exists bool

Expand All @@ -37,15 +25,18 @@ func (db *DB) HasUser(id string) bool {
return exists
}

func (db *DB) HasUserInApp(appID entity.AppID, callerID string) bool {
var exists bool

_ = db.Model(&entity.User{}).
Select("count(*) > 0").
func (db *DB) GetUserInApp(appID entity.AppID, callerID string) (*entity.User, error) {
var u *entity.User
tx := db.Model(&entity.User{}).
Where("application_id = ?", appID).
Where("caller_id = ?", callerID).
Find(&exists).
Error
First(&u)

return exists
if tx.Error != nil {
return nil, ReadError{
Message: tx.Error.Error(),
}
}

return u, nil
}

0 comments on commit a97e252

Please sign in to comment.