forked from fabric8-services/fabric8-wit
/
area.go
270 lines (231 loc) · 7.57 KB
/
area.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"fmt"
"strings"
"golang.org/x/net/context"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/application"
"github.com/almighty/almighty-core/area"
"github.com/almighty/almighty-core/errors"
"github.com/almighty/almighty-core/jsonapi"
"github.com/almighty/almighty-core/login"
"github.com/almighty/almighty-core/rest"
"github.com/goadesign/goa"
uuid "github.com/satori/go.uuid"
)
// AreaController implements the area resource.
type AreaController struct {
*goa.Controller
db application.DB
}
const pathSepInService = "/"
const pathSepInDatabase = "."
// NewAreaController creates a area controller.
func NewAreaController(service *goa.Service, db application.DB) *AreaController {
return &AreaController{Controller: service.NewController("AreaController"), db: db}
}
// ShowChild runs the show-child action
func (c *AreaController) ShowChild(ctx *app.ShowChildAreaContext) error {
id, err := uuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
parentArea, err := appl.Areas().Load(ctx, id)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
children, err := appl.Areas().ListChildren(ctx, parentArea)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.AreaList{}
res.Data = ConvertAreas(appl, ctx.RequestData, children, addResolvedPath)
return ctx.OK(res)
})
}
// CreateChild runs the create-child action.
func (c *AreaController) CreateChild(ctx *app.CreateChildAreaContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
parentID, err := uuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
parent, err := appl.Areas().Load(ctx, parentID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
reqArea := ctx.Payload.Data
if reqArea.Attributes.Name == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("data.attributes.name", nil).Expected("not nil"))
}
childPath := area.ConvertToLtreeFormat(parentID.String())
if parent.Path != "" {
childPath = parent.Path + pathSepInDatabase + childPath
}
newArea := area.Area{
SpaceID: parent.SpaceID,
Path: childPath,
Name: *reqArea.Attributes.Name,
}
err = appl.Areas().Create(ctx, &newArea)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.AreaSingle{
Data: ConvertArea(appl, ctx.RequestData, &newArea, addResolvedPath),
}
ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.RequestData, app.AreaHref(res.Data.ID)))
return ctx.Created(res)
})
}
// Show runs the show action.
func (c *AreaController) Show(ctx *app.ShowAreaContext) error {
id, err := uuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
a, err := appl.Areas().Load(ctx, id)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.AreaSingle{}
res.Data = ConvertArea(appl, ctx.RequestData, a, addResolvedPath)
return ctx.OK(res)
})
}
// addResolvedPath resolves the path in the form of /area1/area2/area3
func addResolvedPath(appl application.Application, req *goa.RequestData, mArea *area.Area, sArea *app.Area) error {
pathResolved, error := getResolvePath(appl, mArea)
sArea.Attributes.ParentPathResolved = pathResolved
return error
}
func getResolvePath(appl application.Application, a *area.Area) (*string, error) {
parentUuidStrings := strings.Split(area.ConvertFromLtreeFormat(a.Path), pathSepInService)
parentUuids := convertToUuid(parentUuidStrings)
parentAreas, err := appl.Areas().LoadMultiple(context.Background(), parentUuids)
if err != nil {
return nil, err
}
pathResolved := ""
for _, a := range parentUuids {
area := getAreaByID(a, parentAreas)
if area == nil {
continue
}
pathResolved = pathResolved + pathSepInService + area.Name
}
// Add the leading "/" in the "area1/area2/area3" styled path
if pathResolved == "" {
pathResolved = "/"
}
return &pathResolved, nil
}
func getAreaByID(id uuid.UUID, areas []*area.Area) *area.Area {
for _, a := range areas {
if a.ID == id {
return a
}
}
return nil
}
// AreaConvertFunc is a open ended function to add additional links/data/relations to a area during
// convertion from internal to API
type AreaConvertFunc func(application.Application, *goa.RequestData, *area.Area, *app.Area) error
// ConvertAreas converts between internal and external REST representation
func ConvertAreas(appl application.Application, request *goa.RequestData, areas []*area.Area, additional ...AreaConvertFunc) []*app.Area {
var is = []*app.Area{}
for _, i := range areas {
is = append(is, ConvertArea(appl, request, i, additional...))
}
return is
}
// ConvertArea converts between internal and external REST representation
func ConvertArea(appl application.Application, request *goa.RequestData, ar *area.Area, additional ...AreaConvertFunc) *app.Area {
areaType := area.APIStringTypeAreas
spaceType := "spaces"
spaceID := ar.SpaceID.String()
selfURL := rest.AbsoluteURL(request, app.AreaHref(ar.ID))
childURL := rest.AbsoluteURL(request, app.AreaHref(ar.ID)+"/children")
spaceSelfURL := rest.AbsoluteURL(request, app.SpaceHref(spaceID))
pathToTopMostParent := pathSepInService + area.ConvertFromLtreeFormat(ar.Path) // /uuid1/uuid2/uuid3s
i := &app.Area{
Type: areaType,
ID: &ar.ID,
Attributes: &app.AreaAttributes{
Name: &ar.Name,
CreatedAt: &ar.CreatedAt,
Version: &ar.Version,
ParentPath: &pathToTopMostParent,
},
Relationships: &app.AreaRelations{
Space: &app.RelationGeneric{
Data: &app.GenericData{
Type: &spaceType,
ID: &spaceID,
},
Links: &app.GenericLinks{
Self: &spaceSelfURL,
},
},
Children: &app.RelationGeneric{
Links: &app.GenericLinks{
Self: &childURL,
},
},
},
Links: &app.GenericLinks{
Self: &selfURL,
},
}
// Now check the path, if the path is empty, then this is the topmost area
// in a specific space.
if ar.Path != "" {
allParents := strings.Split(area.ConvertFromLtreeFormat(ar.Path), pathSepInService)
parentID := allParents[len(allParents)-1]
// Only the immediate parent's URL.
parentSelfURL := rest.AbsoluteURL(request, app.AreaHref(parentID))
i.Relationships.Parent = &app.RelationGeneric{
Data: &app.GenericData{
Type: &areaType,
ID: &parentID,
},
Links: &app.GenericLinks{
Self: &parentSelfURL,
},
}
}
for _, add := range additional {
add(appl, request, ar, i)
}
return i
}
// ConvertAreaSimple converts a simple area ID into a Generic Reletionship
func ConvertAreaSimple(request *goa.RequestData, id interface{}) *app.GenericData {
t := area.APIStringTypeAreas
i := fmt.Sprint(id)
return &app.GenericData{
Type: &t,
ID: &i,
Links: createAreaLinks(request, id),
}
}
func createAreaLinks(request *goa.RequestData, id interface{}) *app.GenericLinks {
selfURL := rest.AbsoluteURL(request, app.AreaHref(id))
return &app.GenericLinks{
Self: &selfURL,
}
}
func convertToUuid(uuidStrings []string) []uuid.UUID {
var uUIDs []uuid.UUID
for i := 0; i < len(uuidStrings); i++ {
uuidString, _ := uuid.FromString(uuidStrings[i])
uUIDs = append(uUIDs, uuidString)
}
return uUIDs
}