-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathrepository.go
More file actions
88 lines (70 loc) · 2.7 KB
/
Copy pathrepository.go
File metadata and controls
88 lines (70 loc) · 2.7 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package accountrepo
import (
"context"
"github.com/TremblingV5/box/dbtx"
"github.com/cloudzenith/DouTok/backend/baseService/internal/infrastructure/dal/query"
"gorm.io/gen"
"gorm.io/gen/field"
"github.com/cloudzenith/DouTok/backend/baseService/internal/infrastructure/dal/models"
)
type PersistRepository struct{}
func New() *PersistRepository {
return &PersistRepository{}
}
func (r *PersistRepository) getFirst(ctx context.Context, conditions ...gen.Condition) (*models.Account, error) {
conditions = append(conditions, query.Q.Account.IsDeleted.Is(false))
account, err := query.Q.WithContext(ctx).Account.Where(
conditions...,
).First()
if err != nil {
return nil, err
}
return account, nil
}
func (r *PersistRepository) Create(ctx context.Context, account *models.Account) error {
return query.Q.WithContext(ctx).Account.Create(account)
}
func (r *PersistRepository) ModifyPassword(ctx context.Context, account *models.Account) error {
_, err := query.Q.WithContext(ctx).Account.UpdateColumns(account)
return err
}
func (r *PersistRepository) GetById(ctx context.Context, id int64) (*models.Account, error) {
return r.getFirst(ctx, query.Q.Account.ID.Eq(id))
}
func (r *PersistRepository) GetByMobile(ctx context.Context, mobile string) (*models.Account, error) {
return r.getFirst(ctx, query.Q.Account.Mobile.Eq(mobile))
}
func (r *PersistRepository) GetByEmail(ctx context.Context, email string) (*models.Account, error) {
return r.getFirst(ctx, query.Q.Account.Email.Eq(email))
}
func (r *PersistRepository) count(ctx context.Context, conditions ...gen.Condition) (int64, error) {
conditions = append(conditions, query.Q.Account.IsDeleted.Is(false))
return query.Q.WithContext(ctx).Account.Where(
conditions...,
).Count()
}
func (r *PersistRepository) isExist(ctx context.Context, conditions ...gen.Condition) (bool, error) {
count, err := r.count(ctx, conditions...)
if err != nil {
return false, err
}
return count > 0, nil
}
func (r *PersistRepository) IsMobileExist(ctx context.Context, mobile string) (bool, error) {
return r.isExist(ctx, query.Q.Account.Mobile.Eq(mobile))
}
func (r *PersistRepository) IsEmailExist(ctx context.Context, email string) (bool, error) {
return r.isExist(ctx, query.Q.Account.Email.Eq(email))
}
func (r *PersistRepository) ClearColumn(ctx context.Context, column field.Expr) error {
return dbtx.TxDo(ctx, func(tx *query.QueryTx) error {
_, err := tx.WithContext(ctx).Account.Update(column, "")
return err
})
}
func (r *PersistRepository) UpdateColumn(ctx context.Context, column field.Expr, voucher interface{}) error {
return dbtx.TxDo(ctx, func(tx *query.QueryTx) error {
_, err := tx.WithContext(ctx).Account.Update(column, voucher)
return err
})
}