forked from fabric8-services/fabric8-wit
/
trackerquery.go
131 lines (119 loc) · 4.35 KB
/
trackerquery.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
package main
import (
"fmt"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/application"
"github.com/almighty/almighty-core/jsonapi"
"github.com/almighty/almighty-core/log"
"github.com/almighty/almighty-core/remoteworkitem"
"github.com/goadesign/goa"
errs "github.com/pkg/errors"
)
// TrackerqueryController implements the trackerquery resource.
type TrackerqueryController struct {
*goa.Controller
db application.DB
scheduler *remoteworkitem.Scheduler
}
// NewTrackerqueryController creates a trackerquery controller.
func NewTrackerqueryController(service *goa.Service, db application.DB, scheduler *remoteworkitem.Scheduler) *TrackerqueryController {
return &TrackerqueryController{Controller: service.NewController("TrackerqueryController"), db: db, scheduler: scheduler}
}
// Create runs the create action.
func (c *TrackerqueryController) Create(ctx *app.CreateTrackerqueryContext) error {
result := application.Transactional(c.db, func(appl application.Application) error {
tq, err := appl.TrackerQueries().Create(ctx.Context, ctx.Payload.Query, ctx.Payload.Schedule, ctx.Payload.TrackerID)
if err != nil {
cause := errs.Cause(err)
switch cause.(type) {
case remoteworkitem.BadParameterError, remoteworkitem.ConversionError:
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrBadRequest(err.Error()))
return ctx.BadRequest(jerrors)
default:
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal(err.Error()))
return ctx.InternalServerError(jerrors)
}
}
ctx.ResponseData.Header().Set("Location", app.TrackerqueryHref(tq.ID))
return ctx.Created(tq)
})
c.scheduler.ScheduleAllQueries()
return result
}
// Show runs the show action.
func (c *TrackerqueryController) Show(ctx *app.ShowTrackerqueryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
tq, err := appl.TrackerQueries().Load(ctx.Context, ctx.ID)
if err != nil {
cause := errs.Cause(err)
switch cause.(type) {
case remoteworkitem.NotFoundError:
log.Error(ctx, map[string]interface{}{
"trackerID": ctx.ID,
}, "tracker query controller not found")
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrNotFound(err.Error()))
return ctx.NotFound(jerrors)
default:
return errs.WithStack(err)
}
}
return ctx.OK(tq)
})
}
// Update runs the update action.
func (c *TrackerqueryController) Update(ctx *app.UpdateTrackerqueryContext) error {
result := application.Transactional(c.db, func(appl application.Application) error {
toSave := app.TrackerQuery{
ID: ctx.ID,
Query: ctx.Payload.Query,
Schedule: ctx.Payload.Schedule,
TrackerID: ctx.Payload.TrackerID,
}
tq, err := appl.TrackerQueries().Save(ctx.Context, toSave)
if err != nil {
cause := errs.Cause(err)
switch cause.(type) {
case remoteworkitem.BadParameterError, remoteworkitem.ConversionError:
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrBadRequest(err.Error()))
return ctx.BadRequest(jerrors)
default:
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal(err.Error()))
return ctx.InternalServerError(jerrors)
}
}
return ctx.OK(tq)
})
c.scheduler.ScheduleAllQueries()
return result
}
// Delete runs the delete action.
func (c *TrackerqueryController) Delete(ctx *app.DeleteTrackerqueryContext) error {
result := application.Transactional(c.db, func(appl application.Application) error {
err := appl.TrackerQueries().Delete(ctx.Context, ctx.ID)
if err != nil {
cause := errs.Cause(err)
switch cause.(type) {
case remoteworkitem.NotFoundError:
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrNotFound(err.Error()))
return ctx.NotFound(jerrors)
default:
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal(err.Error()))
return ctx.InternalServerError(jerrors)
}
}
return ctx.OK([]byte{})
})
c.scheduler.ScheduleAllQueries()
return result
}
// List runs the list action.
func (c *TrackerqueryController) List(ctx *app.ListTrackerqueryContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
result, err := appl.TrackerQueries().List(ctx.Context)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrInternal(fmt.Sprintf("Error listing tracker queries: %s", err.Error())))
return ctx.InternalServerError(jerrors)
}
return ctx.OK(result)
})
}