/
store.go
68 lines (53 loc) · 1.4 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package mysql
import (
"errors"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type Store struct {
DB *gorm.DB
}
func NewStore(db *gorm.DB) *Store {
return &Store{db}
}
func (store *Store) Close() error {
if db, err := store.DB.DB(); err != nil {
return err
} else {
return db.Close()
}
}
func (store *Store) Exists(modelPtr interface{}, whereQuery string, args ...interface{}) (bool, error) {
err := store.DB.Where(whereQuery, args...).First(modelPtr).Error
if err == nil {
return true, nil
}
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
func (store *Store) GetById(modelPtr interface{}, id uint64) (bool, error) {
return store.Exists(modelPtr, "id = ?", id)
}
func (store *Store) List(db *gorm.DB, idDesc bool, offset, limit int, slicePtr interface{}) (total int64, err error) {
var orderBy string
if idDesc {
orderBy = "id DESC"
} else {
orderBy = "id ASC"
}
return store.ListByOrder(db, orderBy, offset, limit, slicePtr)
}
func (*Store) ListByOrder(db *gorm.DB, orderBy string, offset, limit int, slicePtr interface{}) (total int64, err error) {
if err = db.Count(&total).Error; err != nil {
return 0, err
}
if !logrus.IsLevelEnabled(logrus.TraceLevel) && (total <= int64(offset)) {
return total, nil
}
if err = db.Order(orderBy).Offset(offset).Limit(limit).Find(slicePtr).Error; err != nil {
return 0, err
}
return total, nil
}