-
Notifications
You must be signed in to change notification settings - Fork 485
/
UserRepository.go
159 lines (141 loc) · 4.96 KB
/
UserRepository.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
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
@description: user crud
*/
package repository
import (
"github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/internal/sql/models"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type UserRepository interface {
CreateUser(userModel *UserModel, tx *pg.Tx) (*UserModel, error)
UpdateUser(userModel *UserModel, tx *pg.Tx) (*UserModel, error)
GetById(id int32) (*UserModel, error)
GetAll() ([]UserModel, error)
FetchActiveUserByEmail(email string) (bean.UserInfo, error)
FetchUserDetailByEmail(email string) (bean.UserInfo, error)
GetByIds(ids []int32) ([]UserModel, error)
GetConnection() (dbConnection *pg.DB)
FetchUserMatchesByEmailId(email string) ([]UserModel, error)
FetchActiveOrDeletedUserByEmail(email string) (*UserModel, error)
}
type UserRepositoryImpl struct {
dbConnection *pg.DB
Logger *zap.SugaredLogger
}
func NewUserRepositoryImpl(dbConnection *pg.DB) *UserRepositoryImpl {
return &UserRepositoryImpl{dbConnection: dbConnection}
}
type UserModel struct {
TableName struct{} `sql:"users"`
Id int32 `sql:"id,pk"`
EmailId string `sql:"email_id,notnull"`
AccessToken string `sql:"access_token"`
Active bool `sql:"active,notnull"`
models.AuditLog
}
type UserRoleModel struct {
TableName struct{} `sql:"user_roles"`
Id int `sql:"id,pk"`
UserId int32 `sql:"user_id,notnull"`
RoleId int `sql:"role_id,notnull"`
User UserModel
models.AuditLog
}
func (impl UserRepositoryImpl) CreateUser(userModel *UserModel, tx *pg.Tx) (*UserModel, error) {
err := tx.Insert(userModel)
if err != nil {
impl.Logger.Error(err)
return userModel, err
}
//TODO - Create Entry In UserRole With Default Role for User
return userModel, nil
}
func (impl UserRepositoryImpl) UpdateUser(userModel *UserModel, tx *pg.Tx) (*UserModel, error) {
err := tx.Update(userModel)
if err != nil {
impl.Logger.Error(err)
return userModel, err
}
//TODO - Create Entry In UserRole With Default Role for User
return userModel, nil
}
func (impl UserRepositoryImpl) GetById(id int32) (*UserModel, error) {
var model UserModel
err := impl.dbConnection.Model(&model).Where("id = ?", id).Where("active = ?", true).Select()
return &model, err
}
func (impl UserRepositoryImpl) GetAll() ([]UserModel, error) {
var userModel []UserModel
err := impl.dbConnection.Model(&userModel).Where("active = ?", true).Order("updated_on desc").Select()
return userModel, err
}
func (impl UserRepositoryImpl) FetchActiveUserByEmail(email string) (bean.UserInfo, error) {
var users bean.UserInfo
query := "SELECT u.id, u.email_id, u.access_token FROM users u" +
" WHERE u.active = true and u.email_id ILIKE ? order by u.updated_on desc"
_, err := impl.dbConnection.Query(&users, query, email)
if err != nil {
impl.Logger.Error("Exception caught:", err)
return users, err
}
return users, nil
}
func (impl UserRepositoryImpl) FetchUserDetailByEmail(email string) (bean.UserInfo, error) {
//impl.Logger.Info("reached at FetchUserDetailByEmail:")
var users []bean.UserRole
var userFinal bean.UserInfo
query := "SELECT u.id, u.email_id, r.role FROM users u" +
" INNER JOIN user_roles ur ON ur.user_id=u.id" +
" INNER JOIN roles r ON r.id=ur.role_id" +
" WHERE u.email_id= ? and u.active = true" +
" ORDER BY u.updated_on desc;"
_, err := impl.dbConnection.Query(&users, query, email)
if err != nil {
return userFinal, err
}
var role []string
for _, item := range users {
userFinal.Exist = true
userFinal.Id = item.Id
userFinal.EmailId = item.EmailId
role = append(role, item.Role)
}
userFinal.Roles = role
return userFinal, nil
}
func (impl UserRepositoryImpl) GetByIds(ids []int32) ([]UserModel, error) {
var model []UserModel
err := impl.dbConnection.Model(&model).Where("id in (?)", pg.In(ids)).Where("active = ?", true).Select()
return model, err
}
func (impl *UserRepositoryImpl) GetConnection() (dbConnection *pg.DB) {
return impl.dbConnection
}
func (impl UserRepositoryImpl) FetchUserMatchesByEmailId(email string) ([]UserModel, error) {
var model []UserModel
err := impl.dbConnection.Model(&model).Where("email_id like (?)", "%"+email+"%").Where("active = ?", true).Select()
return model, err
}
func (impl UserRepositoryImpl) FetchActiveOrDeletedUserByEmail(email string) (*UserModel, error) {
var model UserModel
err := impl.dbConnection.Model(&model).Where("email_id like (?)", email).Limit(1).Select()
return &model, err
}