/
user.go
36 lines (29 loc) · 888 Bytes
/
user.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
package data
import (
"context"
"github.com/liushuangls/go-server-template/internal/data/ent"
"github.com/liushuangls/go-server-template/internal/data/ent/predicate"
userSchema "github.com/liushuangls/go-server-template/internal/data/ent/user"
)
type UserRepo struct {
*Data
}
func NewUserRepo(data *Data) *UserRepo {
return &UserRepo{data}
}
func (u *UserRepo) findOne(ctx context.Context, wheres ...predicate.User) (*ent.User, error) {
user, err := u.db.User.Query().
Where(wheres...).
Where(userSchema.DeleteTimeIsNil()).
First(ctx)
if err != nil {
return nil, u.warpError(err)
}
return user, nil
}
func (u *UserRepo) FindByEmail(ctx context.Context, email string) (*ent.User, error) {
return u.findOne(ctx, userSchema.Email(email))
}
func (u *UserRepo) FindByID(ctx context.Context, uid int) (*ent.User, error) {
return u.findOne(ctx, userSchema.ID(uid))
}