forked from fabric8-services/fabric8-wit
/
label.go
216 lines (202 loc) · 6.64 KB
/
label.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
package controller
import (
"net/http"
"strings"
"github.com/fabric8-services/fabric8-wit/app"
"github.com/fabric8-services/fabric8-wit/application"
"github.com/fabric8-services/fabric8-wit/errors"
"github.com/fabric8-services/fabric8-wit/jsonapi"
"github.com/fabric8-services/fabric8-wit/label"
"github.com/fabric8-services/fabric8-wit/login"
"github.com/fabric8-services/fabric8-wit/rest"
"github.com/fabric8-services/fabric8-wit/space"
"github.com/goadesign/goa"
)
// LabelController implements the label resource.
type LabelController struct {
*goa.Controller
db application.DB
config LabelControllerConfiguration
}
// LabelControllerConfiguration the configuration for the LabelController
type LabelControllerConfiguration interface {
GetCacheControlLabels() string
GetCacheControlLabel() string
}
// NewLabelController creates a label controller.
func NewLabelController(service *goa.Service, db application.DB, config LabelControllerConfiguration) *LabelController {
return &LabelController{
Controller: service.NewController("LabelController"),
db: db,
config: config}
}
// Show retrieve a single label
func (c *LabelController) Show(ctx *app.ShowLabelContext) error {
var lbl *label.Label
err := application.Transactional(c.db, func(appl application.Application) error {
var err error
lbl, err = appl.Labels().Load(ctx, ctx.LabelID)
return err
})
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
return ctx.OK(&app.LabelSingle{
Data: ConvertLabel(ctx.Request, *lbl),
})
}
// Create runs the create action.
func (c *LabelController) Create(ctx *app.CreateLabelContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
if ctx.Payload.Data.Attributes.Name == nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrBadRequest("Name cannot be empty"))
}
lbl := &label.Label{
SpaceID: ctx.SpaceID,
Name: strings.TrimSpace(*ctx.Payload.Data.Attributes.Name),
}
if ctx.Payload.Data.Attributes.TextColor != nil {
lbl.TextColor = *ctx.Payload.Data.Attributes.TextColor
}
if ctx.Payload.Data.Attributes.BackgroundColor != nil {
lbl.BackgroundColor = *ctx.Payload.Data.Attributes.BackgroundColor
}
if ctx.Payload.Data.Attributes.BorderColor != nil {
lbl.BorderColor = *ctx.Payload.Data.Attributes.BorderColor
}
err = application.Transactional(c.db, func(appl application.Application) error {
return appl.Labels().Create(ctx, lbl)
})
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
result := &app.LabelSingle{
Data: ConvertLabel(ctx.Request, *lbl),
}
ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.Request, app.LabelHref(ctx.SpaceID, result.Data.ID)))
return ctx.Created(result)
}
// ConvertLabel converts from internal to external REST representation
func ConvertLabel(request *http.Request, lbl label.Label) *app.Label {
labelType := label.APIStringTypeLabels
spaceID := lbl.SpaceID.String()
relatedURL := rest.AbsoluteURL(request, app.LabelHref(spaceID, lbl.ID))
spaceRelatedURL := rest.AbsoluteURL(request, app.SpaceHref(spaceID))
l := &app.Label{
Type: labelType,
ID: &lbl.ID,
Attributes: &app.LabelAttributes{
TextColor: &lbl.TextColor,
BackgroundColor: &lbl.BackgroundColor,
BorderColor: &lbl.BorderColor,
Name: &lbl.Name,
CreatedAt: &lbl.CreatedAt,
UpdatedAt: &lbl.UpdatedAt,
Version: &lbl.Version,
},
Relationships: &app.LabelRelations{
Space: &app.RelationGeneric{
Data: &app.GenericData{
Type: &space.SpaceType,
ID: &spaceID,
},
Links: &app.GenericLinks{
Self: &spaceRelatedURL,
Related: &spaceRelatedURL,
},
},
},
Links: &app.GenericLinks{
Self: &relatedURL,
Related: &relatedURL,
},
}
return l
}
// List runs the list action.
func (c *LabelController) List(ctx *app.ListLabelContext) error {
err := application.Transactional(c.db, func(appl application.Application) error {
labels, err := appl.Labels().List(ctx, ctx.SpaceID)
if err != nil {
return err
}
res := &app.LabelList{}
res.Data = ConvertLabels(appl, ctx.Request, labels)
return ctx.OK(res)
})
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
return nil
}
// ConvertLabels from internal to external REST representation
func ConvertLabels(appl application.Application, request *http.Request, labels []label.Label) []*app.Label {
var ls = []*app.Label{}
for _, i := range labels {
ls = append(ls, ConvertLabel(request, i))
}
return ls
}
// ConvertLabelsSimple converts an array of Label IDs into a Generic Reletionship List
func ConvertLabelsSimple(request *http.Request, labelIDs []interface{}) []*app.GenericData {
ops := make([]*app.GenericData, 0, len(labelIDs))
for _, labelID := range labelIDs {
ops = append(ops, ConvertLabelSimple(request, labelID))
}
return ops
}
// ConvertLabelSimple converts a Label ID into a Generic Reletionship
func ConvertLabelSimple(request *http.Request, labelID interface{}) *app.GenericData {
t := label.APIStringTypeLabels
i := labelID.(string)
return &app.GenericData{
Type: &t,
ID: &i,
}
}
// Update runs the update action.
func (c *LabelController) Update(ctx *app.UpdateLabelContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
if ctx.Payload.Data.Attributes.Version == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("data.attributes.version", nil).Expected("not nil"))
}
var lbl *label.Label
err = application.Transactional(c.db, func(appl application.Application) error {
var err error
lbl, err = appl.Labels().Load(ctx.Context, ctx.LabelID)
if err != nil {
return err
}
if lbl.Version != *ctx.Payload.Data.Attributes.Version {
return errors.NewVersionConflictError("version conflict")
}
if ctx.Payload.Data.Attributes.Name != nil {
lbl.Name = strings.TrimSpace(*ctx.Payload.Data.Attributes.Name)
}
if ctx.Payload.Data.Attributes.TextColor != nil {
lbl.TextColor = *ctx.Payload.Data.Attributes.TextColor
}
if ctx.Payload.Data.Attributes.BackgroundColor != nil {
lbl.BackgroundColor = *ctx.Payload.Data.Attributes.BackgroundColor
}
if ctx.Payload.Data.Attributes.BorderColor != nil {
lbl.BorderColor = *ctx.Payload.Data.Attributes.BorderColor
}
lbl, err = appl.Labels().Save(ctx, *lbl)
return err
})
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
result := &app.LabelSingle{
Data: ConvertLabel(ctx.Request, *lbl),
}
ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.Request, app.LabelHref(ctx.SpaceID, result.Data.ID)))
return ctx.OK(result)
}