-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
131 lines (114 loc) · 2.8 KB
/
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package designation
import (
"time"
"github.com/mrexmelle/connect-org/internal/config"
"gorm.io/gorm"
)
type Repository interface {
Create(req *Entity) (*Entity, error)
FindById(id string) (*Entity, error)
UpdateById(fields map[string]interface{}, ehid string) error
DeleteById(id string) error
FindByNodeId(nodeId string) ([]Entity, error)
FindByNodeIdAndRoleId(nodeId string, roleId string) ([]Entity, error)
}
type RepositoryImpl struct {
ConfigService *config.Service
TableName string
Query Query
}
func NewRepository(cfg *config.Service) Repository {
return &RepositoryImpl{
ConfigService: cfg,
TableName: "designations",
Query: NewQuery(cfg.ReadDb, "designations"),
}
}
func (r *RepositoryImpl) Create(req *Entity) (*Entity, error) {
result := r.ConfigService.WriteDb.Raw(
"INSERT INTO "+r.TableName+"(node_id, role_id, ehid, "+
"created_at, updated_at) "+
"VALUES(?, ?, ?, NOW(), NOW()) RETURNING id",
req.NodeId,
req.RoleId,
req.Ehid,
).Scan(&req.Id)
if result.Error != nil {
return nil, result.Error
}
return req, nil
}
func (r *RepositoryImpl) FindById(id string) (*Entity, error) {
response := Entity{
Id: id,
}
result := r.Query.SelectById(FieldsAllExceptId, id).First(&response)
if result.Error != nil {
return nil, result.Error
}
return &response, nil
}
func (r *RepositoryImpl) FindByNodeId(nodeId string) ([]Entity, error) {
response := []Entity{}
result := r.Query.SelectByNodeId(FieldsAll, nodeId).Find(&response)
if result.Error != nil {
return nil, result.Error
}
return response, nil
}
func (r *RepositoryImpl) UpdateById(fields map[string]interface{}, id string) error {
dbFields := map[string]interface{}{}
for i := range FieldsPatchable {
introspectedKey := FieldsPatchable[i]
value, ok := fields[introspectedKey]
if ok {
dbFields[introspectedKey] = value
}
}
if len(dbFields) > 0 {
dbFields["updated_at"] = time.Now()
result := r.ConfigService.WriteDb.
Table(r.TableName).
Where("id = ?", id).
Updates(dbFields)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
}
return nil
}
func (r *RepositoryImpl) DeleteById(id string) error {
now := time.Now()
result := r.ConfigService.WriteDb.
Table(r.TableName).
Where("id = ? AND deleted_at IS NULL", id).
Updates(
map[string]interface{}{
"ehid": "",
"deleted_at": now,
"updated_at": now,
},
)
if result.Error != nil {
return result.Error
}
return nil
}
func (r *RepositoryImpl) FindByNodeIdAndRoleId(
nodeId string,
roleId string,
) ([]Entity, error) {
response := []Entity{}
result := r.Query.SelectByNodeIdAndRoleId(
FieldsAll,
nodeId,
roleId,
).Find(&response)
if result.Error != nil {
return nil, result.Error
}
return response, nil
}