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
146 lines (124 loc) · 3.67 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
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
package organisation
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"github.com/factly/mande-server/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/middlewarex"
"github.com/factly/x/renderx"
"github.com/spf13/viper"
)
type organisationApplication struct {
model.Base
Title string `gorm:"column:title" json:"title"`
Slug string `gorm:"column:slug;unique_index" json:"slug"`
Applications []application `json:"applications"`
}
type application struct {
model.Base
Name string `gorm:"column:name" json:"name"`
Description string `gorm:"column:description" json:"description"`
URL string `gorm:"column:url" json:"url"`
MediumID *uint `gorm:"column:medium_id;default:NULL" json:"medium_id"`
Medium *model.Medium `gorm:"foreignKey:medium_id" json:"medium"`
}
// my - Get super org
// @Summary Get super org
// @Description Get super org
// @Tags Organisation
// @ID get-super-organisation
// @Produce json
// @Param X-User header string true "User ID"
// @Success 200 {object} organisationApplication
// @Router /admin/organisations/my [get]
func my(w http.ResponseWriter, r *http.Request) {
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
// Fetched all organisations of the user
req, err := http.NewRequest("GET", viper.GetString("kavach_url")+"/organisations/my", nil)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
req.Header.Set("X-User", strconv.Itoa(uID))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.NetworkError()))
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
allOrg := []organisationApplication{}
err = json.Unmarshal(body, &allOrg)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
superOrgID, err := middlewarex.GetSuperOrganisationID("mande")
if err != nil {
renderx.JSON(w, http.StatusUnauthorized, nil)
return
}
for _, org := range allOrg {
if org.ID == uint(superOrgID) {
renderx.JSON(w, http.StatusOK, org)
return
}
}
renderx.JSON(w, http.StatusUnauthorized, nil)
}
// list - Get all org
// @Summary Get all org
// @Description Get all org
// @Tags Organisation
// @ID get-all-organisation
// @Produce json
// @Param X-User header string true "User ID"
// @Success 200 {array} organisationApplication
// @Router /admin/organisations/my [get]
func list(w http.ResponseWriter, r *http.Request) {
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
// Fetched all organisations of the user
req, err := http.NewRequest("GET", viper.GetString("kavach_url")+"/organisations/my", nil)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
req.Header.Set("X-User", strconv.Itoa(uID))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.NetworkError()))
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
allOrg := []organisationApplication{}
err = json.Unmarshal(body, &allOrg)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
renderx.JSON(w, http.StatusOK, allOrg)
}