This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
64 lines (53 loc) · 1.64 KB
/
list.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
package space
import (
"net/http"
"github.com/factly/dega-server/config"
"github.com/factly/dega-server/service/core/model"
"github.com/factly/x/renderx"
)
type spaceWithPermissions struct {
model.Space
Permission *model.SpacePermission `json:"permission"`
}
type paging struct {
Nodes []spaceWithPermissions `json:"nodes"`
Total int64 `json:"total"`
}
// list - Get all Space permissions
// @Summary Show all Space permissions
// @Description Get all Space permissions
// @Tags Space_Permissions
// @ID get-all-space-permissions
// @Produce json
// @Param X-User header string true "User ID"
// @Param X-Space header string true "Space ID"
// @Param q query string false "Query"
// @Success 200 {array} paging
// @Router /core/permissions/spaces [get]
func list(w http.ResponseWriter, r *http.Request) {
result := paging{}
result.Nodes = make([]spaceWithPermissions, 0)
// Get all spaces
spaceList := make([]model.Space, 0)
config.DB.Model(&model.Space{}).Count(&result.Total).Find(&spaceList)
if len(spaceList) == 0 {
renderx.JSON(w, http.StatusOK, result)
return
}
// Get all permissions
permissionsList := make([]model.SpacePermission, 0)
config.DB.Model(&model.SpacePermission{}).Find(&permissionsList)
permissionsMap := make(map[uint]model.SpacePermission)
for _, perm := range permissionsList {
permissionsMap[perm.SpaceID] = perm
}
for _, space := range spaceList {
spacePerm := spaceWithPermissions{}
spacePerm.Space = space
if perm, found := permissionsMap[space.ID]; found {
spacePerm.Permission = &perm
}
result.Nodes = append(result.Nodes, spacePerm)
}
renderx.JSON(w, http.StatusOK, result)
}