-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
60 lines (46 loc) · 1.29 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
package sdump
import (
"context"
"time"
"github.com/google/uuid"
"github.com/uptrace/bun"
)
const (
ErrPlanNotFound = appError("plan does not exists")
ErrUserNotFound = appError("user not found")
ErrCounterExhausted = appError("no more units left")
)
type Counter int64
func (c *Counter) Add() {
(*c)++
}
func (c *Counter) Take() error {
if *c <= 0 {
return ErrCounterExhausted
}
(*c)--
return nil
}
func (c *Counter) TakeN(n int64) error {
if *c <= 0 {
return c.Take()
}
(*c) -= Counter(n)
return nil
}
type User struct {
ID uuid.UUID `bun:"type:uuid,default:uuid_generate_v4()" json:"id,omitempty"`
SSHFingerPrint string `json:"ssh_finger_print,omitempty"`
IsBanned bool `json:"is_banned,omitempty"`
CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created_at,omitempty" bson:"created_at"`
UpdatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty" bson:"updated_at"`
DeletedAt *time.Time `bun:",soft_delete,nullzero" json:"-,omitempty" bson:"deleted_at"`
bun.BaseModel `bun:"table:users"`
}
type FindUserOptions struct {
SSHKeyFingerprint string
}
type UserRepository interface {
Create(context.Context, *User) error
Find(context.Context, *FindUserOptions) (*User, error)
}