-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
137 lines (120 loc) · 3.87 KB
/
link.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
package store
import (
"encoding/json"
"github.com/Brightscout/mattermost-plugin-azure-devops/server/constants"
"github.com/Brightscout/mattermost-plugin-azure-devops/server/serializers"
"github.com/pkg/errors"
)
type LinkStore interface {
StoreProject(project *serializers.ProjectDetails) error
GetProject() (*ProjectList, error)
GetAllProjects(userID string) ([]serializers.ProjectDetails, error)
DeleteProject(project *serializers.ProjectDetails) error
}
type ProjectListMap map[string]serializers.ProjectDetails
type ProjectList struct {
ByMattermostUserID map[string]ProjectListMap
}
func NewProjectList() *ProjectList {
return &ProjectList{
ByMattermostUserID: map[string]ProjectListMap{},
}
}
func storeProjectAtomicModify(project *serializers.ProjectDetails, initialBytes []byte) ([]byte, error) {
projectList, err := ProjectListFromJSON(initialBytes)
if err != nil {
return nil, err
}
projectList.AddProject(project.MattermostUserID, project)
modifiedBytes, marshalErr := json.Marshal(projectList)
if marshalErr != nil {
return nil, marshalErr
}
return modifiedBytes, nil
}
func (s *Store) StoreProject(project *serializers.ProjectDetails) error {
key := GetProjectListMapKey()
if err := s.AtomicModify(key, func(initialBytes []byte) ([]byte, error) {
return storeProjectAtomicModify(project, initialBytes)
}); err != nil {
return err
}
return nil
}
func (projectList *ProjectList) AddProject(userID string, project *serializers.ProjectDetails) {
if _, valid := projectList.ByMattermostUserID[userID]; !valid {
projectList.ByMattermostUserID[userID] = make(ProjectListMap)
}
projectKey := GetProjectKey(project.ProjectID, userID)
projectListValue := serializers.ProjectDetails{
MattermostUserID: userID,
ProjectID: project.ProjectID,
ProjectName: project.ProjectName,
OrganizationName: project.OrganizationName,
}
projectList.ByMattermostUserID[userID][projectKey] = projectListValue
}
func (s *Store) GetProject() (*ProjectList, error) {
key := GetProjectListMapKey()
initialBytes, appErr := s.Load(key)
if appErr != nil {
return nil, errors.New(constants.GetProjectListError)
}
projects, err := ProjectListFromJSON(initialBytes)
if err != nil {
return nil, errors.New(constants.GetProjectListError)
}
return projects, nil
}
func (s *Store) GetAllProjects(userID string) ([]serializers.ProjectDetails, error) {
projects, err := s.GetProject()
if err != nil {
return nil, err
}
var projectList []serializers.ProjectDetails
for _, project := range projects.ByMattermostUserID[userID] {
projectList = append(projectList, project)
}
return projectList, nil
}
func deleteProjectAtomicModify(project *serializers.ProjectDetails, initialBytes []byte) ([]byte, error) {
projectList, err := ProjectListFromJSON(initialBytes)
if err != nil {
return nil, err
}
projectKey := GetProjectKey(project.ProjectID, project.MattermostUserID)
projectList.DeleteProjectByKey(project.MattermostUserID, projectKey)
modifiedBytes, marshalErr := json.Marshal(projectList)
if marshalErr != nil {
return nil, marshalErr
}
return modifiedBytes, nil
}
func (s *Store) DeleteProject(project *serializers.ProjectDetails) error {
key := GetProjectListMapKey()
if err := s.AtomicModify(key, func(initialBytes []byte) ([]byte, error) {
return deleteProjectAtomicModify(project, initialBytes)
}); err != nil {
return err
}
return nil
}
func (projectList *ProjectList) DeleteProjectByKey(userID, projectKey string) {
for key := range projectList.ByMattermostUserID[userID] {
if key == projectKey {
delete(projectList.ByMattermostUserID[userID], key)
}
}
}
func ProjectListFromJSON(bytes []byte) (*ProjectList, error) {
var projectList *ProjectList
if len(bytes) != 0 {
unmarshalErr := json.Unmarshal(bytes, &projectList)
if unmarshalErr != nil {
return nil, unmarshalErr
}
} else {
projectList = NewProjectList()
}
return projectList, nil
}