diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-26 22:26:10 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-26 22:40:16 +0200 |
commit | 4d930c0c8cb585979798fac2bb254f991faa62fb (patch) | |
tree | 0c33e0e0f2a2f47b0f64843f7d9a3eb299abb260 /pkg/database/sql | |
parent | d4e1ca3a48e74573df6965ceee217e119ff899ae (diff) | |
download | lens-4d930c0c8cb585979798fac2bb254f991faa62fb.tar.gz lens-4d930c0c8cb585979798fac2bb254f991faa62fb.tar.bz2 lens-4d930c0c8cb585979798fac2bb254f991faa62fb.zip |
feat: Add initial user setup
Diffstat (limited to 'pkg/database/sql')
-rw-r--r-- | pkg/database/sql/user.go | 15 | ||||
-rw-r--r-- | pkg/database/sql/user_test.go | 20 |
2 files changed, 25 insertions, 10 deletions
diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index 2d74162..a02b67b 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -187,3 +187,18 @@ func (self *UserRepository) Delete(ctx context.Context, id uint) error { } return nil } + +func (u *UserRepository) Any(ctx context.Context) (bool, error) { + var exists bool + result := u.db. + WithContext(ctx). + Model(&User{}). + Select("count(id) > 0"). + Find(&exists) + + if result.Error != nil { + return false, result.Error + } + + return exists, nil +} diff --git a/pkg/database/sql/user_test.go b/pkg/database/sql/user_test.go index 875b8e6..473ce03 100644 --- a/pkg/database/sql/user_test.go +++ b/pkg/database/sql/user_test.go @@ -12,7 +12,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" - "git.sr.ht/~gabrielgio/img/pkg/components/auth" + "git.sr.ht/~gabrielgio/img/pkg/components/user" ) func setup(t *testing.T) (*gorm.DB, func()) { @@ -48,7 +48,7 @@ func TestCreate(t *testing.T) { repository := NewUserRepository(db) - id, err := repository.Create(context.Background(), &auth.CreateUser{ + err := repository.Create(context.Background(), &user.CreateUser{ Username: "new_username", Name: "new_name", }) @@ -56,12 +56,12 @@ func TestCreate(t *testing.T) { t.Fatalf("Error creating: %s", err.Error()) } - got, err := repository.Get(context.Background(), id) + got, err := repository.Get(context.Background(), 1) if err != nil { t.Fatalf("Error getting: %s", err.Error()) } - want := &auth.User{ - ID: id, + want := &user.User{ + ID: 1, Username: "new_username", Name: "new_name", } @@ -78,7 +78,7 @@ func TestUpdate(t *testing.T) { repository := NewUserRepository(db) - id, err := repository.Create(context.Background(), &auth.CreateUser{ + err := repository.Create(context.Background(), &user.CreateUser{ Username: "username", Name: "name", }) @@ -86,7 +86,7 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error creating user: %s", err.Error()) } - err = repository.Update(context.Background(), id, &auth.UpdateUser{ + err = repository.Update(context.Background(), 1, &user.UpdateUser{ Username: "new_username", Name: "new_name", }) @@ -94,12 +94,12 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error update user: %s", err.Error()) } - got, err := repository.Get(context.Background(), id) + got, err := repository.Get(context.Background(), 1) if err != nil { t.Fatalf("Error getting user: %s", err.Error()) } - want := &auth.User{ - ID: id, + want := &user.User{ + ID: 1, Username: "new_username", Name: "new_name", } |