This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
my.go
66 lines (53 loc) · 1.55 KB
/
my.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
package space
import (
"net/http"
"github.com/factly/bindu-server/config"
"github.com/factly/bindu-server/util"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/bindu-server/model"
)
type orgWithSpace struct {
model.Organisation
Spaces []model.Space `json:"spaces"`
}
// list - Get all spaces for a user
// @Summary Show all spaces
// @Description Get all spaces
// @Tags Space
// @ID get-all-spaces
// @Produce json
// @Param X-User header string true "User ID"
// @Success 200 {array} []orgWithSpace
// @Router /spaces [get]
func my(w http.ResponseWriter, r *http.Request) {
orgList, err := util.RequestOrganisation(r)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
var allOrgIDs []int
for _, each := range orgList {
allOrgIDs = append(allOrgIDs, int(each.ID))
}
// Fetched all the spaces related to all the organisations
var allSpaces = make([]model.Space, 0)
config.DB.Model(model.Space{}).Where("organisation_id IN (?)", allOrgIDs).Preload("Logo").Preload("LogoMobile").Preload("FavIcon").Preload("MobileIcon").Find(&allSpaces)
result := make([]orgWithSpace, 0)
for _, organisation := range orgList {
spaces := make([]model.Space, 0)
for _, space := range allSpaces {
if space.OrganisationID == int(organisation.ID) {
spaces = append(spaces, space)
}
}
os := orgWithSpace{
Organisation: organisation,
Spaces: spaces,
}
result = append(result, os)
}
renderx.JSON(w, http.StatusOK, result)
}