forked from fabric8-services/fabric8-wit
/
work-item-link-category.go
139 lines (129 loc) · 5.52 KB
/
work-item-link-category.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
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/goadesign/goa"
//satoriuuid "github.com/satori/go.uuid"
)
// WorkItemLinkCategoryController implements the work-item-link-category resource.
type WorkItemLinkCategoryController struct {
*goa.Controller
db application.DB
}
// NewWorkItemLinkCategoryController creates a WorkItemLinkCategoryController.
func NewWorkItemLinkCategoryController(service *goa.Service, db application.DB) *WorkItemLinkCategoryController {
if db == nil {
panic("db must not be nil")
}
return &WorkItemLinkCategoryController{
Controller: service.NewController("WorkItemLinkCategoryController"),
db: db,
}
}
// enrichLinkCategorySingle includes related resources in the single's "included" array
func enrichLinkCategorySingle(ctx *workItemLinkContext, single *app.WorkItemLinkCategorySingle) error {
// Add "links" element
selfURL := rest.AbsoluteURL(ctx.RequestData, ctx.LinkFunc(*single.Data.ID))
single.Data.Links = &app.GenericLinks{
Self: &selfURL,
}
return nil
}
// enrichLinkCategoryList includes related resources in the list's "included" array
func enrichLinkCategoryList(ctx *workItemLinkContext, list *app.WorkItemLinkCategoryList) error {
// Add "links" element
for _, data := range list.Data {
selfURL := rest.AbsoluteURL(ctx.RequestData, ctx.LinkFunc(*data.ID))
data.Links = &app.GenericLinks{
Self: &selfURL,
}
}
return nil
}
// Create runs the create action.
func (c *WorkItemLinkCategoryController) Create(ctx *app.CreateWorkItemLinkCategoryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
cat, err := appl.WorkItemLinkCategories().Create(ctx.Context, ctx.Payload.Data.Attributes.Name, ctx.Payload.Data.Attributes.Description)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
linkCtx := newWorkItemLinkContext(ctx.Context, appl, c.db, ctx.RequestData, ctx.ResponseData, app.WorkItemLinkCategoryHref)
err = enrichLinkCategorySingle(linkCtx, cat)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link category: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
ctx.ResponseData.Header().Set("Location", app.WorkItemLinkCategoryHref(cat.Data.ID))
return ctx.Created(cat)
})
}
// Show runs the show action.
func (c *WorkItemLinkCategoryController) Show(ctx *app.ShowWorkItemLinkCategoryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
res, err := appl.WorkItemLinkCategories().Load(ctx.Context, ctx.ID)
if err != nil {
jerrors, httpStatusCode := jsonapi.ErrorToJSONAPIErrors(err)
return ctx.ResponseData.Service.Send(ctx.Context, httpStatusCode, jerrors)
}
linkCtx := newWorkItemLinkContext(ctx.Context, appl, c.db, ctx.RequestData, ctx.ResponseData, app.WorkItemLinkCategoryHref)
err = enrichLinkCategorySingle(linkCtx, res)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link category: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(res)
})
}
// List runs the list action.
func (c *WorkItemLinkCategoryController) List(ctx *app.ListWorkItemLinkCategoryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
result, err := appl.WorkItemLinkCategories().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.WorkItemLinkCategoryHref)
err = enrichLinkCategoryList(linkCtx, result)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link categories: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(result)
})
}
// Delete runs the delete action.
func (c *WorkItemLinkCategoryController) Delete(ctx *app.DeleteWorkItemLinkCategoryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
err := appl.WorkItemLinkCategories().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{})
})
}
// Update runs the update action.
func (c *WorkItemLinkCategoryController) Update(ctx *app.UpdateWorkItemLinkCategoryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
toSave := app.WorkItemLinkCategorySingle{
Data: ctx.Payload.Data,
}
linkCategory, err := appl.WorkItemLinkCategories().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.WorkItemLinkCategoryHref)
err = enrichLinkCategorySingle(linkCtx, linkCategory)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal("Failed to enrich link category: %s", err.Error()))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(linkCategory)
})
}