Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add debug line for user query #224

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,21 @@ func (be *BotEngine) NetworkStatus() (*network.NetStatus, error) {
}

func (be *BotEngine) GetUser(appID entity.PlatformID, platformUserID string) (*entity.User, error) {
if u, _ := be.db.GetUserByApp(appID, platformUserID); u != nil {
return u, nil
existingUser, _ := be.db.GetUserByPlatformID(appID, platformUserID)
if existingUser != nil {
return existingUser, nil
}

user := &entity.User{PlatformID: appID, PlatformUserID: platformUserID}
if err := be.db.AddUser(user); err != nil {
newUser := &entity.User{
PlatformID: appID,
PlatformUserID: platformUserID,
Role: entity.BasicUser,
}
if err := be.db.AddUser(newUser); err != nil {
return nil, err
}

return user, nil
return newUser, nil
}

func (be *BotEngine) Stop() {
Expand Down
8 changes: 4 additions & 4 deletions internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository
import "github.com/pagu-project/pagu/internal/entity"

func (db *Database) AddUser(u *entity.User) error {
tx := db.gormDB.Create(u)
tx := db.gormDB.Debug().Create(u)
if tx.Error != nil {
return WriteError{
Message: tx.Error.Error(),
Expand All @@ -13,7 +13,7 @@ func (db *Database) AddUser(u *entity.User) error {
return nil
}

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

_ = db.gormDB.Model(&entity.User{}).
Expand All @@ -25,9 +25,9 @@ func (db *Database) HasUser(id string) bool {
return exists
}

func (db *Database) GetUserByApp(appID entity.PlatformID, callerID string) (*entity.User, error) {
func (db *Database) GetUserByPlatformID(appID entity.PlatformID, callerID string) (*entity.User, error) {
var user *entity.User
tx := db.gormDB.Model(&entity.User{}).
tx := db.gormDB.Debug().Model(&entity.User{}).
Where("platform_id = ?", appID).
Where("platform_user_id = ?", callerID).
First(&user)
Expand Down
32 changes: 32 additions & 0 deletions internal/repository/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package repository

import (
"testing"

"github.com/pagu-project/pagu/internal/entity"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetUserByID(t *testing.T) {
db, err := NewDB("sqlite:file::memory:")
require.NoError(t, err)

user := entity.User{
PlatformID: entity.PlatformIDDiscord,
PlatformUserID: "1234",
Role: entity.BasicUser,
}
err = db.AddUser(&user)
assert.NoError(t, err)

foundUser, err := db.GetUserByPlatformID(user.PlatformID, user.PlatformUserID)
require.NoError(t, err)

assert.Equal(t, foundUser.PlatformID, user.PlatformID)
assert.Equal(t, foundUser.PlatformUserID, user.PlatformUserID)
assert.Equal(t, foundUser.Role, user.Role)

hasUser := db.HasUser(foundUser.DBModel.ID)
assert.True(t, hasUser)
}
Loading