-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuser.go
140 lines (113 loc) · 3.45 KB
/
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package models
import (
"context"
"time"
"github.com/google/uuid"
"github.com/uptrace/bun"
)
type User struct {
bun.BaseModel `bun:"table:users"`
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()" json:"id"`
Name string `bun:"name,unique,notnull" json:"name"`
Email string `bun:"email,unique,notnull" json:"email"`
EncryptedPassword string `bun:"encrypted_password,notnull" json:"encrypted_password"`
CurrentSignInAt time.Time `bun:"current_sign_in_at,type:timestamp" json:"current_sign_in_at"`
LastSignInAt time.Time `bun:"last_sign_in_at,type:timestamp" json:"last_sign_in_at"`
CurrentSignInIP string `bun:"current_sign_in_ip" json:"current_sign_in_ip"`
LastSignInIP string `bun:"last_sign_in_ip" json:"last_sign_in_ip"`
CreatedAt time.Time `bun:"created_at,type:timestamp,notnull" json:"created_at"`
UpdatedAt time.Time `bun:"updated_at,type:timestamp,notnull" json:"updated_at"`
}
type GetUserParams struct {
id uuid.UUID
name *string
email *string
}
func NewGetUserParams() *GetUserParams {
return &GetUserParams{}
}
func (gup *GetUserParams) SetID(id uuid.UUID) *GetUserParams {
gup.id = id
return gup
}
func (gup *GetUserParams) SetName(name string) *GetUserParams {
gup.name = &name
return gup
}
func (gup *GetUserParams) SetEmail(email string) *GetUserParams {
gup.email = &email
return gup
}
type CountUserParams struct {
name *string
email *string
}
func NewCountUserParams() *CountUserParams {
return &CountUserParams{}
}
func (gup *CountUserParams) SetName(name string) *CountUserParams {
gup.name = &name
return gup
}
func (gup *CountUserParams) SetEmail(email string) *CountUserParams {
gup.email = &email
return gup
}
func NewCountUserParam() *CountUserParams {
return &CountUserParams{}
}
type IUserRepo interface {
Get(ctx context.Context, params *GetUserParams) (*User, error)
Count(ctx context.Context, params *CountUserParams) (int, error)
Insert(ctx context.Context, user *User) (*User, error)
GetEPByName(ctx context.Context, name string) (string, error)
}
var _ IUserRepo = (*UserRepo)(nil)
type UserRepo struct {
db bun.IDB
}
func NewUserRepo(db bun.IDB) IUserRepo {
return &UserRepo{db: db}
}
func (userRepo *UserRepo) Get(ctx context.Context, params *GetUserParams) (*User, error) {
user := &User{}
query := userRepo.db.NewSelect().Model(user)
if uuid.Nil != params.id {
query = query.Where("id = ?", params.id)
}
if params.name != nil {
query = query.Where("name = ?", params.name)
}
if params.email != nil {
query = query.Where("email = ?", *params.email)
}
err := query.Limit(1).Scan(ctx)
if err != nil {
return nil, err
}
return user, nil
}
func (userRepo *UserRepo) Count(ctx context.Context, params *CountUserParams) (int, error) {
query := userRepo.db.NewSelect().Model((*User)(nil))
if params.name != nil {
query = query.Where("name = ?", params.name)
}
if params.email != nil {
query = query.Where("email = ?", *params.email)
}
return query.Count(ctx)
}
func (userRepo *UserRepo) Insert(ctx context.Context, user *User) (*User, error) {
_, err := userRepo.db.NewInsert().Model(user).Exec(ctx)
if err != nil {
return nil, err
}
return user, nil
}
func (userRepo *UserRepo) GetEPByName(ctx context.Context, name string) (string, error) {
var ep string
return ep, userRepo.db.NewSelect().
Model((*User)(nil)).Column("encrypted_password").
Where("name = ?", name).
Scan(ctx, &ep)
}