Skip to content

Commit 698a8d8

Browse files
committed
fix: handle count data
1 parent 0bd72fb commit 698a8d8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

sql/common.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package sql
22

3-
import "gorm.io/gorm"
3+
import (
4+
"github.com/ochom/gutils/logs"
5+
"gorm.io/gorm"
6+
)
47

58
// Create ...
69
func Create[T any](data *T) error {
@@ -43,9 +46,14 @@ func FindWithLimit[T any](query *T, page, limit int, scopes ...func(*gorm.DB) *g
4346
}
4447

4548
// Count ...
46-
func Count[T any](query *T, scopes ...func(*gorm.DB) *gorm.DB) (int64, error) {
49+
func Count[T any](query *T, scopes ...func(*gorm.DB) *gorm.DB) int {
4750
var count int64
4851
var model T
4952
err := conn.Model(&model).Scopes(scopes...).Where(query).Count(&count).Error
50-
return count, err
53+
if err != nil {
54+
logs.Error("Failed to count: %s", err.Error())
55+
return 0
56+
}
57+
58+
return int(count)
5159
}

0 commit comments

Comments
 (0)