Skip to content

Commit

Permalink
Export user repo functions
Browse files Browse the repository at this point in the history
CreateUser
ReadUserByUserId
UpdateUser
ResetUserState
  • Loading branch information
bugfloyd committed May 6, 2024
1 parent 4e6d5e8 commit 2b4926e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bot/common/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *RootHandler) languageCallback(b *gotgbot.Bot, ctx *ext.Context, action
return fmt.Errorf("invalid language code in callback data: %s", cb.Data)
}

err := r.userRepo.updateUser(r.user, map[string]interface{}{
err := r.userRepo.UpdateUser(r.user, map[string]interface{}{
"State": Idle,
"ContactUUID": "",
"ReplyMessageID": 0,
Expand Down
6 changes: 3 additions & 3 deletions bot/common/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *RootHandler) sendAnonymousMessage(b *gotgbot.Bot, ctx *ext.Context) err
}

// Reset sender user
err = r.userRepo.resetUserState(r.user)
err = r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (r *RootHandler) sendAnonymousMessage(b *gotgbot.Bot, ctx *ext.Context) err
}

// Reset sender user
err = r.userRepo.resetUserState(r.user)
err = r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func (r *RootHandler) replyCallback(b *gotgbot.Bot, ctx *ext.Context) error {
}

// Store the message id in the user and set status to replying
err = r.userRepo.updateUser(r.user, map[string]interface{}{
err = r.userRepo.UpdateUser(r.user, map[string]interface{}{
"State": Sending,
"ContactUUID": receiverUUID,
"ReplyMessageID": messageID,
Expand Down
12 changes: 6 additions & 6 deletions bot/common/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

func (r *RootHandler) processUser(userRepo *UserRepository, ctx *ext.Context) (*User, error) {
user, err := userRepo.readUserByUserId(ctx.EffectiveUser.Id)
user, err := userRepo.ReadUserByUserId(ctx.EffectiveUser.Id)
if err != nil {
user, err = userRepo.createUser(ctx.EffectiveUser.Id)
user, err = userRepo.CreateUser(ctx.EffectiveUser.Id)
if err != nil {
return nil, err
}
Expand All @@ -26,7 +26,7 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error {
args := ctx.Args()
if len(args) == 1 && args[0] == "/start" {
// Reset user state
err := r.userRepo.resetUserState(r.user)
err := r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error {
}

// Set user state to sending
err = r.userRepo.updateUser(r.user, map[string]interface{}{
err = r.userRepo.UpdateUser(r.user, map[string]interface{}{
"State": Sending,
"ContactUUID": receiverUser.UUID,
})
Expand All @@ -133,7 +133,7 @@ func (r *RootHandler) info(b *gotgbot.Bot, ctx *ext.Context) error {
if err != nil {
return fmt.Errorf("failed to send bot info: %w", err)
}
err = r.userRepo.resetUserState(r.user)
err = r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *RootHandler) getLink(b *gotgbot.Bot, ctx *ext.Context) error {
if err != nil {
return err
}
err = r.userRepo.resetUserState(r.user)
err = r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion bot/common/root_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r *RootHandler) runCommand(b *gotgbot.Bot, ctx *ext.Context, command inter
case CallbackCommand:
// Reset user state if necessary
if r.user.State != Idle || r.user.ContactUUID != "" || r.user.ReplyMessageID != 0 {
err := r.userRepo.resetUserState(r.user)
err := r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions bot/common/user_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewUserRepository() (*UserRepository, error) {
}, nil
}

func (repo *UserRepository) createUser(userId int64) (*User, error) {
func (repo *UserRepository) CreateUser(userId int64) (*User, error) {
u := User{
UUID: uuid.New().String(),
UserID: userId,
Expand All @@ -62,7 +62,7 @@ func (repo *UserRepository) readUserByUUID(uuid string) (*User, error) {
return &u, nil
}

func (repo *UserRepository) readUserByUserId(userId int64) (*User, error) {
func (repo *UserRepository) ReadUserByUserId(userId int64) (*User, error) {
var u User
err := repo.table.Get("UserID", userId).Index("UserID-GSI").One(&u)
if err != nil {
Expand All @@ -89,7 +89,7 @@ func (repo *UserRepository) readUserByLinkKey(linkKey int32, createdAt int64) (*
return &u, nil
}

func (repo *UserRepository) updateUser(user *User, updates map[string]interface{}) error {
func (repo *UserRepository) UpdateUser(user *User, updates map[string]interface{}) error {
updateBuilder := repo.table.Update("UUID", user.UUID)
for key, value := range updates {
updateBuilder = updateBuilder.Set(key, value)
Expand All @@ -116,8 +116,8 @@ func (repo *UserRepository) updateUser(user *User, updates map[string]interface{
return nil
}

func (repo *UserRepository) resetUserState(user *User) error {
err := repo.updateUser(user, map[string]interface{}{
func (repo *UserRepository) ResetUserState(user *User) error {
err := repo.UpdateUser(user, map[string]interface{}{
"State": Idle,
"ContactUUID": "",
"ReplyMessageID": 0,
Expand Down
8 changes: 4 additions & 4 deletions bot/common/username.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action
return fmt.Errorf("failed to answer callback: %w", err)
}
} else if action == "SET" {
err := r.userRepo.updateUser(r.user, map[string]interface{}{
err := r.userRepo.UpdateUser(r.user, map[string]interface{}{
"State": SettingUsername,
"ContactUUID": "",
"ReplyMessageID": 0,
Expand All @@ -100,7 +100,7 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action
return fmt.Errorf("failed to answer callback: %w", err)
}
} else if action == "REMOVE" {
err := r.userRepo.updateUser(r.user, map[string]interface{}{
err := r.userRepo.UpdateUser(r.user, map[string]interface{}{
"State": Idle,
"Username": "",
"ContactUUID": "",
Expand Down Expand Up @@ -144,7 +144,7 @@ func (r *RootHandler) setUsername(b *gotgbot.Bot, ctx *ext.Context) error {

existingUser, err := r.userRepo.readUserByUsername(username)
if err != nil || existingUser == nil {
err := r.userRepo.updateUser(r.user, map[string]interface{}{
err := r.userRepo.UpdateUser(r.user, map[string]interface{}{
"Username": username,
"State": Idle,
"ContactUUID": "",
Expand All @@ -167,7 +167,7 @@ func (r *RootHandler) setUsername(b *gotgbot.Bot, ctx *ext.Context) error {
text = i18n.T(i18n.SameUsernameText)

// Reset sender user
err = r.userRepo.resetUserState(r.user)
err = r.userRepo.ResetUserState(r.user)
if err != nil {
return err
}
Expand Down

0 comments on commit 2b4926e

Please sign in to comment.