-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_repository.go
108 lines (89 loc) · 4.99 KB
/
user_repository.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
package helpers
import (
"github.com/jmoiron/sqlx"
)
type UserRepository interface {
FindById(id FKInt) (User, error)
FindByAuthToken(token string) (User, error)
FindByDisplayName(accountCode, displayName string) (User, error)
FindByLogin(accountCode, login string) (User, error)
FindByEmail(accountCode, email string) (User, error)
}
type WorkerbotRepository interface {
FindWorkerbotByAccount(accountCode string) (User, error)
}
func NewUserRepository(db *sqlx.DB) UserRepository {
return &relationalUserRepository{db}
}
func NewWorkerbotRepository(db *sqlx.DB) WorkerbotRepository {
return &relationalUserRepository{db}
}
type relationalUserRepository struct {
DB *sqlx.DB
}
func (r *relationalUserRepository) FindById(id FKInt) (User, error) {
var sql string
if r.DB.DriverName() == "postgres" {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.id=$1"
} else {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.id=?"
}
user := User{}
err := r.DB.Get(&user, sql, id)
return user, err
}
func (r *relationalUserRepository) FindByAuthToken(token string) (User, error) {
var sql string
if r.DB.DriverName() == "postgres" {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.authentication_token=$1"
} else {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.authentication_token=?"
}
user := User{}
err := r.DB.Get(&user, sql, token)
return user, err
}
func (r *relationalUserRepository) FindByDisplayName(accountCode, name string) (User, error) {
var sql string
if r.DB.DriverName() == "postgres" {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.display_name=$1 and a.nickname=$2"
} else {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.display_name=? and a.nickname=?"
}
user := User{}
err := r.DB.Get(&user, sql, name, accountCode)
return user, err
}
func (r *relationalUserRepository) FindWorkerbotByAccount(accountCode string) (User, error) {
var sql string
if r.DB.DriverName() == "postgres" {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.is_bot=1 and a.nickname=$1"
} else {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.is_bot=1 and a.nickname=?"
}
user := User{}
err := r.DB.Get(&user, sql, accountCode)
return user, err
}
func (r *relationalUserRepository) FindByLogin(accountCode, login string) (User, error) {
var sql string
if r.DB.DriverName() == "postgres" {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.login=$1 and a.nickname=$2"
} else {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.login=? and a.nickname=?"
}
user := User{}
err := r.DB.Get(&user, sql, login, accountCode)
return user, err
}
func (r *relationalUserRepository) FindByEmail(accountCode, email string) (User, error) {
var sql string
if r.DB.DriverName() == "postgres" {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.email=$1 and a.nickname=$2"
} else {
sql = "select u.id, u.account_id, u.is_account_admin, a.nickname, email, display_name, login, u.created_at, u.updated_at, u.authentication_token from case_blocks_users u, case_blocks_accounts a where u.account_id=a.id and u.email=? and a.nickname=?"
}
user := User{}
err := r.DB.Get(&user, sql, email, accountCode)
return user, err
}