forked from fabric8-services/fabric8-wit
/
work-item-link-type.go
185 lines (172 loc) · 7.37 KB
/
work-item-link-type.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
package main
import (
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/application"
"github.com/almighty/almighty-core/jsonapi"
"github.com/almighty/almighty-core/rest"
"github.com/almighty/almighty-core/workitem/link"
"github.com/goadesign/goa"
)
// WorkItemLinkTypeController implements the work-item-link-type resource.
type WorkItemLinkTypeController struct {
*goa.Controller
db application.DB
}
// NewWorkItemLinkTypeController creates a work-item-link-type controller.
func NewWorkItemLinkTypeController(service *goa.Service, db application.DB) *WorkItemLinkTypeController {
if db == nil {
panic("db must not be nil")
}
return &WorkItemLinkTypeController{
Controller: service.NewController("WorkItemLinkTypeController"),
db: db,
}
}
// enrichLinkTypeSingle includes related resources in the single's "included" array
func enrichLinkTypeSingle(ctx *workItemLinkContext, single *app.WorkItemLinkTypeSingle) error {
// Add "links" element
selfURL := rest.AbsoluteURL(ctx.RequestData, ctx.LinkFunc(*single.Data.ID))
single.Data.Links = &app.GenericLinks{
Self: &selfURL,
}
// Now include the optional link category data in the work item link type "included" array
linkCat, err := ctx.Application.WorkItemLinkCategories().Load(ctx.Context, single.Data.Relationships.LinkCategory.Data.ID)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
single.Included = append(single.Included, linkCat.Data)
return nil
}
// enrichLinkTypeList includes related resources in the list's "included" array
func enrichLinkTypeList(ctx *workItemLinkContext, list *app.WorkItemLinkTypeList) error {
// Add "links" element
for _, data := range list.Data {
selfURL := rest.AbsoluteURL(ctx.RequestData, ctx.LinkFunc(*data.ID))
data.Links = &app.GenericLinks{
Self: &selfURL,
}
}
// Build our "set" of distinct category IDs already converted as strings
categoryIDMap := map[string]bool{}
for _, typeData := range list.Data {
categoryIDMap[typeData.Relationships.LinkCategory.Data.ID] = true
}
// Now include the optional link category data in the work item link type "included" array
for categoryID := range categoryIDMap {
linkCat, err := ctx.Application.WorkItemLinkCategories().Load(ctx.Context, categoryID)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
list.Included = append(list.Included, linkCat.Data)
}
return nil
}
// Create runs the create action.
func (c *WorkItemLinkTypeController) Create(ctx *app.CreateWorkItemLinkTypeContext) error {
// WorkItemLinkTypeController_Create: start_implement
// Convert payload from app to model representation
model := link.WorkItemLinkType{}
in := app.WorkItemLinkTypeSingle{
Data: ctx.Payload.Data,
}
err := link.ConvertLinkTypeToModel(in, &model)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrBadRequest(err.Error()))
return ctx.BadRequest(jerrors)
}
return application.Transactional(c.db, func(appl application.Application) error {
linkType, err := appl.WorkItemLinkTypes().Create(ctx.Context, model.Name, model.Description, model.SourceTypeName, model.TargetTypeName, model.ForwardName, model.ReverseName, model.Topology, model.LinkCategoryID)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
// Enrich
linkCtx := newWorkItemLinkContext(ctx.Context, appl, c.db, ctx.RequestData, ctx.ResponseData, app.WorkItemLinkTypeHref)
err = enrichLinkTypeSingle(linkCtx, linkType)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link type: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
ctx.ResponseData.Header().Set("Location", app.WorkItemLinkTypeHref(linkType.Data.ID))
return ctx.Created(linkType)
})
// WorkItemLinkTypeController_Create: end_implement
}
// Delete runs the delete action.
func (c *WorkItemLinkTypeController) Delete(ctx *app.DeleteWorkItemLinkTypeContext) error {
// WorkItemLinkTypeController_Delete: start_implement
return application.Transactional(c.db, func(appl application.Application) error {
err := appl.WorkItemLinkTypes().Delete(ctx.Context, ctx.ID)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
return ctx.OK([]byte{})
})
// WorkItemLinkTypeController_Delete: end_implement
}
// List runs the list action.
func (c *WorkItemLinkTypeController) List(ctx *app.ListWorkItemLinkTypeContext) error {
// WorkItemLinkTypeController_List: start_implement
return application.Transactional(c.db, func(appl application.Application) error {
result, err := appl.WorkItemLinkTypes().List(ctx.Context)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
// Enrich
linkCtx := newWorkItemLinkContext(ctx.Context, appl, c.db, ctx.RequestData, ctx.ResponseData, app.WorkItemLinkTypeHref)
err = enrichLinkTypeList(linkCtx, result)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link types: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(result)
})
// WorkItemLinkTypeController_List: end_implement
}
// Show runs the show action.
func (c *WorkItemLinkTypeController) Show(ctx *app.ShowWorkItemLinkTypeContext) error {
// WorkItemLinkTypeController_Show: start_implement
return application.Transactional(c.db, func(appl application.Application) error {
res, err := appl.WorkItemLinkTypes().Load(ctx.Context, ctx.ID)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
// Enrich
linkCtx := newWorkItemLinkContext(ctx.Context, appl, c.db, ctx.RequestData, ctx.ResponseData, app.WorkItemLinkTypeHref)
err = enrichLinkTypeSingle(linkCtx, res)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link type: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(res)
})
// WorkItemLinkTypeController_Show: end_implement
}
// Update runs the update action.
func (c *WorkItemLinkTypeController) Update(ctx *app.UpdateWorkItemLinkTypeContext) error {
// WorkItemLinkTypeController_Update: start_implement
return application.Transactional(c.db, func(appl application.Application) error {
toSave := app.WorkItemLinkTypeSingle{
Data: ctx.Payload.Data,
}
linkType, err := appl.WorkItemLinkTypes().Save(ctx.Context, toSave)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
// Enrich
linkCtx := newWorkItemLinkContext(ctx.Context, appl, c.db, ctx.RequestData, ctx.ResponseData, app.WorkItemLinkTypeHref)
err = enrichLinkTypeSingle(linkCtx, linkType)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link type: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(linkType)
})
// WorkItemLinkTypeController_Update: end_implement
}