-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
186 lines (165 loc) · 5.02 KB
/
init.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package repository
import (
"database/sql"
"time"
"github.com/artem-malko/auth-and-go/infrastructure/caller"
"github.com/artem-malko/auth-and-go/infrastructure/database"
sq "github.com/Masterminds/squirrel"
"github.com/artem-malko/auth-and-go/infrastructure/convert"
"github.com/artem-malko/auth-and-go/models"
"github.com/artem-malko/auth-and-go/repositories/account"
"github.com/google/uuid"
"gopkg.in/guregu/null.v3"
)
const accountsTableName = "accounts"
const repositoryPrefix = accountsTableName + " repository: "
const idFieldName = "id"
const accountTypeFieldName = "account_type"
const accountStatusFieldName = "account_status"
const accountNameFieldName = "account_name"
const profileFieldName = "profile"
const settingsFieldName = "settings"
const lastIPFieldName = "last_ip"
const lastLoginFieldName = "last_login"
const createdAtFieldName = "created_at"
const updatedAtFieldName = "updated_at"
const accountNameUniqIndexName = "accounts_account_name_active_account_uniq_idx"
type dbProfile struct {
FirstName null.String `json:"first_name"`
LastName null.String `json:"last_name"`
Country null.String `json:"country"`
City null.String `json:"city"`
Gender null.String `json:"gender"`
Description null.String `json:"description"`
Birthday null.Time `json:"birthday"`
SocialLinks []models.SocialLink `json:"social_links"`
Interests []string `json:"interests"`
}
type dbEmailNotification struct {
Email string `json:"email"`
Scope []models.EmailNotificationScopeType `json:"scope"`
}
type dbNotifications struct {
Email dbEmailNotification `json:"email"`
}
type dbSettings struct {
Notifications dbNotifications `json:"notifications"`
}
// User for DB
type dbAccount struct {
ID uuid.UUID
AccountStatus models.AccountStatus
AccountType models.AccountType
AccountName string
Profile dbProfile
LastIP string
LastLogin time.Time
Settings dbSettings
CreatedAt time.Time
UpdatedAt time.Time
}
func (a *dbAccount) GetBaseAccount() models.Account {
return models.Account{
ID: a.ID,
AccountStatus: a.AccountStatus,
AccountType: a.AccountType,
AccountName: a.AccountName,
Profile: models.Profile{
FirstName: a.Profile.FirstName.String,
LastName: a.Profile.LastName.String,
Country: a.Profile.Country.String,
City: a.Profile.City.String,
Gender: a.Profile.Gender.String,
Description: a.Profile.Description.String,
Birthday: a.Profile.Birthday.Time,
SocialLinks: a.Profile.SocialLinks,
Interests: a.Profile.Interests,
},
Settings: models.Settings{
Notifications: models.Notifications{
Email: models.EmailNotifications{
Email: a.Settings.Notifications.Email.Email,
Scope: a.Settings.Notifications.Email.Scope,
},
},
},
LastIP: a.LastIP,
LastLogin: a.LastLogin,
CreatedAt: a.CreatedAt,
UpdatedAt: a.UpdatedAt,
}
}
func createDBAccount(a models.Account) dbAccount {
return dbAccount{
ID: a.ID,
AccountStatus: a.AccountStatus,
AccountType: a.AccountType,
AccountName: a.AccountName,
Profile: dbProfile{
FirstName: convert.NewSQLNullString(a.Profile.FirstName),
LastName: convert.NewSQLNullString(a.Profile.LastName),
Country: convert.NewSQLNullString(a.Profile.Country),
City: convert.NewSQLNullString(a.Profile.City),
Gender: convert.NewSQLNullString(a.Profile.Gender),
Birthday: convert.NewSQLNullTime(a.Profile.Birthday),
Description: convert.NewSQLNullString(a.Profile.Description),
SocialLinks: a.Profile.SocialLinks,
Interests: a.Profile.Interests,
},
LastIP: a.LastIP,
LastLogin: a.LastLogin,
Settings: dbSettings{
Notifications: dbNotifications{
Email: dbEmailNotification{
Email: a.Settings.Notifications.Email.Email,
Scope: a.Settings.Notifications.Email.Scope,
},
},
},
UpdatedAt: a.UpdatedAt,
CreatedAt: a.CreatedAt,
}
}
// New creates postgresRepository
func New() (account.Repository, error) {
psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
return &repository{
psql: psql,
}, nil
}
type repository struct {
psql sq.StatementBuilderType
}
func getCaller() string {
// Remove getCaller from stack
if callerName, ok := caller.GetCaller(2); ok == true {
return callerName
}
return repositoryPrefix + "unknown caller"
}
func (r *repository) exec(executor database.QueryExecutor, queryBuilder database.QueryBuilder) error {
return database.ExecWithCaller(
getCaller(),
executor,
queryBuilder,
)
}
func (r *repository) queryRow(executor database.QueryExecutor, queryBuilder database.QueryBuilder) (*sql.Row, error) {
return database.QueryRowWithCaller(
getCaller(),
executor,
queryBuilder,
)
}
func (r *repository) query(
executor database.QueryExecutor,
queryBuilder database.QueryBuilder,
scanFunc func(rows *sql.Rows) error,
) error {
return database.QueryWithCaller(
getCaller(),
executor,
queryBuilder,
scanFunc,
)
}