forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkitem.go
103 lines (91 loc) · 2.73 KB
/
workitem.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
package main
import (
"log"
"github.com/goadesign/goa"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/models"
"github.com/almighty/almighty-core/transaction"
)
// WorkitemController implements the workitem resource.
type WorkitemController struct {
*goa.Controller
wiRepository models.WorkItemRepository
ts transaction.Support
}
// NewWorkitemController creates a workitem controller.
func NewWorkitemController(service *goa.Service, wiRepository models.WorkItemRepository, ts transaction.Support) *WorkitemController {
ctrl := WorkitemController{Controller: service.NewController("WorkitemController"), wiRepository: wiRepository, ts: ts}
if ctrl.wiRepository == nil {
panic("nil work item repository")
}
return &ctrl
}
// Show runs the show action.
func (c *WorkitemController) Show(ctx *app.ShowWorkitemContext) error {
return transaction.Do(c.ts, func() error {
wi, err := c.wiRepository.Load(ctx.Context, ctx.ID)
if err != nil {
switch err.(type) {
case models.NotFoundError:
log.Printf("not found, id=%s", ctx.ID)
return goa.ErrNotFound(err.Error())
default:
return err
}
}
return ctx.OK(wi)
})
}
// Create runs the create action.
func (c *WorkitemController) Create(ctx *app.CreateWorkitemContext) error {
return transaction.Do(c.ts, func() error {
wi, err := c.wiRepository.Create(ctx.Context, ctx.Payload.Type, ctx.Payload.Name, ctx.Payload.Fields)
if err != nil {
switch err := err.(type) {
case models.BadParameterError, models.ConversionError:
return goa.ErrBadRequest(err.Error())
default:
return goa.ErrInternal(err.Error())
}
}
ctx.ResponseData.Header().Set("Location", app.WorkitemHref(wi.ID))
return ctx.Created(wi)
})
}
// Delete runs the delete action.
func (c *WorkitemController) Delete(ctx *app.DeleteWorkitemContext) error {
return transaction.Do(c.ts, func() error {
err := c.wiRepository.Delete(ctx.Context, ctx.ID)
if err != nil {
switch err.(type) {
case models.NotFoundError:
return goa.ErrNotFound(err.Error())
default:
return goa.ErrInternal(err.Error())
}
}
return ctx.OK([]byte{})
})
}
// Update runs the update action.
func (c *WorkitemController) Update(ctx *app.UpdateWorkitemContext) error {
return transaction.Do(c.ts, func() error {
toSave := app.WorkItem{
ID: ctx.ID,
Name: ctx.Payload.Name,
Type: ctx.Payload.Type,
Version: ctx.Payload.Version,
Fields: ctx.Payload.Fields,
}
wi, err := c.wiRepository.Save(ctx.Context, toSave)
if err != nil {
switch err := err.(type) {
case models.BadParameterError, models.ConversionError:
return goa.ErrBadRequest(err.Error())
default:
return goa.ErrInternal(err.Error())
}
}
return ctx.OK(wi)
})
}