aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/sql
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/database/sql')
-rw-r--r--pkg/database/sql/user.go15
-rw-r--r--pkg/database/sql/user_test.go20
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",
}